C++继承(九)

32 篇文章 0 订阅
  1. 公共继承
    1. 父类中私有权限,子类不能访问
    2. 父类中保护权限,子类中变为保护权限
    3. 父类中公共权限,子类中变为公共权限
  2. 保护继承
    1. 父类中公共权限,子类中变为保护权限
    2. 父类中保护权限,子类中变为保护权限
    3. 父类中私有权限,子类不能访问
  3. 私有继承
    1. 父类中公共权限,子类中变为私有权限
    2. 父类中保护权限,子类中变为私有权限
    3. 父类中私有权限,子类不能访问

注意:

        父类中私有属性也会继承,但是在子类中无法访问

        父类中,构造函数、析构函数、重载不会被继承 

#include<iostream>
using namespace std;

class structure {
private:
	char pageName[30];
public:
	void header() {
		cout << "页面头部公共部分" << endl;
	}
	void aside() {
		cout<<"页面侧边栏公共部分" << endl;
	}
	void footer() {
		cout << "页面尾部公共部分" << endl;
	}
protected:
	char serverName[30];
};

//公共继承
class news:public structure {
private:
public:
	void desc() {
		strcpy_s(serverName, 30, "apache");
		//strcpy_s(pageName, 30, "index");
		cout << "新闻列表" << endl;
	}
};

//保护继承
class sports:protected structure {
private:
public:
	void desc() {
		strcpy_s(serverName, 30, "apache");
		cout << "体育列表" << endl;
	}
};

//私有继承
class games :private structure {
private:
public:
	void desc() {
		strcpy_s(serverName, 30, "apache");
		cout << "游戏列表" << endl;
		cout << "server " << serverName << endl;
	}
};

class test {
public:
	void t() {
		//strcpy_s(serverName,30, "apache");
	}
};

void test() {
	news n1;
	n1.header();
	n1.aside();
	n1.desc();
	n1.footer();

	cout << "next" << endl;

	sports s1;
	//s1.header();
	//s1.aside();
	s1.desc();
	//s1.footer();

	cout << "next" << endl;

	games g1;
	g1.desc();
}

int main() {
	test();
	system("pause");
	return EXIT_SUCCESS;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值