#include <iostream>
#include<string>
using namespace std;
class BookItem
{
private:
string bookName;
size_t cnt;
public:
BookItem(const string&s,size_t c,double p):
bookName(s),cnt(c),price(p)
{}
~BookItem(){}
protected:
double price;
public:
double bookPrice()
{
return this->price;
}
string getBookName()
{
return this->bookName;
}
size_t getBookCount()
{
return this->cnt;
}
virtual double money()
{
return cnt*price;
}
virtual void costMoney()
{
cout<<money()<<endl;
}
};
class BookBatchItem:public BookItem
{
private:
string bookName;
size_t cnt;
public:
BookBatchItem(const string&s,size_t c,double p,double discountRate):
BookItem(s,c,p),cnt(c),discount(discountRate)
{}
~BookBatchItem(){}
private:
double discount;
public:
double money()
{
if(cnt>=10)
return cnt*price*(1.0-discount);
else
return cnt*price;
}
void costMoney()
{
cout<<money()<<endl;
// cout<<cnt<<endl;
// cout<<price<<endl;
// cout<<discount<<endl;
// cout<<"..."<<endl;
}
};
int main()
{
BookItem b1("Uncle Tom's house",11,12.5);
b1.costMoney();
BookBatchItem b2("Gone with wind",11,12.5,0.12);
b2.costMoney();
BookItem* pb=&b1;
pb->costMoney();
pb=&b2;
pb->costMoney();
return 0;
}
class Base
{
public:
Base() { p = new char ; }
~Base() { delete p; }
private:
char * p ;
};
class Derived:public Base
{
public:
Derived() { d = new char[10] ; }
~Derived() { delete[] d; }
private:
char * d ;
};
int main()
{
Base *pA = new Derived();
delete pA ;
Derived *pA = new Derived();
delete pA ;
}
class Base
{
public:
virtual void Func() { cout<<"Base"<<endl; }
};
class Derived:public Base
{
public:
virtual void Func() { cout<<"Derived"<<endl; }
};
int main()
{
Derived obj;
Base * p1 = &obj;
Base & p2 = obj;
Base obj2 ;
obj.Func() ; //静态绑定,Derived的func
p1->Func(); //动态绑定,Derived的func
(*p1).Func(); //动态绑定,Derived的func
p2.Func(); //动态绑定,Derived的func
obj2.Func(); //静态绑定,Base的func
return 0 ;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有