思维导图
2.自定义类
#include <iostream>
using namespace std;
class Rect
{
private:
int width;
int 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 << endl;
cout << "矩形的周长为:" << (width + height) * 2 << endl;
}
};
int main(int argc, char const *argv[])
{
/* code */
class Rect r1;
r1.init(10, 15);
r1.show();
r1.set_w(500);
r1.set_h(200);
r1.show();
return 0;
}