#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 ;
}
温馨提示:内容为网友见解,仅供参考