01 继承的引出

#include <iostream>
using namespace std;
class BasePage {
public:
    void header() {
        cout << "这是页面顶部" << endl;
        
    }

    void footer() {
        cout << "这是页面低部" << endl;

    }


    void left() {
        cout << "这是页面左部分" << endl;

    }
};


class News: public BasePage {
public:
    

    void content() {
        cout << "这是内容" << endl;

    }
};

class Liveshow : public BasePage {
public:
    

    void content() {
        cout << "这是KPL直播" << endl;

    }
};
int main() {
    BasePage base;
    base.header();
    base.footer();
    Liveshow liveshow;
    liveshow.header();
    liveshow.content();
    system("pause");


}

02 继承方式

#include <iostream>
using namespace std;
class BasePage {
public:
    int a;
    BasePage(int x = 1, int y = 2, int z = 3):a(x), b(y), c(z) {

    }
protected:
    int b;
private:
    int c;
};


class Son1 : public BasePage {
    void func() {
        cout << a << endl; //父类中共有属性,可以继承,继承后还是public
        cout << b << endl; //父类中保护属性,可以继承,继承后还是protected
        
    }

};

class Son2 : protected BasePage {
    void func() {
        cout << a << endl;  //父类中共有属性,可以继承,继承后变为Protected
        cout << b << endl;    //父类中保护属性,可以继承,继承后还是protected
    }

};

class Son3 : private BasePage {
    void func() {
        cout << a << endl; //父类中共有属性,可以继承,继承后变为private
        cout << b << endl; //父类中保护属性,可以继承,继承后变为private
    }

};


class Grandson : public Son3 {
    void func() {
        //cout << a << endl; //访问不到了
        //cout << b << endl; //访问不到了
    }

};


void func1() {
    Son1 son1;
    //son1.b; 受保护属性类外访问不到
}

void func2() {
    Son2 son2;
    //son2.a; //受保护属性类外访问不到
}


void func2() {
    Son2 son2;
    //son2.a; //私有属性类外访问不到
}


int main() {

}

03 继承中的对象类型

#include <iostream>
using namespace std;

class Base {
public:
    int a;
protected:
    int b;
private:
    int c;
};


class Son :private Base {
public:
    int d;
};

int main() {
    Son s;
    cout << sizeof(s) << endl;
    system("pause");
}

04 继承中的构造和析构顺序

#include <iostream>
using namespace std;


class BasePage {
public:
    
    BasePage() {
        cout << "basepage默认构造函数调用" << endl;
    }

    ~BasePage() {
        cout << "basepage析构函数调用" << endl;
    }



};


class Son : public BasePage {
public:
    Son() {
        cout << "son构造函数调用" << endl;
    }
    ~Son() {
        cout << "Son析构函数调用" << endl;
    }

};


class Base2 {
public:
    Base2(int a) {
        this->m_a = a;
        cout << "带参数的构造函数" << endl;
    }

private:
    int m_a;

    
};

class sonofbase2 : public Base2 {
public:
    sonofbase2(int a): Base2(a) {

    }
private:

};


void test01() {
    BasePage bp;
    
}

void test02() {
    Son son;
}

void test03() {
    sonofbase2 son(10);

}
//先调用父类的构造函数
//再调用子类的构造函数
//先析构子类的构造函数
//再析构父类的构造函数

int main() {
    test01();
    test02();
    test03();
    system("pause");
}

05 继承中的同名函数处理

#include <iostream>
using namespace std;

class Base {
public:
    Base() {
        a = 10;
    }
    int a;

    void func() {
        cout << "父类func调用" << endl;
    }



    void func(int a ) {
        cout << "父类func(int a)调用" << endl;
    }
    
};


class Base2 :public Base {
public:
    Base2() {
        a = 200;
    }
    int a;

    void func() {
        cout << "子类func调用" << endl;
    }
};

void test01() {
    Base2 base2;
    cout << base2.a << endl;
    cout << base2.Base::a << endl;

    base2.func();
    base2.Base::func();
    base2.Base::func(10);
}

//如果子类和父类都有同名的函数 属性,子类会覆盖父类的成员嘛? 不会
// 如果子类和父类有相同的成员函数名称,子类会把父类的所有的同名版本隐藏
//想嗲用父类的方法需要加作用域。

int main() {
    test01();
    system("pause");
}

06 继承中的静态函数成员处理

#include <iostream>
using namespace std;

class Base {
public:
    
    static int a ;
    static void func() {
        cout << "父类func()调用" << endl;
    }

    static void func(int a) {
        cout << "父类func(a)调用" << endl;
    }



};

int Base::a = 10;


class Base2 : public Base {
public:
    static int a;
    static void func() {
        cout << "子类func()调用" << endl;
    }

};

int Base2::a = 20;


void test01() {
    Base2 base2;
    cout << base2.a << endl;
    cout << base2.Base::a << endl;
    Base2::Base::func(10);
    Base2::Base::func();
    Base::func();
    base2.func();
}

int main() {
    test01();
    system("pause");
}

07 多继承的概念和问题

#include <iostream>
using namespace std;

class Base {
public:
    Base() {
        this->m_A = 10;
    }
    int m_A;

};

class Base2 {
public:
    Base2() {
        this->m_A = 20;
    }
    int m_A;

};

//多继承容易产生二义性冲突 
class Son : public Base, public Base2 {
public:
    int m_C;
    int m_D;
};


void test01() {
    Son s;
    cout << sizeof(s) << endl;
    cout << s.Base::m_A << endl;
    cout << s.Base::m_A << endl;
}


int main() {
    test01();
    system("pause");
}

08 虚继承的概念和解决方法(菱形继承)

#include <iostream>
using namespace std;

class Animal {
public:
    int age;
};

//虚基类demo 
class Sheep : virtual public Animal {

};

//虚基类demo 
class Tuo : virtual public Animal {

};

//虚继承
class SheepTuo : public Sheep, public Tuo {

};

//解决菱形问题需要虚继承,节省内存
void test01() {
    SheepTuo st;
    st.Tuo::age = 20;
    st.Sheep::age = 10;
    st.age = 30;
    cout << st.age << endl;
}

int main() {
    test01();
    system("pause");
}
最后修改:2021 年 03 月 14 日 08 : 16 PM
如果觉得我的文章对你有用,请随意赞赏