11.6.1重学C++之【继承的基本语法】

#include<stdlib.h>
#include<iostream>
#include<string>
using namespace std;



/*
    4.6 继承
    4.6.1 继承的基本语法
        继承优点:减少重复代码
        语法:  class 子类 :继承方式 父类{}
        其中,子类又称为派生类,父类又称为基类
*/



/*
class Java{
public:
    void header(){
        cout << "首页、公开课、登录、注册...(公共头部)" << endl;
    }
    void footer(){
        cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
    }
    void left(){
        cout << "Java、Python、C++、C...(公共分类列表)" << endl;
    }
    void content(){
        cout << "[Java学科视频]" << endl;
    }
};



class Python{
public:
    void header(){
        cout << "首页、公开课、登录、注册...(公共头部)" << endl;
    }
    void footer(){
        cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
    }
    void left(){
        cout << "Java、Python、C++、C...(公共分类列表)" << endl;
    }
    void content(){
        cout << "[Python学科视频]" << endl;
    }
};



class CPP{
public:
    void header(){
        cout << "首页、公开课、登录、注册...(公共头部)" << endl;
    }
    void footer(){
        cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
    }
    void left(){
        cout << "Java、Python、C++、C...(公共分类列表)" << endl;
    }
    void content(){
        cout << "[C++学科视频]" << endl;
    }
};
*/



// 继承实现页面
class BasePage{
public:
    void header(){
        cout << "首页、公开课、登录、注册...(公共头部)" << endl;
    }
    void footer(){
        cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
    }
    void left(){
        cout << "Java、Python、C++、C...(公共分类列表)" << endl;
    }
};



class Java:public BasePage{
public:
    void content(){
        cout << "[Java学科视频]" << endl;
    }
};



class Python:public BasePage{
public:
    void content(){
        cout << "[Python学科视频]" << endl;
    }
};



class CPP:public BasePage{
public:
    void content(){
        cout << "[C++学科视频]" << endl;
    }
};



void test1(){
    cout << "Java学习页面如下:" << endl;
    Java jj;
    jj.header();
    jj.footer();
    jj.left();
    jj.content();
    cout << endl;

    cout << "Python学习页面如下:" << endl;
    Python pp;
    pp.header();
    pp.footer();
    pp.left();
    pp.content();
    cout << endl;

    cout << "C++学习页面如下:" << endl;
    CPP CC;
    CC.header();
    CC.footer();
    CC.left();
    CC.content();
    cout << endl;
}



int main()
{
    test1();

    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/HAIFEI666/article/details/114916805
今日推荐