#include <iostream>
using namespace std;
class Rectangle
{
private:
int w, h;
static int totalArea; //矩形总面积
static int totalNumber; //矩形总数
public:
Rectangle(int x,int y):w(x),h(y){
totalArea=w*h;
totalNumber++;
}
void display()
{ cout<<"长:"<<w<<",高:"<<h
<<",总面积:"<<totalArea<<",总数:"<<totalNumber<<endl; }
};
int Rectangle::totalArea=w*h;
int Rectangle::totalNumber=0;
int main()
{
Rectangle r1(2,4),r2(6,8);
s1.display();
s2.display();
return 0;
}
这个编译为什么会出问题呢
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
CSDN专家-三岁丫 2021-04-04 21:46关注小问题很多呀
#include <iostream> using namespace std; class Rectangle { private: int w, h; static int totalArea; //矩形总面积 static int totalNumber; //矩形总数 public: Rectangle(int x,int y):w(x),h(y){ totalArea=w*h; totalNumber++; } void display() { cout<<"长:"<<w<<",高:"<<h <<",总面积:"<<totalArea<<",总数:"<<totalNumber<<endl; } }; // 初始化为 0 int Rectangle::totalArea=0; int Rectangle::totalNumber=0; int main() { Rectangle r1(2,4),r2(6,8); // 要用 r1、r2 r1.display(); r2.display(); return 0; }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报