第14周课后习题
类的声明和成员函数的实现–this指针
好水的一题。。。。。。
#include <iostream>
using namespace std;
class Car
{
public:
void disp_welcomemsg();
int set_wheels(int n);
int get_wheels();
private:
int m_nWheels;
};
void Car::disp_welcomemsg()
{
cout<<"Welcome to the car world!"<<endl;
}
int Car::set_wheels(int n)
{
return this->m_nWheels=n;
}
int Car::get_wheels()
{
return this->m_nWheels;
}
int main()
{
int n;
cin >> n;
Car myfstcar, myseccar; //定义类对象
myfstcar.disp_welcomemsg();//访问成员函数,显示欢迎信息
myfstcar.set_wheels(n); //访问成员函数,设置车轮数量
myseccar.set_wheels(n+4); //访问成员函数,设置车轮数量
//访问成员函数,显示车轮数量
cout << "my first car wheels num = " << myfstcar.get_wheels() << endl;
cout << "my second car wheels num = " << myseccar.get_wheels() << endl;
return 0;
}