#include<iostream>
using namespace std;
class Sale{
public:
Sale();
Sale(double the_price);
virtual double bill() const;
double savings(const Sale& other) const;
protected:
double price;
};
bool operator < (const Sale& first,const Sale& second);
Sale::Sale():price(0){}
Sale::Sale(double the_price):price(the_price){}
double Sale::bill() const
{
return price;
}
double Sale::savings(const Sale& other) const
{
return (bill()-other.bill());
}
bool operator < (const Sale& first,const Sale& second)
{
return (first.bill()<second.bill());
}
class DiscountSale:public Sale
{
public:
DiscountSale();
DiscountSale(double the_price,double the_discount);
virtual double bill() const;
protected:
double discount;
};
DiscountSale::DiscountSale():Sale(),discount(0)
{}
DiscountSale::DiscountSale(double the_price,double the_discount):Sale(the_price),discount(the_discount){};
double DiscountSale::bill() const{
double fraction=discount/100;
return (1-fraction)*price;
}
int main(){
Sale simple(10.00);
DiscountSale discount(11.00,10);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
if(discount<simple){
cout<<"Discounted item is cheaper.\n";
cout<<"Savings is $"<<simple.savings(discount)<<endl;
}
else
cout<<"Discounted item is not cheaper.\n";
return 0;
}
为什么去了virtual之后输出的会是cout<<"Discounted item is not cheaper.\n";