• 【C++札记】类的继承


    继承

    面向对象的设计思想,类对数据做了封装,并可以加入访问权限,类的继承是面向对象思想的精髓。类的继承可以让新类从以有的类中获得已有的特征。原有类称为基类或父类,新类称为派生类或子类。

    语法:

    class  子类类名:[继承方式]  父类类名
    {
    
    } ;

    继承的方式有三种:

    1.共有继承:(使用最广泛)

    在派生类中原来的公有成员,保护成员身份不变。原私有成员仍不可访问。

    2.私有继承:

    在派生类中原来的公有成员,保护成员都称为了保护成员。原私有成员仍不可访问。

    3.保护继承:

    在派生类中原来的公有成员,保护成员都称为了私有成员。原私有成员仍不可访问。

    派生类的构造函数和析构函数

    派生类继承基类过程中,基类的构造函数和析构是不能继承下来的。所以,派生类必须设置自己的构造函数和析构函数。

    派生类构造函数语法:

    派生类名::派生类名(参数):基类名(参数),派生类新成员()
    {
        
    }
    

    构造调用顺序:

    基类构造--->派生类构造

    析构函数调用顺序

    派生类析构--->基类析构

    代码演示:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Man
    {
    public:
        Man(string sn, int i):name(sn),age(i)
        {
            cout << "Man construct" << endl;
        }
    
        ~Man()
        {
            cout << "Man destruct" << endl;
        }
    
        void dis()
        {
            cout << "ame:" << name << endl;
            cout << "age:" << age << endl;
        }
    private:
        string  name;
        int     age;
    };
    
    class Birthday
    {
    public:
        Birthday(int y, int m):year(y), month(m)
        {
            cout << "Birthday construct " << endl;
        }
    
        ~Birthday()
        {
            cout << "Birthday destruct " << endl;
        }
    private:
        int year;
        int month;
    };
    
    class Student:public Man
    {
    public:
        Student(string name, int age, float fs):Man(name, age),bday(10, 10),_score(fs)
        {
            cout << "Student construct" << endl;
        }
    
        ~Student()
        {
            cout << "Student destruct" << endl;
        }
    private:
        float   _score;
        Birthday bday;
    };
    
    int main()
    {
        Student stu("wpf", 18, 100);
        stu.dis();
    }
    

    运行结果:

    同名隐藏

    子类中定义了与父类同名的方法(不管参数),子类的该方法将会隐藏掉所有的父类的同名方法;

    
    class Father
    {
    public:
    	void show()
    	{
    		cout << "Father	show()" << endl;
    	}
    
    	void show(int a)
    	{
    		cout << "Father	show(int a)" << endl;
    	}
    };
    
    class Son : public Father
    {
    public:
    	void show()
    	{
    		cout << "Son	show()" << endl;
    	}
    };
    
    int main()
    {
    	Son son;
    	son.show();  //调用子类中的方法
    	son.show(1); //父类中的同名方法,void show(int a)被隐藏,无法调用
    	getchar();
    }

    多继承

    多继承是的新建的获得多个类(>=2)中获得已有的特征, 并非所有面向对象的语言都有这种语法,如Java,C#取消了多继承,多继承可能引起多种问题,如二义性等。

    多继承语法:

    派生类名:public 基类名1,public 基类名2
    {
    
    };

    代码演示:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    //基类X
    class X
    {
    public:
        X(int a):i(a)
        {
            cout << "construct X" << endl;
        }
    
        void displayX()
        {
            cout << "X:" << i << endl;
        }
    private:
        int i;
    };
    
    //基类Y
    class Y
    {
    public:
        Y(int a):i(a)
        {
            cout << "construct Y" << endl;
        }
    
        void displayY()
        {
            cout << "Y:" << i << endl;
        }
    private:
        int i;
    };
    
    //派生类Z
    class Z: public X, public Y
    {
    public:
        Z(int a, int b, int c):X(a), Y(b), i(c)
        {
            cout << "construct Z" << endl;
        }
    
        void displayZ()
        {
            cout << "Z:" << i << endl;
        }
     private:
        int i;
    };
    
    int main()
    {
        Z z(1,2,3);
        z.displayX();
        z.displayY();
        z.displayZ();
    }
    

    运行结果:

  • 相关阅读:
    docker~save与load的使用
    docker~从Dockerfile到Container的过程(终于算是OK了)
    docker~使用阿里加速器
    Draw2d中的布局管理器Layout比较
    利用glibc中锁结构的信息解决死锁问题
    android 利用重力感应监听 来电时翻转手机后静音。
    hdu 1754 I Hate It
    九度笔记之 1209最小邮票数
    java zip工具类
    基于XMPP实现的Openfire的配置安装+Android客户端的实现
  • 原文地址:https://www.cnblogs.com/woniu201/p/11694514.html
Copyright © 2020-2023  润新知