继承 多态《基类和派生类的赋值转换》《继承中的作用域》《派生类的默认成员函数》《继承与友元》《菱形继承与菱形虚继承》

继承定义:
1.定义格式

class  student::public  person
{
 public:
      int   _stuid;  //学号
      int   _major;  //专业  
}

2.继承基类成员访问方式的变化
在这里插入图片描述
在实际运用中一般使用都是public继承,几乎很少使用protetced/private继承,也不提倡使用protetced/private继承

3.基类和派生类对象的赋值转换
派生类对象 可以赋值给 基类的对象 / 基类的指针 / 基类的引用。这里有个形象的说法叫切片或者切割。寓意把派生类中父类那部分切来赋值过去。
基类对象不能赋值给派生类对象
基类的指针可以通过强制类型转换赋值给派生类的指针。但是必须是基类的指针是指向派生类对象时才是安全的

class Person
{
protected :
 string _name; // 姓名
 string _sex; // 性别
 int _age; // 年龄
};
class Student : public Person
{
public :
 int _No ; // 学号
};
void Test ()
{
 Student sobj ;
 // 1.子类对象可以赋值给父类对象/指针/引用
 Person pobj = sobj ;
 Person* pp = &sobj;
 Person& rp = sobj;

 //2.基类对象不能赋值给派生类对象
 sobj = pobj;

 // 3.基类的指针可以通过强制类型转换赋值给派生类的指针
 pp = &sobj
 Student* ps1 = (Student*)pp; // 这种情况转换时可以的。
 ps1->_No = 10;

 pp = &pobj;
 Student* ps2 = (Student*)pp; // 这种情况转换时虽然可以,但是会存在越界访问的问题
 ps2->_No = 10;

4.继承中的作用域
子类成员将屏蔽父类对同名成员的直接访问,这种情况叫隐藏,也叫重定义。(在子类成员函数中,可以使用 基类::基类成员 显示访问)同名隐藏

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;
 }
};
void Test()
{
 B b;
 b.fun(10);
};

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

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);
				_num = s._num;
		}
		return *this;
	}
	~student()
	{
		cout << "~student()" << endl;
	}

protected:
	int _num;      //学号
};

void Test()
{
	student s1("jack", 27);
	student s2(s1);
	student s3("lisa", 45);
	s1 = s3;
}

int main()
{
	Test();
	system("pause");
	return 0;
}

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

class Student;
class Person
{
public:
 friend void Display(const Person& p, const Student& s);
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;
}
void main()
{
 Person p;
 Student s;
 Display(p, s);
}

7.继承与静态成员
基类定义了static静态成员,则整个继承体系里面只有一个这样的成员。无论派生出多少个子类,都只有一个static成员实例 。

8.菱形继承与菱形虚拟继承
菱形继承的问题:存在二义性的问题。

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 ; // 主修课程
};
void Test ()
{
 // 这样会有二义性无法明确知道访问的是哪一个
 Assistant a ;
 a._name = "peter";

 // 需要显示指定访问哪个父类的成员可以解决二义性问题,但是数据冗余问题无法解决
 a.Student::_name = "xxx";
 a.Teacher::_name = "yyy";
}

虚拟继承可以解决菱形继承的二义性和数据冗余的问题,在student和teacher的继承person时使用虚拟继承,需要注意的是,虚拟继承不要在其他地方使用

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 ; // 主修课程
};
void Test ()
{
 Assistant a ;
 a._name = "peter";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值