#include <iostream>
using namespace std;
class Rect //创建矩形类
{
private: //私有成员
double width;
double height;
public: //公有成员
void init(int w,int h) //初始化宽高
{
width = w;
height = h;
}
void set_w(int w) //设置宽度
{
width = w;
}
void set_h(int h) //设置高度
{
height = h;
}
void show()
{
cout << "面积:" << width * height << "周长:" << (width + height) * 2 << endl;
}
};
int main()
{
Rect a;
int w,h;
cout << "请输入宽和高:";
cin >> w >> h;
a.init(w,h); //初始化
a.show();//输出
cout << "请输入宽:";
cin >> w;
a.set_w(w); //设置宽度
a.show();//输出
cout << "请输入高:";
cin >> h;
a.set_h(h); //设置高度
a.show();//输出
return 0;
}
【有道云笔记】C++.mindmap
https://note.youdao.com/s/8zn9E1DE