跪求编程高手帮忙,小弟感激不尽!!(c++编程)

设计一个car类,它的数据成员要能描述一部汽车的品牌(honda),型号(civic),出厂年份和价格。类接口所包括的成员函数应提供合适的途径来访问数据成员(如汽车的款式或他的价格)。类还应该有一个compare成员函数,用来对两辆车进行比较:
void compare(const car&) const;
compare 成员函数还能输出一份简短的结果比较报告。

#include <iostream>
#include <map>
#include <string>

using namespace std;

class car
{
public:
car(int year=2001){price=0.0;};
void compare(const car&) const;
void setHonda(char* s){strcpy(honda,s);}
void setCivic(char* s){strcpy(civic,s);}
void setYear(int y){year=y;}
void setPrice(double d){price = d;}
char* getHonda(){return honda;}
char* getCivic(){return civic;}
int getYear(){return year;}
double getPrice(){return price;}
void show(){cout<<"\n品牌:"<<honda<<"\n"<<"型号:"<<civic<<"\n"<<"出厂年份:"<<year<<"\n价格:"<<price<<endl;}
private:
char honda[50] ;
char civic[50];
int year;
double price;
};

void car::compare(const car& c) const
{
cout<<"\n【价格比较】"<<endl;
cout<<honda<<":"<<civic<<":"<<price<<"万"<<endl;
cout<<c.honda<<":"<<c.civic<<":"<<c.price<<"万"<<endl;
cout<<"\n比较结果:"<<endl;

if (this->price > c.price )
cout<<this->honda<<" "<<this->civic<<"较贵!"<<endl;
else
cout<<c.honda<<" "<<c.civic<<"较贵!"<<endl;
}
int main()
{
car mzd,bmw;
mzd.setHonda("马自达6");
mzd.setCivic("07款 2.0 自动超豪华");
mzd.setYear(2009);
mzd.setPrice(18.98);

bmw.setHonda("宝马3系");
bmw.setCivic("2010款 318i进取型");
bmw.setYear(2010);
bmw.setPrice(28.30);

mzd.show();
bmw.show();
mzd.compare(bmw);
return 0 ;
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答
大家正在搜