[C++] 继承

慈云数据 8个月前 (03-13) 技术支持 53 0

在这里插入图片描述

文章目录

  • 1、继承的概念及定义
    • 1.1 继承的概念
    • 1.2 继承的定义
      • 1.2.1 定义格式
      • 1.2.2 继承关系和访问限定符
      • 1.2.3 继承基类成员访问方式的变化
      • 2、基类和派生类对象赋值转换
      • 3、继承中的作用
      • 4、派生类的默认成员函数
      • 5、继承与友元
      • 6、继承与静态成员
      • 7、菱形继承,菱形虚拟继承
        • 7.1 菱形继承问题
        • 7.2 虚拟继承解决菱形继承问题
          • 7.2.1 虚拟继承解决问题的原理
          • 8、继承的总结

            1、继承的概念及定义

            1.1 继承的概念

            继承(inheritance)机制是面向对象程序设计使代码可以复用的最重要的手段,它允许程序员在保持原有类特性的基础上进行扩展,增加功能,这样产生新的类,称派生类。继承呈现了面向对象程序设计的层次结构,体现了由简单到复杂的认知过程。以前我们接触的复用都是函数复用,继承是类设计层次的复用。

            我们来看看继承的例子:

            class Person
            {
            public:
            	void Print()
            	{
            		cout 
            protected:
            	string _stuid; // 学号
            };
            class Teacher : public Person
            {
            protected:
            	string _jobid; // 工号
            };
            int main()
            {
            	Student s;
            	Teacher t;
            	s.Print();
            	t.Print();
            	return 0;
            }
            
            public:
            	void Print()
            	{
            		cout };
            int main()
            {
            	B b;
            	b._a;
            	b._b;
            	b._c;
            	return 0;
            }
            
            protected:
            	string _name; // 姓名
            	string _sex; // 性别
            	int _age; // 年龄
            };
            class Student : public Person
            {
            public:
            	int _No; // 学号
            };
            
            protected:
            	string _name = "小李子"; // 姓名
            	int _num = 111; // 身份证号
            };
            class Student : public Person
            {
            public:
            	void Print()
            	{
            		cout 
            	Student s1;
            	s1.Print();
            };
            
            public:
            	void fun()
            	{
            		cout 
            public:
            	void fun(int i)
            	{
            		A::fun(); // 调用基类的func()方法
            		cout 
            	B b;
            	b.fun(10);
            }
            int main()
            {
            	Test();
            	return 0;
            }
            
            public:
            	Person(const char* name)
            		: _name(name)
            	{
            		cout 
            		cout 
            		cout 
            		cout 
            public:
            	Student(const char* name, int num)
            		: Person(name)
            		, _num(num)
            	{
            		cout 
            		cout 
            		if (this != &s)
            		{
            			Person::operator =(s);
            			_num = s._num;
            		}
            		cout 
            		cout 
            	Student s1("张三", 1);
            	Student s2(s1);
            	Student s3("李四", 2);
            	s1 = s3;
            	return 0;
            }
            
            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; // 学号
            };
            void Display(const Person& p, const Student& s)
            {
            	cout 
            public:
            	Person() { ++_count; }
            protected:
            	string _name; // 姓名
            public:
            	static int _count; // 统计人的个数。
            };
            int Person::_count = 0;
            class Student : public Person
            {
            protected:
            	int _stuNum; // 学号
            };
            int main()
            {
            	cout 
            public:
            	string _name; // 姓名
            };
            class Student : public Person
            {
            protected:
            	int _num; //学号
            };
            class Teacher : public Person
            {
            protected:
            	int _id; // 职工编号
            };
            class Assistant : public Student, public Teacher
            {
            protected:
            	string _majorCourse; // 主修课程
            };
            
            public:
            	string _name; // 姓名
            };
            class Student : virtual public Person // 虚继承的关键字:virtual
            {
            protected:
            	int _num; //学号
            };
            class Teacher : virtual public Person
            {
            protected:
            	int _id; // 职工编号
            };
            class Assistant : public Student, public Teacher
            {
            protected:
            	string _majorCourse; // 主修课程
            };
            
            public:
            	int _a;
            };
            // class B : public A
            class B : virtual public A
            {
            public:
            	int _b;
            };
            // class C : public A
            class C : virtual public A
            {
            public:
            	int _c;
            };
            class D : public B, public C
            {
            public:
            	int _d;
            };
            
微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon