C++ 我怎样才能写这个程序?

C++ 我怎样才能写这个程序?,c++,C++,我的任务是编写main函数,它定义一个名为recs[]的对象数组,大小为4。recs的每个元素必须用宽度和长度初始化,我必须计算其面积和周长 #include <iostream> #include <cmath> using namespace std; struct recs { double width; double length; }; class rect { public: rect();

我的任务是编写main函数,它定义一个名为recs[]的对象数组,大小为4。
recs
的每个元素必须用宽度和长度初始化,我必须计算其面积和周长

#include <iostream>
#include <cmath>
using namespace std;

struct recs
{
    double width;
    double length;
};

class rect
{
    public:
           rect();
           double area; 
           double circumference;
           void setrect();
           void calarea();
           void calcircumf();
           void print(); 
           friend bool operator==(rect rec1,rect rec2);
    private:
            double width,length;
            };
            rect recs[4];

bool operator==(rect rec1,rect rec2)
{
     return (rec1.area == rec1.area && rec1.circumference == rec2.circumference);
}

rect::rect()
{
    width=0.0;
    length=0.0;
}

void rect::setrect()
{
     cout << "width = ";
     cin >> width;
     cout << "length = ";
     cin >> length;
     print();
}

void rect::calarea()
{
     width == length;
     cout << "the return value true" << endl;
}

void rect::calcircumf()
{
     width == length;
     cout << "the return value true" << endl;
}

void rect::print()
{
     cout << "width = " << width << endl;
     cout << "length = " << length << endl << endl;
     }

int main()
{   
    double area;
    double circumference;
    double width;
    double length;

    rect recs[4] = { {10.0, 12.0}, {12.0, 14.0}, {14.0, 16.0}, {16.0, 18.0} };

    for (int i = 0; i <= 4; i++)
    {
        area = width * length;
        cout << "the area "<< i << " = " << area << endl ;

        circumference = ((width + length) + (width + length));
        cout << "the circumference" << i << " = " << circumference << endl;

    };

    system("pause");
    return 0;
}
#包括
#包括
使用名称空间std;
结构记录
{
双倍宽度;
双倍长度;
};
类矩形
{
公众:
rect();
双区;
双周长;
void setrect();
void calarea();
void calcircumf();
作废打印();
友元布尔运算符==(rect rec1,rect rec2);
私人:
双宽、双长;
};
rect recs[4];
布尔运算符==(rect rec1,rect rec2)
{
返回值(rec1.area==rec1.area&&rec1.personal==rec2.personal);
}
rect::rect()
{
宽度=0.0;
长度=0.0;
}
void rect::setrect()
{
宽度;
长度;
打印();
}
void rect::calarea()
{
宽度==长度;
看不见这个

我希望这有帮助……)

//我的任务是编写main函数,该函数定义一个名为recs[]的对象数组,大小为4。
//REC的每个元素必须用宽度和长度初始化,我必须计算它的面积和周长。
#包括
使用名称空间std;
类矩形{
//数据抽象-不允许用户访问类变量
私人:
双倍宽度;
双倍长度;
公众:
Rect(){//默认构造函数
宽度=0.0;
长度=0.0;
}
//宽度和长度设置器
空隙设置宽度和长度(双w,双l);
//面积和周长计算器
双面积();
双周长();
};
void Rect::setWidthAndLength(双w,双l){
宽度=w;
长度=l;
}
双矩形::区域(){
返回宽度*长度;
}
双矩形::周长(){
返回2*宽度+2*长度;
}
int main(int argc,char*argv[]){
Rect r[4];
int i;
双宽、双长;

对于(i=0;ii)如果您遇到编译器错误,请在此处发布准确的错误消息。不要只发布所有代码并说“它未编译”。
width==length;
这些是为什么,顺便说一句?rect recs[4]={{{10.0,12.0},{12.0,14.0},{14.0,16.0},{16.0,18.0};-
rec'必须由构造函数初始化,而不是由
{…}“-错误就在那一行。很抱歉,我在这方面做得不太好…所以,我可以知道该把它放在哪里,因为我已经尝试了很多方法,但仍然不能…@H2CO3看到9GAG接管StackOverflow的证据真是太有趣了:-)谢谢尼米什……这真的很有帮助……@NurhidayahAbdullah很高兴知道:)如果这是您的解决方案,请标记它,因为它将帮助面临类似问题的人:)
//My task is to write the main function which defines an array of objects called recs[] with the size of 4.
//Each element of recs must be initialized with width and length, and I have to calculate its area and circumference.

#include <iostream>

using namespace std;

class Rect{

//Data Abstraction - Not letting the user access the class variables
private:    
    double width;
    double length;

public:

    Rect(){         //default constructor

        width = 0.0;
        length = 0.0;

    }

    //setters for width and length
    void setWidthAndLength(double w, double l);

    //area and circumference calculators
    double area(); 
    double circumference();     
};

void Rect :: setWidthAndLength(double w, double l){

width = w;
length = l;

}

double Rect :: area(){

return width * length;

}


double Rect :: circumference(){

return 2 * width + 2 * length;   

}

int main(int argc, char* argv[]){

Rect r[4];
int i;
double width,length;

for(i=0;i<4;i++){

    cout<<"Enter the width and length for rectangle "<<i<<endl;
    cin>>width>>length;

    //set the width
    r[i].setWidthAndLength(width,length);               

    cout<<"The Area of the Rectangle is "<<r[i].area()<<endl;
    cout<<"The Circumference of the Rectangle is "<<r[i].circumference()<<endl<<endl;

}

return 0;
}