1、C++中类和C中结构体的区别
C中结构体中不可以定义函数,类中可以定义函数;
C中结构体中成员没有私有、公有的区别;
2、
#include <iostream>
using namespace std;
class Rectangle {
private:
int length;
int width;
public:
void set_l(int l);
void set_w(int w);
int get_l();
int get_w();
void show();
};
void Rectangle::set_l(int l){
length=l;
}
void Rectangle::set_w(int w){
width=w;
}
int Rectangle::get_l(){
return length;
}
int Rectangle::get_w(){
return width;
}
void Rectangle::show(){
cout << "周长为:" << 2*(length+width) << endl;
cout << "面积为:" << length*width << endl;
}
int main()
{
Rectangle A;
A.set_l(10);
A.set_w(20);
cout << "矩形长为:" << A.get_l() << endl;
cout << "矩形宽为:" << A.get_w() << endl;
A.show();
return 0;
}
3、