某桌子类Desk具有长、宽、高、重四个数据成员,为它设计构造函数,通过构造函数的参数对数据成员进行初始化。
#include<iostream>
using namespace std;
class Desk {
public:
Desk(int, int);
void outData() {
cout << "Wight=" << weigth << "\tHeight=" << high << endl;
cout << "Length=" << length << "\tWidth=" << width << endl;
}
private:
int width, length, weigth = 2, high = 3;
};
Desk::Desk(int w, int h) {
width = w;
length = h;
cout << "call constructor!" << endl;
}
void main() {
Desk d(3, 5);
d.outData();
}