C++ -- 继承

1. 继承的概念和定义

1.1 概念

继承机制是面向对象设计程序使得代码可以复用,它允许程序员在保持原有类特性的基础上进程扩展,增加功能,这样产生的新的类叫做派生类。简单来说继承是类设计层次的复用。

#include <iostream>
using namespace std;
class Person
{
public:
	void Print()
	{
		cout << "name:" << _name << endl;
		cout << "age:" << _age << endl;
	}
protected:
	string _name = "peter"; // 姓名
	int _age = 18; // 年龄
};
class Student : public Person
{
protected:
	int _stuid; // 学号
};

class Teacher : public Person
{
protected:
	int _jobid; // 工号
};

int main()
{
	Student s;
	Teacher t;
	s.Print();
	t.Print();
	return 0;
}

通过观察,我们看到了Student类和Teacher类都复用了Person类。此时,这里的Student类和Teacher类都是派生类,也可以叫做子类,而Person类是基类,也可以叫做父类。

1.2 定义

1.2.1 定义格式

class 派生类名 : 继承方式 基类名 {}; 比如:class student : public Person{};

1.2.2 继承基类成员访问方式的变化

  1. 基类private成员在派生类中无论以什么方式继承都是不可见的。这里的不可见是指基类的私有成员还是被继承到了派生类对象中,但是语法上限制派生类对象不管在类里面还是类外面都不能去访问它。
  2. 基类private成员在派生类中是不能被访问,如果基类成员不想在类外直接被访问,但需要在派生类中能访问,就定义为protected。可以看出保护成员限定符是因继承才出现的。
  3. 基类的其他成员在子类的访问方式 == Min(成员在基类的访问限定符,继承方式),public > protected > private。
  4. 使用关键字class时默认的继承方式是private,使用struct时默认的继承方式是public,不过最好显示的写出继承方式。在实际运用中一般使用都是public继承,几乎很少使用protetced/private继承,也不提倡使用protetced/private继承,因为protetced/private继承下来的成员都只能在派生类的类里面使用,实际中扩展维护性不强。

2. 基类和派生类对象赋值转换

派生类对象 可以赋值给 基类的对象 / 基类的指针 / 基类的引用。这里有个形象的说法叫切片或者切割。寓意把派生类中父类那部分切来赋值过去。基类对象不能赋值给派生类对象。

double d = 1.1;
int i = d; //这里会产生临时变量:引用赋值就必须带上const
const int& ri = d;
class Person
{
protected:
	string _name; // 姓名
	string _sex; // 性别
	int _age; // 年龄
};

class Student : public Person
{
public:
	int _id; // 学号
};

int main()
{
	Person p;
	Student s;
	p = s;
    
    Person& rp = s;
    
    Person* pp = &s;
	return 0;
}

这里派生类直接赋值给基类,按常理来说也会有中间变量的产生,但是这里并不是,这里是天然支持的,Student类实例化后继承Person类的成员,但是Student中有特有的成员:

理解: Person& rp = s;

理解:Person* pp = &s;

3. 继承中的作用域

  1. 在继承体系中基类和派生类都有独立的作用域。
class Person
{
protected:
	string _name = "小李子"; // 姓名
	int _num = 111; // 身份证号
};
class Student : public Person
{
public:
	void Print()
	{
		cout << " 姓名:" << _name << endl; //输出小李子
		cout << " 身份证号:" << Person::_num << endl; //输出111
		cout << " 学号:" << _num << endl; //输出999
	}
protected:
	int _num = 999; // 学号
};

int main()
{
	Student s;
	s.Print();
	return 0;
}
  1. 子类和父类中有同名成员,子类成员将屏蔽父类对同名成员的直接访问,这种情况叫隐藏,也叫重定义。(在子类成员函数中,可以使用 基类::基类成员 显示访问)。 需要注意的是如果是成员函数的隐藏,只需要函数名相同就构成隐藏。
class A
{
public:
	void fun()
	{
		cout << "func()" << endl;
	}
};
class B : public A
{
public:
	void fun(int i)
	{
		A::fun();
		cout << "func(int i)->" << i << endl;
	}
};
int main()
{
	B b;
	b.fun(10);
	return 0;
}

上面两个fun并不是构成函数重载,函数重载的前提是必须在同一个作用域中,所以这里是隐藏关系。

  1. 注意在实际中在继承体系里面最好不要定义同名的成员。

4. 派生类的默认成员函数

  1. 派生类的构造函数必须调用基类的构造函数初始化基类的那一部分成员。如果基类没有默认的构造函数,则必须在派生类构造函数的初始化列表阶段显示调用。
  2. 派生类的拷贝构造函数必须调用基类的拷贝构造完成基类的拷贝初始化。
  3. 派生类的operator=必须要调用基类的operator=完成基类的复制。
  4. 派生类的析构函数会在被调用完成后自动调用基类的析构函数清理基类成员。因为这样才能保证派生类对象先清理派生类成员再清理基类成员的顺序。
  5. 派生类对象初始化先调用基类构造再调派生类构造。
  6. 派生类对象析构清理先调用派生类析构再调基类的析构。
class Person
{
public:
	Person(const char* name = "peter")
		: _name(name)
	{
		cout << "Person()" << endl;
	}
	Person(const Person& p)
		: _name(p._name)
	{
		cout << "Person(const Person& p)" << endl;
	}
	Person& operator=(const Person& p)
	{
		cout << "Person operator=(const Person& p)" << endl;
		if (this != &p)
			_name = p._name;
		return *this;
	}
	~Person()
	{
		cout << "~Person()" << endl;
	}
protected:
	string _name; // 姓名
};
class Student : public Person
{
public:
	Student(const char* name, int num)
		: Person(name)
		, _num(num)
	{
		cout << "Student()" << endl;
	}
	Student(const Student& s)
		: Person(s)
		, _num(s._num)
	{
		cout << "Student(const Student& s)" << endl;
	}
	Student& operator = (const Student& s)
	{
		cout << "Student& operator= (const Student& s)" << endl;
		if (this != &s)
		{
			Person::operator =(s); //Person::operator=() --> 防止隐藏
			_num = s._num;
		}
		return *this;
	}
	~Student()
	{
		cout << "~Student()" << endl;
	}
protected:
	int _num; //学号
};

int main()
{
	Student s1("jack", 18); 
	Student s2(s1);
	Student s3("rose", 17);
	s1 = s3;
	return 0;
}

5. 继承与友元

友元关系不能继承,也就是说基类友元不能访问子类私有和保护成员

class Student;
class Person
{
public:
	friend void Display(const Person& p, const Student& s); //无法访问Student类中的_stuNum
protected:
	string _name; // 姓名
};

class Student : public Person
{
protected:
	int _stuNum; // 学号
};
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;
}

6. 继承与静态成员

基类定义了static静态成员,则整个继承体系里面只有一个这样的成员。无论派生出多少个子类,都只有一个static成员实例 。因为static修饰的在静态区是共享的。

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()
{
	Student s1;
	Student s2;
	Student s3;
	Graduate s4;
	cout << " 人数 :" << Person::_count << endl; //4
	cout << " 人数 :" << Student::_count << endl; //4
	cout << " 人数 :" << Graduate::_count << endl; //4
	Student::_count = 0;
	cout << " 人数 :" << Person::_count << endl; //0
	return 0;
}

实现一个不能被继承的类:

class A
{
private:
	A()
	{}
};

class B : public A
{

};

int main() //把基类的构造方法封装
{
	B b;

	return 0;
}

7. 复杂的菱形继承及菱形虚拟继承

  1. 单继承:一个子类只有一个直接父类时称这个继承关系为单继承
  1. 多继承:一个子类有两个或以上直接父类时称这个继承关系为多继承
  1. 菱形继承

菱形继承造成两个问题:数据冗余(空间浪费)和二义性

class Person
{
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; // 主修课程
};

int main()
{
	// 这样会有二义性无法明确知道访问的是哪一个
	Assistant a;
	a._name = "peter";
	// 需要显示指定访问哪个父类的成员可以解决二义性问题,但是数据冗余问题无法解决
	a.Student::_name = "xxx";
	a.Teacher::_name = "yyy"; //这个用法生活中就不对,_name生活中只有身份证上的名字
	return 0;
}

怎么来解决数据冗余问题?

class Person
{
public:
	string _name; // 姓名
};
class Student : virtual public Person
{
protected:
	int _num; //学号
};
class Teacher : virtual public Person
{
protected:
	int _id; // 职工编号
};
class Assistant : public Student, public Teacher
{
protected:
	string _majorCourse; // 主修课程
};

int main()
{
	Assistant a;
	a._name = "peter";
	a.Student::_name = "baulo";
	a.Teacher::_name = "manco";
	cout << a._name << endl; //最终输出manco
	return 0;
}

为什么这里的virtual关键字可以解决数据冗余问题?

class A{public:int _a;};
class B : virtual public A{public: int _b;};
class C : virtual public A{public: int _c;};
class D : public B, public C{public: int _d;};
int main()
{
	D d;
	d.B::_a = 1;
	d.C::_a = 2;
	d._b = 3;
	d._c = 4;
	d._d = 5;
	return 0;
}

关系

非虚继承

B和C类中都有_a,此时出现了二义性和数据冗余的问题。D这个类中只有 _d,其他的都是继承下来的。

虚继承

这里是通过偏移量来解决了二义性和数据冗余的问题。再来看B类中:

上述中的假设B* pB = &b; pB->_a;其实和上面D类中的图中的B* pB = &d; pB-> _a是一样的,都是通过偏移量找到这里的 _a。验证:

这里观察仔细的可以发现上面我们使用偏移量的方式导致使用空间更大了,这里当有很多对象的时候,这里使用虚继承的话类中的数据也只有一份,相比非虚继承就会空间使用很少。

注意:实际运用中不要出现菱形继承的情况

8. 继承和组合

//组合
class A
{};

class B
{
private:
	A _aa;
};
//继承
class C
{};
class D : public C
{};

public继承是一种is-a的关系。也就是说每个派生类对象都是一个基类对象。

组合是一种has-a的关系。假设B组合了A,每个B对象中都有一个A对象。

继承相比组合耦合度更高。相比继承更应该使用组合,但是要按照场景来说。

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

脚踏车(crush)

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

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

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

打赏作者

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

抵扣说明:

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

余额充值