面向对象程序设计题目,用c++
展开全部
class CCar
{
public:
CCar()
{
strBrand = "";
strType = "";
nDisplacement = 0;
strColor = "";
}
CCar(const string &brand, const string &type, int displacement, const string &color)
{
strBrand = brand;
strType = type;
nDisplacement = displacement;
strColor = color;
}
CCar(const CCar &car)
{
strBrand = car.strBrand;
strType = car.strType;
nDisplacement = car.nDisplacement;
strColor = car.strColor;
}
CCar &operator=(const CCar &car)
{
strBrand = car.strBrand;
strType = car.strType;
nDisplacement = car.nDisplacement;
strColor = car.strColor;
return *this;
}
friend ostream &operator<<(ostream &o, const CCar &car)
{
o << "品牌:" << car.strBrand << endl;
o << "类型:" << car.strType << endl;
o << "排量:" << car.nDisplacement << endl;
o << "颜色:" << car.strColor << endl;
return o;
}
friend istream &operator>>(istream &i, CCar &car)
{
cout << "输入品牌:";
i >> car.strBrand;
cout << "输入类型:";
i >> car.strType;
cout << "输入排量:";
i >> car.nDisplacement;
cout << "输入颜色:";
i >> car.strColor;
return i;
}
bool operator==(const CCar &car)
{
return nDisplacement == car.nDisplacement;
}
bool operator<(const CCar &car)
{
return nDisplacement < car.nDisplacement;
}
bool operator>(const CCar &car)
{
return nDisplacement > car.nDisplacement;
}
private:
string strBrand;
string strType;
int nDisplacement;
string strColor;
};
int main()
{
CCar car_1;
CCar car_2("宝马", "轿车", 1000, "白色");
car_1 = car_2;
CCar car_3(car_1);
CCar car_4;
cin >> car_4;
cout << car_4;
if (car_1 == car_2)
{
cout << "相等\n";
}
if (car_4 > car_3)
{
cout << "大于\n";
}
if (car_4 < car_1)
{
cout << "小于\n";
}
system("pause");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询