http://blog.csdn.net/ly21st http://ly21st.blog.chinaunix.net
分类: C/C++
2011-11-29 20:41:05
#include
#include
using namespace std;
class Item_base {
public:
Item_base(string& book,double f):isbn(book),price(f) {}
string book()
{
return isbn;
}
virtual double net_price(size_t n)
{
return n*price;
}
private:
string isbn;
protected:
double price;
};
class Bulk_item:public Item_base {
public:
Bulk_item(string book,double i): Item_base(book,i) {}
double net_price(int n)
{
if (n >= count)
return n*(1-discount)*price;
else
return n*price;
}
private:
size_t count;
double discount;
};
int main()
{
string book="java";
cout<<"基类对象的函数调用"<
Item_base a(book,30);
double total=a.net_price(2);
cout<<"the total price are:"<
book="c++";
cout<<"派生类对象的函数调用"<
Bulk_item b(book,40);
total=b.net_price(2);
cout<<"the total price are:"<
cout<<"基类类型指针的不同指向"<
Item_base *pa;
pa=&a;
total=pa->net_price(2);
cout<<"the total price are:"<
pa=&b;
total=pa->net_price(2);
cout<<"the total price are:"<
cout<<"基类类型对象引用的不同绑定"<
Item_base &sa=a;
total=sa.net_price(2);
cout<<"the total price are:"<
Item_base &sa2=b;
total=sa2.net_price(2);
cout<<"the total price are:"<
cout<<"派生类类型的指针的不同指向"<
Bulk_item *pb;
pb=static_cast
// pb=dynamic_cast
total=pb->net_price(2);
cout<<"the total price are:"<
pb=&b;
total=pb->net_price(2);
cout<<"the total price are:"<
getchar();
return 0;
}