zoukankan      html  css  js  c++  java
  • 嘀嘀咕(4)

    1.继承

    class Student
    {
    public:
        Student()
        {
    
        }
        Student(int id, string name)
        {
            this->id = id;
            this->name = name;
        }
    
        void printS() {
            cout << "id = " << this->id << ", name = " << this->name << endl;
        }
        int id;
        string name;
    };
    
    
    
    
    //通过继承创建一个新的学生类
    class Student2 :public Student
    {
    public:
        Student2(int id, string name, int score) :Student(id, name)        //调用父类构造函数
        {
    
            this->score = score;
        }
    
        void printS() {
            Student::printS();
            cout << "score = " << this->score << endl;
        }
    private:
        int score;
    };
    
    private成员在子类中依然存在,但是却无法访问到。不论何种方式继承基类,派生类都不能直接使用基类的私有成员  。
    

    父类指针可以指向子类对象

    void myPrint(parent *p)
    {
    }
    
    myPrint(&p);
    myPrint(&c);
    

    父类、子类出现重名变量

    class Parent
    {
    public:
        Parent(int a) {
            this->a = a;
        }
    
        int a;
    };
    
    class Child :public Parent
    {
    public:
        Child(int p_a, int c_a) :Parent(p_a)
        {
            this->a = c_a;
        }
    
        void print()
        {
            cout << Parent::a << endl;
            cout << this->a << endl;//child's a
        }
        int a;
    };
    

    继承中构造和析构

    先构造父类,再构造成员变量、最后构造自己
    先析构自己,在析构成员变量、最后析构父类
    
    class Object
    {
    public:
         Object(const char* s)
         {
               cout<<"Object()"<<"  "<<s<<endl;
         }
         ~Object()
         {
               cout<<"~Object()"<<endl;
         }
    };
    
    class Parent : public Object
    {
    public:
         Parent(const char* s) : Object(s)
         {
               cout<<"Parent()"<<"  "<<s<<endl;
         }
         ~Parent()
         {
               cout<<"~Parent()"<<endl;
         }
    };
    
    class Child : public Parent
    {
    public:
         Child() : o2("o2"), o1("o1"), Parent("Parameter  from  Child!")
         {
               cout<<"Child()"<<endl;
         }
         ~Child()
         {
               cout<<"~Child()"<<endl;
         }
    private:
         Object o1;
         Object o2;
    };
    
    void run()
    {
         Child child;
    }
    
    int main(int argc, char *argv[])
    {
         run();
         return 0;
    }
    

    2.多继承、虚继承

    //家具类
    class Furniture
    {
    public:
        int m; //材质
    };
    
    //将父亲类继承爷爷类  改成虚继承, 防止儿子在多继承的时候,出现爷爷中的变量会拷贝多份。
    class Bed:virtual public Furniture
    {
    public:
        void sleep() {
            cout << "在床上睡觉" << endl;
        }
    };
    
    
    class Sofa:virtual public Furniture
    {
    public:
        void sit() {
            cout << "在沙发上休息" << endl;
        }
    };
    
    //沙发床
    class SofaBed :public Bed, public Sofa
    {
    public:
        void SleepAndSit() {
            sleep();
            sit();
        }
    };
    

  • 相关阅读:
    F系列车牌识别设备
    金蝶云星空安装及卸载教程
    RG-RAC256高性能无线控制器
    关于IP网段划分
    Win10关闭自动更新的三种方法
    锐捷网络RG-S2528G-24P
    光纤信号的传输距离
    POE交换机
    光纤收发器
    大华工具管家 1.01.1官方版
  • 原文地址:https://www.cnblogs.com/EngineerZhang/p/9880079.html
Copyright © 2011-2022 走看看