zoukankan      html  css  js  c++  java
  • C++之继承

      1 #include <iostream>
      2 #include <string>
      3 
      4 using namespace std;
      5 //基类
      6 class Item_base
      7 {
      8 public:
      9     Item_base(const string &book=" ",double sale_price=0.0):
     10         isbn(book),
     11         price(sale_price){}
     12     string book() const
     13     {
     14             return isbn;
     15     }
     16     //虚函数可以被子类重新定义
     17     virtual double net_price(size_t n)const
     18     {
     19         return n*price;
     20     }
     21 private:
     22     string isbn;
     23 protected:
     24     double price;
     25 };
     26 //继承 子类
     27 class Bulk_item : public Item_base
     28 {
     29 public:
     30     Bulk_item(const string &book=" ",double sale_price=0.0,size_t qty=0,double disc_rate=0.0):
     31         Item_base(book,sale_price), //先初始化基类
     32         min_qty(qty),
     33         discount(disc_rate){} //初始化子类
     34     //虚函数进行重写
     35     double net_price(size_t cnt)const
     36     {
     37         if(cnt>=min_qty)
     38         {
     39             return cnt*(1-discount)*price;
     40         }else{
     41             return cnt*price;
     42         }
     43     }
     44 private:
     45     size_t min_qty;
     46     double discount;
     47 };
     48 
     49 //多态
     50 void print_total(ostream &o,Item_base* it,size_t n)
     51 {
     52     o<<"ISBN"<<it->book()<<"=="<<"sold"<<n<<"=="<<"price"<<it->net_price(n)<<endl;
     53 }
     54 //
     55 void print_total_1(ostream &o,Item_base it,size_t n)
     56 {
     57     o<<"ISBN"<<it.book()<<"=="<<"sold"<<n<<"=="<<"price"<<it.net_price(n)<<endl;
     58 }
     59 //
     60 void print_total_2(ostream &o,Item_base& it,size_t n)
     61 {
     62     o<<"ISBN"<<it.book()<<"=="<<"sold"<<n<<"=="<<"price"<<it.net_price(n)<<endl;
     63 }
     64 //=============================================================
     65 
     66 //派生类的构造函数
     67 class Base1
     68 {
     69 public:
     70     Base1(int i)
     71     {
     72         b1=i;
     73         cout<<"base1的构造函数被调用"<<endl;
     74     }
     75     void Print()const
     76     {
     77         cout<<b1<<endl;
     78     }
     79 private:
     80     int b1;
     81 };
     82 
     83 class Base2
     84 {
     85 public:
     86     Base2(int i)
     87     {
     88         b2=i;
     89         cout<<"base2的构造函数被调用"<<endl;
     90     }
     91     void Print()const
     92     {
     93         cout<<b2<<endl;
     94     }
     95 private:
     96     int b2;
     97 };
     98 
     99 class Base3
    100 {
    101 public:
    102     Base3()
    103     {
    104         b3=0;
    105         cout<<"base3的构造函数被调用"<<endl;
    106     }
    107     void Print()const
    108     {
    109         cout<<b3<<endl;
    110     }
    111 private:
    112     int b3;
    113 };
    114 class Member
    115 {
    116 public:
    117     Member(int i)
    118     {
    119         m=i;
    120         cout<<"Member的构造函数被调用"<<endl;
    121     }
    122     int GetM()
    123     {
    124         return m;
    125     }
    126 private:
    127     int m;
    128 };
    129 class Derive : public Base2,public Base1,public Base3
    130 {
    131 public:
    132     Derive(int i,int j,int k,int l);
    133     void Print();
    134 private:
    135     int d;
    136     Member mem;
    137 };
    138 Derive::Derive(int i,int j,int k,int l):Base1(i),Base2(j),mem(k)
    139 {
    140     d=l;
    141     cout<<"派生类的构造函数被调用了"<<endl;
    142 }
    143 
    144 void Derive::Print()
    145 {
    146     Base1::Print();
    147     Base2::Print();
    148     Base3::Print();
    149     cout<<mem.GetM()<<endl;
    150     cout<<d<<endl;
    151 }
    152 //============================================
    153 
    154 
    155 int main()
    156 {
    157     //继承测试
    158 //    Item_base item1("0-120324-k",20.00);
    159 //    Bulk_item item2("0-120324-k",20.00,10,0.12);
    160 //    cout<<item2.book()<<'-'<<item2.net_price(20)<<endl;
    161     //多态
    162 //    Item_base* books[5];
    163 //    books[0]=new Item_base("0-123-456-1",10.0);
    164 //    books[1]=new Bulk_item("0-123-456-2",20.0,6,0.05);
    165 //    books[2]=new Item_base("0-123-456-3",30.0);
    166 //    books[3]=new Bulk_item("0-123-456-4",40.0,4,0.05);
    167 //    books[4]=new Item_base("0-123-456-5",50.0);
    168 //
    169 //    int num[5];
    170 //    num[0]=2;
    171 //    num[1]=9;
    172 //    num[2]=5;
    173 //    num[3]=4;
    174 //    num[4]=3;
    175 //
    176 //    for(int i=0;i<5;i++)
    177 //    {
    178 //        print_total(cout,books[i],num[i]);
    179 //    }
    180 
    181     //派生类的构造函数
    182     //构造子类的对象必须先构造基类的对象然后是成员对象的构造函数最后构造自己的成员函数 只有先初始化基类对象才能子类使用
    183     //析构函数是正好相反的
    184 //    Derive d1(1,2,3,4);
    185 //    d1.Print();
    186 
    187 
    188     //转换与继承
    189     //基类转换成派生类是自动的 派生类转换成基类是强制的
    190 //    Item_base item1("0-120324-k",20.00);
    191 //    Bulk_item item2("0-120324-k",20.00,10,0.12);
    192 //    print_total_1(cout,item1,10);
    193 //    //派生类自动转换成基类 但是调用的方法为基类的方法没有打折 有问题
    194 //    print_total_1(cout,item2,10);
    195 //    print_total(cout,&item1,10);
    196 //    //指针转换调用方法没问题 可以打折
    197 //    print_total(cout,&item2,10);
    198 //    print_total_2(cout,item1,10);
    199 //    //引用转换调用方法没问题 可以打折
    200 //    print_total_2(cout,item2,10);
    201 
    202     //友元与继承  友元关系不能继承,要明确的授予友元关系
    203 
    204     return 0;
    205 }
  • 相关阅读:
    ASP.NET网页动态添加数据行
    动态生成自定义控件ascx如何给ascx传值
    读取txt文件将文本行组合成特定格式
    怎样对文本文件内的文本行排序
    动态变更Repeater控件HeaderTemplate列名
    减少代码冗余,封装你的程序
    在Repeater控件的OnItemDataBound获取数据源记录总数
    查看服务器系统资源(cpu,内容)利用率前几位的进程的方法
    Nginx反向代理+缓存开启+url重写+负载均衡(带健康探测)的部署记录
    gerrit代码简单备份方案分享
  • 原文地址:https://www.cnblogs.com/yh2924/p/12683831.html
Copyright © 2011-2022 走看看