C++继承之private继承

1 概述

  通过继承机制,可以利用已有的对象类型来定义新对象类型。所定义的新对象类型不仅仅拥有新定义的成员,而且还同时拥有旧的成员。我们称已存在的用来派生新类的类为基类,又称为父类。由已存在的类派生出的新类称为派生类,又称为子类。。

2 对象访问作用域

作用域:

  • public(公有作用域) 对象自身、派生类内部,对象外部都可以访问。
  • protected(保护作用域) 对象自身、派生类内部可以访问。
  • private(私有作用域) 只有对象自身可以访问。

说明:

  • 友元函数和友元类不受上述作用域限制。
  • protected(保护作用域)就是为了派生使用的。

3 继承

继承分类:

  • public继承 派生类中父类public成员还是public成员。
  • protected继承 派生类中父类public成员变成protected成员。
  • private继承 派生类中父类public成员变成private成员。
  • 多重继承 派生类可以从多个父类派生。
  • 虚继承 - 虚继承 在多重继承,有共同父类时只保持有一个父类。

3.3 private继承

3.3.1 例子

#include <iostream>

struct BaseClass
{

	BaseClass(int num)   //构造函数
	: number(num)
	{
		std::cout << "BaseClass is called" << std::endl;
	}
	
	~BaseClass()  //析构函数
	{
		std::cout << "~BaseClass is called" << std::endl;
    }

	int getNumber() { return number; }//对外提供的接口
	bool setNumber(int num){
		if(num > 100)
			return false;
		number = num;
        return true;
	}

protected:  //保护成员变量,外部无法访问
	int number;
};

struct Child1Class : private BaseClass
{
	Child1Class(int num)   //构造函数
	: BaseClass(num)
	{
		std::cout << "Child1Class is called" << std::endl;
	}
	
	~Child1Class()  //析构函数
	{
		std::cout << "~Child1Class is called" << std::endl;
    }
	
    void printNumber()
    {
        std::cout << "child.number:" << getNumber() << std::endl;
    }

	bool setNumber(int num)//覆盖父类setNumber
	{
		if(num > 200)
			return false;
		number = num;
        return true;
	}
	using BaseClass::getNumber;
};

struct Child2Class : Child1Class
{
	Child2Class(int num)   //构造函数
	: Child1Class(num)
	{
		std::cout << "Child2Class is called" << std::endl;
	}
	
	~Child2Class()  //析构函数
	{
		std::cout << "~Child2Class is called" << std::endl;
    }
	int getNumber() { return BaseClass::getNumber(); } //BaseClass::getNumber()对Child2Class来说是私有的

};
int main(int argc, char *argv[])
{
    {
        BaseClass base(20);
        std::cout << "base.number: " << base.getNumber() << std::endl;
        base.setNumber(150);
        std::cout << "base.number: " << base.getNumber() << std::endl;
    }

	
	Child2Class child(40);
    
    child.printNumber();
    child.setNumber(150);
    child.printNumber();

	std::cout << "number: " << child.getNumber() << std::endl;//
	
	return 0;
}

3.3.2 运行结果

编译结果:

private.cpp: In member function ‘int Child2Class::getNumber():
private.cpp:69:27: error:struct BaseClass BaseClass::BaseClass’ is inaccessible within this context
   69 |  int getNumber() { return BaseClass::getNumber(); } //BaseClass::getNumber()对Child2Class来说是私有的
      |                           ^~~~~~~~~
private.cpp:4:1: note: declared here
    4 | {
      | ^
private.cpp:69:48: error: ‘BaseClass’ is not an accessible base of ‘Child2Class’
   69 |  int getNumber() { return BaseClass::getNumber(); } //BaseClass::getNumber()对Child2Class来说是私有的

从编译结果看BaseClass::getNumber函数对派生类Child2Class来说是private.

修改代码如下:

struct Child2Class : Child1Class
{
	Child2Class(int num)   //构造函数
	: Child1Class(num)
	{
		std::cout << "Child2Class is called" << std::endl;
	}
	
	~Child2Class()  //析构函数
	{
		std::cout << "~Child2Class is called" << std::endl;
    }
	int getNumber() { return Child1Class::getNumber(); } //调用Child1Class::getNumber()

};

重新实现getNumber函数调用Child1Class::getNumber()

运行结果:

BaseClass is called
base.number: 20
base.number: 20
~BaseClass is called
BaseClass is called
Child1Class is called
Child2Class is called
child.number:40
child.number:150
number: 150
~Child2Class is called
~Child1Class is called
~BaseClass is called
  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysnow010

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值