加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C++-----对象的继承(一)

发布时间:2020-12-15 04:48:13 所属栏目:百科 来源:网络整理
导读:#include "stdafx.h" #include using namespace std; //继承: //减少代码的重复性; //网页实例: class news { public: void head() { cout } void footer() { cout } void left() { cout } void content() { cout } }; class Entertainment { public: voi

#include "stdafx.h"

#include

using namespace std;

//继承:

//减少代码的重复性;

//网页实例:

class news

{

public:

void head()

{

cout << "公共的头部" << endl;

}

void footer()

{

cout << "公共的底部" << endl;

}

void left()

{

cout << "左侧列表" << endl;

}

void content()

{

cout << "新闻播报" << endl;

}

};

class Entertainment

{

public:

void head()

{

cout << "公共的头部" << endl;

}

void footer()

{

cout << "公共的底部" << endl;

}

void left()

{

cout << "左侧列表" << endl;

}

void content()

{

cout << "娱乐" << endl;

}

};

void test01()

{

news m_new;

m_new.head();

m_new.footer();

m_new.left();

m_new.content();

Entertainment m_ent;

m_ent.head();

m_ent.footer();

m_ent.left();

m_ent.content();

}

//避免代码冗余,所以提前抽象一个基类网页;

//重复的代码写到这个基类网页上;

class basePage

{

public:

void head()

{

cout << "公共的头部" << endl;

}

void footer()

{

cout << "公共的底部" << endl;

}

void left()

{

cout << "左侧列表" << endl;

}

};

class news2 : public basePage //继承的写法,表示news2类继承于basePage;

{

public:

void content()

{

cout << "新闻播报" << endl;

}

};

class entertainment2 :public basePage

{

public:

void content()

{

cout << "娱乐" << endl;

}

};

class Game :public basePage

{

public:

void content()

{

cout << "游戏直播" << endl;

}

};

void test02()

{

entertainment2 m_ent2;

news2 m_new2;

Game m_game;

m_ent2.content();

m_new2.content();

m_game.content();

}

int main()

{

test01();

test02();

return 0;

}

//上述的basePage 叫做 基类(父类),其余的 news2,entertainment2 , Game 叫做派生类(子类);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读