封闭类的定义(P124)

/*

定义:
一个类的成员变量如果是另一个类的对象,则该成员变量称为“成员对象”。这两个类为包含关系。包含成员对象的类叫作封闭类。

*/

#include<iostream>
using namespace std;

class CTyre
{
private:
int radius;
int width;
public:
CTyre():radius(16),width(185){} //构造函数
CTyre(int r,int w):radius(r),width(w){} //构造函数
int getRadius()
{
return radius;
}
int getWidth()
{
return width;
}
};

class CCar
{
private:
int price;
CTyre tyre;
public:
CCar();
CCar(int p,int tr,int tw);
int getPrice()
{
return price;
}
CTyre getCTyre()
{
return tyre;
}
};

CCar::CCar()
{
price = 50010;
CTyre();
};

CCar::CCar(int p,int tr,int tw):price(p),tyre(tr,tw){}

int main()
{
CCar car(48900,17,225);
cout<<"price="<<car.getPrice();
cout<<"\tCTyre.Radius="<<car.getCTyre().getRadius()
<<"\tCTyre.Width="<<car.getCTyre().getWidth()<<endl;
CCar car1;
cout<<"price="<<car1.getPrice();
cout<<"\tCTyre.Radius="<<car1.getCTyre().getRadius()
<<"\tCTyre.Width="<<car1.getCTyre().getWidth()<<endl;
return 0;
}

posted @ 2020-03-09 17:40  CollisionDimension  阅读(570)  评论(0)    收藏  举报