zoukankan      html  css  js  c++  java
  • cc26a_demoCppPrimer_动态绑定_多态代码示范

    //多态性
        //从派生类到基类的转换
        //引用或者指针既可以指向基类对象,也可以指向派生类对象
        //只有通过引用或者指针调用虚函数才会发生动态绑定。
        //为什么定义虚的函数?可以重新定义。只有虚函数才可以重写,(基类或者派生类里面)

    ////动态绑定的方法,执行出来的效果,就是多态

      1 #include <iostream>//txwtech-202001
      2 #include <string>//多态,动态绑定
      3 
      4 using namespace std;//导入名称空间
      5 
      6 class Animal
      7 {
      8     //成员略
      9 };
     10 
     11 class Dog :public Animal
     12 {
     13     //成员略
     14 };
     15 class Cat :public Animal
     16 {
     17 
     18 };
     19 class Item_base
     20 {
     21 public:
     22     //int x;
     23     Item_base(const std::string &book = "",
     24         double sales_price = 0.0) :isbn(book), price(sales_price) {}//构造函数
     25     std::string book() const
     26     {
     27         return isbn;
     28     }
     29     virtual double net_price(size_t n) const//为什么定义虚的函数?可以重新定义。只有虚函数才可以重写
     30     {
     31         return n * price;
     32     }
     33 
     34 private://类的内部使用
     35     std::string isbn;
     36 protected://专门用来做继承用的
     37     double price;
     38 
     39 };
     40 class Bulk_item :public Item_base
     41 {
     42 public:
     43     Bulk_item(const std::string &book = "", double sales_price = 0.0,
     44         size_t qty = 0, double disc_rate = 0.0) :
     45         Item_base(book, sales_price), min_qty(qty), discount(disc_rate) {}
     46     void test()
     47     {
     48         //cout << x << endl;
     49         cout << price << endl;
     50         //cout << isbn << endl;
     51     }
     52     void test2(const Bulk_item &d, const Item_base &b)
     53     {
     54         //cout << d.x << endl;
     55         cout << d.price << endl;
     56         //cout << b.x << endl;
     57         //cout << b.price << endl;
     58     }
     59     double net_price(size_t cnt) const//继承函数时,这里可以重定义,继承函数
     60     {
     61         if (cnt >= min_qty)
     62             return cnt * (1 - discount)*price;
     63         else
     64             return cnt * price;
     65 
     66     }
     67 
     68 private:
     69     size_t min_qty;
     70     double discount;
     71 
     72 };
     73 //做一个函数
     74 void print_total(ostream &os, Item_base *item, size_t n)
     75 {
     76     os << "ISBN: " << item->book() << "\t number sold: " << n << "\ttotal price: " << item->net_price(n) << endl;
     77     //item->net_price(n)代码就是多态的
     78 }
     79 int main()
     80 {
     81     //Animal a;
     82     //Dog d;
     83     //Cat c;
     84     //Item_base item("0-12-3456-789", 9.9);
     85     //cout << item.book() << ": " << item.net_price(10) << endl;
     86     ////cout << item.x << endl;
     87     ///*cout << item.isbn << endl;
     88     //cout << item.price << endl;*/
     89     ////cout << "hello111" << endl;
     90     //Bulk_item item2("0-12-3456-789", 9.9, 10, 0.12);
     91     ////cout << item2.x << endl;
     92     //cout << item2.book() << ": " << item2.net_price(10) << endl;
     93     //item2.test();
     94     //item2.test2(item2, item);
     95 
     96     //多态,动态绑定的实现
     97 
     98     Item_base* a = new Item_base("1-234-567-01",11.0);
     99     Bulk_item* b = new Bulk_item("1-234-567-02",22.0,2,0.05);//指向子类对象的指针
    100 
    101     print_total(cout,a,2);
    102     print_total(cout,b,3);
    103 
    104     Item_base* books[5];//指针数组,5个指针
    105     books[0] = new Item_base("0-123-456-01",10.0);//new 创建基类对象
    106     books[1] = new Bulk_item("0-123-456-01", 20.0,6,0.05);//new 也可以指向派生类对象
    107     books[2] = new Item_base("0-123-456-03", 30.0);//new 创建基类对象
    108     books[3] = new Bulk_item("0-123-456-04", 40.0, 4, 0.15);//new 也可以指向派生类对象
    109     books[4] = new Bulk_item("0-123-456-05", 50.0, 2, 0.18);//new 也可以指向派生类对象
    110     
    111     int num[5];
    112     num[0] = 2;
    113     num[1] = 10;
    114     num[2] = 1;
    115     num[3] = 5;
    116     num[4] = 3;
    117 
    118     for (int i = 0; i < 5; i++)
    119     {
    120         print_total(cout,books[i],num[i]);
    121         //动态绑定的方法,执行出来的效果,就是多态
    122     //根据指针所指向的对象不同,指向不同的对象,对象去调用不同的虚函数。有可能是基类,有可能是
    123     //派生类的。
    124     }
    125     //分别调用基类的netprice与派生类的Netprice. netprice就是多态,根据对象决定(基类不打折,派生类打折)
    126     //比如飞机在飞,鸟儿也在非,不同的飞
    127     
    128     
    129     getchar();
    130     return 0;
    131 
    132     //多态性
    133     //从派生类到基类的转换
    134     //引用或者指针既可以指向基类对象,也可以指向派生类对象
    135     //只有通过引用或者指针调用虚函数才会发生动态绑定。
    136     //为什么定义虚的函数?可以重新定义。只有虚函数才可以重写,(基类或者派生类里面)
    137 }
    欢迎讨论,相互学习。 txwtech@163.com
  • 相关阅读:
    中国一些web service收藏
    DataSet 和 List<T> 相互 转换
    JS图表
    IIS DirectoryEntry
    JS弹框
    Remoting
    Del SVN Configuare File BAT
    Viewport3D对象转换成图片
    在WPF中进行碰撞检测
    Button自定义样式
  • 原文地址:https://www.cnblogs.com/txwtech/p/12192405.html
Copyright © 2011-2022 走看看