C++复习第六天(继承、多态)

发布时间 2023-04-14 19:52:19作者: ElevHe

vector.clear()  将 size 设置为0,capacity 不变

 

继承

//公共页面
class BasePage
{
public:
    void header()
    {
        cout << "首页、公开课、登录、注册...(公共头部)" << endl;
    }

    void footer()
    {
        cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
    }
    void left()
    {
        cout << "Java,Python,C++...(公共分类列表)" << endl;
    }

};

//Java页面
class Java : public BasePage
{
public:
    void content()
    {
        cout << "JAVA学科视频" << endl;
    }
};
//Python页面
class Python : public BasePage
{
public:
    void content()
    {
        cout << "Python学科视频" << endl;
    }
};
//C++页面
class CPP : public BasePage
{
public:
    void content()
    {
        cout << "C++学科视频" << endl;
    }
};

void test01()
{
    //Java页面
    cout << "Java下载视频页面如下: " << endl;
    Java ja;
    ja.header();
    ja.footer();
    ja.left();
    ja.content();
    cout << "--------------------" << endl;

    //Python页面
    cout << "Python下载视频页面如下: " << endl;
    Python py;
    py.header();
    py.footer();
    py.left();
    py.content();
    cout << "--------------------" << endl;

    //C++页面
    cout << "C++下载视频页面如下: " << endl;
    CPP cp;
    cp.header();
    cp.footer();
    cp.left();
    cp.content();


}

 继承的好处:减少重复的代码

继承方式:

1.公共继承

2.保护继承

3.私有继承 

// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>

using namespace std;

/*继承有三种形式
1.公共继承  
2.保护继承
3.私有继承

继承方式                    private继承           protected继承             public继承
基类的 private 成员         不可见                不可见                    不可见
基类的 protected 成员       变为private成员       仍为protected成员         仍为protected成员 
基类的 public 成员          变为private成员       变为protected成员         仍为public成员

*/ class father { public: string m_name; protected: int m_age; private: int m_passwoard; }; class son_1 :public father { void func() { cout << m_name; cout << m_age; // cout << m_password; 未定义的标识符 } }; class son_2 :protected father { void func() { cout << m_name; cout << m_age; // cout << m_password; 未定义的标识符 } }; class son_3 :private father { void func() { cout << m_name; cout << m_age; // cout << m_password; 未定义的标识符 } }; int main() { return 0; }