• C++学习基础九——继承


    1.不同于Java中通过extends实现继承,C++是通过:实现的。

    2.C++中同样包含public,private,protected三个关键字: 

    public关键字表示在任意其他类中可调用该成员。

    private关键字表示该成员只能在声明该成员的类中使用。

    protected关键字用于继承,可在本类中调用声明为protected的成员,也可以在子类中通过子类对象调用,而不能通过父类对象调用。

    3.virtual关键字表示该函数可以被子类继承并重写。

    如果父类的成员函数声明为virtual,则子类可以重新定义该成员函数,重新定义时去掉virtual关键字。

    如果父类的成员函数是普通的函数,则子类继承之后不能重新定义该函数。

    4.代码片段如下:

     1 #include <iostream>
     2 #include <string>
     3 
     4 using namespace std;
     5 
     6 class Item_base
     7 {
     8 public:
     9     Item_base(const string &is,double p):isbn(is),price(p){}
    10     string book()
    11     {
    12         return isbn;
    13     }
    14     virtual double calMoney(int c)
    15     {
    16         return c * price;
    17     }
    18 private:
    19     string isbn;
    20 protected:
    21     double price;    
    22 };
    23 
    24 class Bulk_item : public Item_base
    25 {
    26 public:
    27     Bulk_item::Bulk_item(const string &is,double p,int c,double dis):
    28         Item_base(is,p),min_py(c),discocunt(dis){}
    29     double calMoney(int c)
    30     {
    31         cout<<endl<<"子类"<<endl; 
    32         if(c > min_py)
    33         {
    34             return c * price * discocunt;
    35         }
    36         else
    37         {
    38             return c * price;
    39         }
    40     } 
    41     void test()
    42     {
    43         cout<<endl<<"子类测试!!"<<endl;
    44     }
    45 private:
    46     int min_py;
    47     double discocunt;
    48 };
    49 
    50 
    51     
    52 int main()
    53 {
    54     string itemIsbn = "x-123-1234-x";
    55     Item_base item(itemIsbn,10.0);
    56     cout<<item.calMoney(100)<<endl;
    57     
    58     Bulk_item item2("123-456-x",10.0,30,0.8);
    59     cout<<item2.calMoney(100)<<endl;
    60     item2.test();
    61     
    62     Item_base *item3 = new Bulk_item("123-456-789-x",10.0,10,0.8);
    63     cout<<item3->calMoney(20)<<endl;
    64     //不能调用子类的test方法 
    65     return 0;
    66 }

     5.C++中的继承不同于Java,C++支持公有继承、私有继承和受保护的继承。

    公有继承又称为接口继承,私有继承和受保护继承又称为实现继承。最常用的继承方式是公有继承。

    私有继承是指将父类中的public,protected的成员变为private。

    受保护继承是指将父类中public的成员变成protected的。

    public 继承:

     1 class A
     2 {
     3 public :
     4     A()
     5     {
     6         a = 10;
     7         b = 20;
     8         c = 30;
     9     }
    10     int a;
    11 protected:
    12     int b;
    13 private:
    14     int c;    
    15 };
    16 
    17 class B1 : public A
    18 {
    19 public :
    20     void test()
    21     {
    22         cout<<"公有继承  "<<a<<" , "<<b<<endl;
    23         //cout<<c<<endl;
    24     }    
    25 };

    private继承:

     1 class B2 : private A
     2 { 
     3     //私有继承将A中public protected的成员变为private 
     4 public :
     5     void test()
     6     {
     7         cout<<"私有继承  "<<a<<" , "<<b<<endl;
     8         //cout<<c<<endl;
     9     }
    10 };
    protected继承:
    1 class B3 : protected A
    2 {
    3 //受保护继承将A中public的成员变为protected
    4 public:
    5     void test()
    6     {
    7         cout<<"受保护继承  "<<a<<" , "<<b<<endl;
    8     }    
    9 };

    6.可以通过修改继承访问方式实现去除个别成员(关键字是using)。

    例如父类A中public 的成员,通过私有继承时,父类的public成员变为私有的,可以通过using A::a;语句实现公有。

     1 class B2 : private A
     2 { 
     3     //私有继承将A中public protected的成员变为private 
     4 public :
     5     using A::a2;//将a2变为public 的 
     6     void test()
     7     {
     8         cout<<"私有继承  "<<a<<" , "<<b<<endl;
     9         //cout<<c<<endl;
    10     }
    11 };

    7.默认继承访问级别:

    (1)class默认的继承是私有的,其内的成员默认的也是私有的。

    (2)struct默认的继承是公有的,其内的成员默认的也是公有的。

    (3)C++中除了默认的继承访问级别不同,class和struct是相同的。

    1 class D : A//默认的继承是private 
    2 {
    3     int a;//默认的成员变量是private 
    4 };
    5 
    6 struct E : A//默认的继承是public 
    7 {
    8     int a;//默认的是public    
    9 };
    8.整体代码如下:
     1 class A
     2 {
     3 public :
     4     A()
     5     {
     6         a = 10;
     7         b = 20;
     8         c = 30;
     9         a2 = 40;
    10     }
    11     int a;
    12     int a2; 
    13 protected:
    14     int b;
    15 private:
    16     int c;    
    17 };
    18 
    19 class D : A//默认的继承是private 
    20 {
    21     int a;//默认的成员变量是private 
    22 };
    23 
    24 struct E : A//默认的继承是public 
    25 {
    26     int a;//默认的是public    
    27 };
    28 
    29 class B1 : public A
    30 {
    31 public :
    32     void test()
    33     {
    34         cout<<"公有继承  "<<a<<" , "<<b<<endl;
    35         //cout<<c<<endl;
    36     }    
    37 };
    38 
    39 class B2 : private A
    40 { 
    41     //私有继承将A中public protected的成员变为private 
    42 public :
    43     using A::a2;//将a2变为public 的 
    44     void test()
    45     {
    46         cout<<"私有继承  "<<a<<" , "<<b<<endl;
    47         //cout<<c<<endl;
    48     }
    49 };
    50 
    51 class B3 : protected A
    52 {
    53 //受保护继承将A中public的成员变为protected
    54 public:
    55     void test()
    56     {
    57         cout<<"受保护继承  "<<a<<" , "<<b<<endl;
    58     }    
    59 };
    60 
    61 
    62 int main()
    63 {
    64     B1 b1;
    65     cout<<b1.a<<endl;
    66 //    cout<<b1.b<<endl;//不可访问
    67 //    cout<<b1.c<<endl;//不可访问
    68     b1.test();
    69     
    70     B2 b2;
    71     cout<<b2.a2<<endl; 
    72 //    cout<<b2.a<<endl;//不可访问
    73 //    cout<<b2.b<<endl;//不可访问
    74 //    cout<<b1.c<<endl;//不可访问
    75     b2.test();
    76     
    77     
    78     B3 b3;
    79     //cout<<b3.a<<endl;//不可访问
    80     //cout<<b3.b<<endl;//不可访问
    81     //cout<<b3.c<<endl;//不可访问
    82     b3.test();
    83     return 0;
    84 }
    后续更新中....
  • 相关阅读:
    bzoj 4451 : [Cerc2015]Frightful Formula FFT
    bzoj 3928: [Cerc2014] Outer space invaders
    HDU 5306 线段树
    bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形
    bzoj 4519: [Cqoi2016]不同的最小割 最小割树
    bzoj : 4504: K个串 区间修改主席树
    bzoj 4332:JSOI2012 分零食
    bzoj 2595 : [Wc2008]游览计划
    poj 3683 Priest John's Busiest Day
    bzoj 1823: [JSOI2010]满汉全席 && bzoj 2199 : [Usaco2011 Jan]奶牛议会 2-sat
  • 原文地址:https://www.cnblogs.com/calence/p/5880013.html
Copyright © 2020-2023  润新知