当前位置: 首页 > news >正文

六、继承(二)

1 继承与友元

如果一个基类中存在友元关系,那么这个友元关系能不能继承呢?

例:

#include <iostream>
using namespace std;
class Student;
class Person
{
public:
	friend void Display(const Person& p, const Student& s);
protected:
	string _name = "张三"; // 姓名
};

class Student : public Person
{
protected:
	int _stuNum = 12345678; // 学号
};

void Display(const Person& p, const Student& s)
{
	cout << p._name << endl;
	cout << s._stuNum << endl;//访问派生类Student的成员_stuNum
}

int main()
{
	Person p;
	Student s;
	Display(p, s);
	return 0;
}

运行结果:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

从运行结果可以看到,友元关系不能继承,也就是说基类友元不能访问派生类的私有和保护成员。

如果要让基类友元访问派生类的私有和保护成员,需要在派生类中也进行声明。

例:

#include <iostream>
using namespace std;
class Student;
class Person
{
public:
	friend void Display(const Person& p, const Student& s);
protected:
	string _name = "张三"; // 姓名
};

class Student : public Person
{
public:
	friend void Display(const Person& p, const Student& s);
protected:
	int _stuNum = 12345678; // 学号
};

void Display(const Person& p, const Student& s)
{
	cout << p._name << endl;
	cout << s._stuNum << endl;
}

int main()
{
	Person p;
	Student s;
	Display(p, s);
	return 0;
}

运行结果:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

2 继承与静态成员

如果一个基类中有存在静态成员,那么在派生类中是怎么继承的呢?

例:

#include <iostream>
using namespace std;
class Person
{
public:
	Person() { ++_count; }
protected:
	string _name; // 姓名
public:
	static int _count; // 统计人的个数。
};

int Person::_count = 0;

class Student : public Person
{
protected:
	int _stuNum; // 学号
};
class Graduate : public Student
{
protected:
	string _seminarCourse; // 研究科目
};

int main()
{
	Person p;
	Student s;
	Graduate g;
	cout << &(p._count) << endl;
	cout << &(s._count) << endl;
	cout << &(g._count) << endl;
	cout << "人数:" << Person::_count << endl;
	cout << "人数:" << Student::_count << endl;
	cout << "人数:" << Graduate::_count << endl;
	return 0;
}

运行结果:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

从运行结果可以看到,不管是基类还是派生类,静态成员_count的地址都是一样的,而且不管是基类还是派生类都可以对它进行访问,也就是说当基类定义了static静态成员,那么整个继承体系里面只有一个这样的成员。无论派生出多少个子类,都只有一个static成员实例

如果我们想实现一个不能被继承的类,该怎么做呢?

实际上,我们可以将类的构造和析构函数设为私有:

#include <iostream>
using namespace std;
class A
{
private:
	A()
	{}
    //~A()
	//{}
};

class B :public A
{};
int main()
{
	B bb;
	return 0;
}

将构造函数设为私有的运行结果:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

将析构函数设为私有的运行结果:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

但如果这样的话,当我们想定义一个A类的对象时,也将调用不到它的构造和析构函数,那该怎么办呢?

要解决这个问题,我们就可以在A中定义一个可以静态成员函数让它来调用私有的构造函数从而实现创建对象。

#include <iostream>
using namespace std;
class A
{
public:
	static A CreatObj()
	{
		return A();
	}
private:
	A()
	{}
};

class B :public A
{};
int main()
{
	A::CreatObj();
	return 0;
}

运行结果:外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

这样一来,就可以实现一个不能被继承同时又不影响创建自己对象的类了。

http://www.dtcms.com/a/122033.html

相关文章:

  • JavaScript学习教程,从入门到精通,JavaScript 运算符及语法知识点详解(8)
  • 2025年Java无服务器架构实战:AWS Lambda与Spring Cloud Function深度整合
  • uniapp 打包 H5 向 打包的APP 使用 @dcloudio/uni-webview-js 传值
  • 数据结构实验4.3:利用队列实现杨辉三角的输出
  • BOTA六维力矩传感器在三层AI架构中的集成实践:从数据采集到力控闭环
  • 绿算技术团队受邀出席英伟达GTC2025大会丨重塑AI存储新范式
  • 【android bluetooth 框架分析 01】【关键线程 3】【bt_jni_thread 线程介绍】
  • MySQL多表查询实战指南:从SQL到XML映射的完整实现(2W+字深度解析)
  • [Windows] Gopeed-v1.7.0
  • HashMap、LinkedHashMap与TreeMap的核心特性与使用场景总结
  • Navicat 17 for Mac 数据库管理
  • C语言资源自动释放实现详解:基于GCC cleanup属性
  • Socket通信保护概论,Android系列
  • SAP-ABAP:SAP PO接口中System Landscape(SL Landscape Directory,SLD)作用详解
  • windows11下pytorch(cpu)安装
  • 记录一次SSH和SFTP服务分离后文件上传权限问题
  • AI比人脑更强,因为被植入思维模型【52】福格行为模型
  • 0303hooks-react-仿低代码平台项目
  • OSPF的数据报文格式【复习篇】
  • 算法基础—二分算法
  • STM32 vs ESP32:如何选择最适合你的单片机?
  • 网络协议学习
  • PDFtk
  • 2025年3月全国青少年软件编程等级考试(Python六级)试卷及答案
  • 带无源位置反馈气动V型调节开关球阀的特点解析-耀圣
  • find指令中使用正则表达式
  • C++中STL学习(一)——向量、栈、堆、集合
  • PyQt6实例_A股财报数据维护工具_解说并数据与完整代码分享
  • ISP的过程
  • 用户注册(阿里云手机验证码)