做了一个手机品牌与价格的实验
#include <iostream>
using namespace std;
class cellphone
{
private:
int price;
string brand;
public:
cellphone(string a = "0", int b = 0)
{
brand = a;
price = b;
}
string showbrand()
{
return brand;
}
int showprice()
{
return price;
}
virtual void display()
{
cout<<brand<<" "<<price<<endl;
}
};
class icellphone:public cellphone
{
private:
int iprice;
string ibrand;
public:
icellphone(string a, int b, string c, int d):cellphone(a, b)
{
ibrand = c;
iprice = d;
}
string showibrand()
{
return ibrand;
}
int showiprice()
{
return iprice;
}
int operator-(icellphone &a);
void display()
{
cout<<ibrand<<" "<<iprice<<endl;
}
};
int icellphone::operator-(icellphone &a)
{
return iprice - a.iprice;
}
int main()
{
icellphone a("Huawei", 5000, "Huawei", 6000), b("iphone", 7000, "iphone", 8000);
cout<<a.showbrand()<<" "<<a.showprice()<<endl;
cout<<a.showibrand()<<" "<<a.showiprice()<<endl;
cout<<b.showbrand()<<" "<<b.showprice()<<endl;
cout<<b.showibrand()<<" "<<b.showiprice()<<endl;
cout<<b - a<<endl;
a.display();
b.display();
return 0;
}