C++继承

博客围绕C++继承展开,虽暂无具体内容,但C++继承是重要的面向对象编程概念,可实现代码复用和多态性等特性,在软件开发中应用广泛。
#include<iostream>
using namespace std;

#if 0
class Person
{
public:
	void Print()
	{
		cout << "name:" << _name << endl;
		cout << "age:" << _age << endl;
	}

protected:
	string _name = "peter";
private:
	int _age = 18;
};

class Student : public Person
//class Student : protected Person
//class Student : private Person
{
protected:
	int _stuid;
};

class Teacher : public Person
{
protected:
	int _jobid;
};

int main()
{
	Student s;
	//Teacher t;
	s.Print();
	//t.Print();

	return 0;
}
#endif

#if 0
class Person
{
protected:
	string _name;
	string _sex;
	int _age;
};

class Student : public Person
{
public:
	int _id;
};

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->_id = 10;

	pp = &pobj; //父类指针指向父类对象
	Student* ps2 = (Student*)pp; //这种情况虽然可以,但会存在越界的问题
	//将一个指向父类的父类指针强转成子类指针赋值给子类指针,则访问的时候会越界
	//(这个指针如果只访问了继承下来的成员则没问题,一旦访问只有子类有的成员则会越界)
	ps2->_id = 15;

}

int main()
{
	Test();
	return 0;
}
#endif


#if 0
//1.在继承体系中基类和派生类都有独立的作用域
//2.子类和父类中有同名的成员,子类成员将屏蔽父类对同名成员的直接访问,这种情况叫
//	隐藏,也叫重定义。(在子类成员函数中,可以使用 基类 :: 基类成员 显示访问)
//3.需要注意的是,如果是成员函数的隐藏,只需要函数名相同就构成隐藏
//4.在实际继承体系中最后不要定义同名成员
class Person
{
protected:
	string _name = "小李子";
	int _num = 1111111;
};

class Student : public Person
{
public:
	void Print()
	{
		cout << "姓名:" << _name << endl;
		cout << "身份证号:" << Person::_num << endl; //显示访问同名成员
		cout << "学号:" << _num << endl;
	}
protected:
	int _num = 100;
};

class A
{
public:
	void fun()
	{
		cout << "fun()" << endl;
	}
};


//B中的fun和A中的fun不是构成重载,因为不是在同意作用域
//B中的fun和A中的fun构成隐藏,成员函数名相同就构成隐藏
class B : public A
{
public:
	void fun(int i)
	{
		A::fun();
		cout << "fun(int i)" << endl;
	}
};

int main()
{
	//Student s1;
	//s1.Print();

	B b;
	b.fun(10);
	
	return 0;
}
#endif


#if 0
//1. 派生类的构造函数必须调用基类的构造函数初始化基类的那一部分成员。
//	 如果基类没有默认的构造函 数,则必须在派生类构造函数的初始化列表阶段显示调用。 
//2. 派生类的拷贝构造函数必须调用基类的拷贝构造完成基类的拷贝初始化。 
//3. 派生类的operator = 必须要调用基类的operator = 完成基类的复制。 
//4. 派生类的析构函数会在被调用完成后自动调用基类的析构函数清理基类成员。
//	 因为这样才能保证派生类 对象先清理派生类成员再清理基类成员的顺序。 
//5. 派生类对象初始化先调用基类构造再调派生类构造。 
//6. 派生类对象析构清理先调用派生类析构再调基类的析构。


class Person
{
public:
	Person(const char* name = "lc")
		:_name(name)
	{
		cout << "Person()父类的构造函数" << endl;
	}

	Person(const Person& p)
		:_name(p._name)
	{
		cout << "Person(cosnt 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 id)
		:Person(name)//调用父类的构造函数
		,_id(id)
	{
		cout << "Student()子类构造函数" << endl;
	}

	Student(const Student& s)
		:Person(s)
		,_id(s._id)
	{
		cout << "Student(const Student& s)子类拷贝构造函数" << endl;
	}

	Student& operator=(const Student& s)
	{
		cout << "Student& operator=(const Student& s)子类的赋值运算符重载" << endl;
		if (this != &s)
		{
			Person :: operator=(s);
			_id = s._id;
		}
		return *this;
	}

	~Student()
	{
		cout << "~Student()子类析构函数" << endl;
		//Person::~Person(); 会自动调用
	}

private:
	int _id;
};

int main()
{
	Student s1("jacks", 20);
	Student s2(s1);
	Student s3("rose", 25);
	s1 = s3;

	return 0;
}
#endif


#if 0
//面试题:实现一个不能被继承的类

//C++98中构造函数私有化,派生类调不到基类的构造函数。则无法继承
class A
{
public:
	static A GetA()
	{
		return A();
	}
private:
	A()	//构造函数
	{}
};

class B:public A
{
public:
	//B()
	//{}    //A()不能访问
};

//C++11中给出了关键字 final禁止继承
class C final
{};

//class D:public C  //不能将final 类作为基类
//{};
#endif


#if 0
//友元关系不能继承,基类友元不能访问子类私有和保护成员
class Student;

class Person
{
public:
	friend void Display(const Person& p, const Student& s);

protected:
	string _name;
};

class Student : public Person
{
protected:
	int _id;
};

void Display(const Person& p, const Student& s)
{
	cout << p._name << endl;
	//cout << s._id << endl;  //不能访问
}

int main()
{
	Person p;
	Student s;
	Display(p, s);
}
#endif 


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

class Person
{
public:
	Person()
	{
		++_count;
	}
public:
	static int _count;
protected:
	string _name;
};

int Person::_count = 0;

class Student : Person
{
protected:
	int _id;
};

class Graduate : public Student
{
protected:
	string _seminarCourse;  //研究科目
};

int main()
{
	Student s1;
	Student s2;
	Student s3;
	Graduate s4;
	cout << "人数" << Person::_count << endl;
	//Student::_count = 0;  //无法访问
	cout << "人数" << Person::_count << endl;

	return 0;
}
#endif 
//单继承和多继承
//1.单继承--一个子类只有一个直接父类时称这个继承关系为单继承
//2.多继承--一个子类有两个或两个以上直接父类称这个继承为多继承
//3菱形继承--是多继承的一种特殊情况(有数据冗余和二义性问题)

//class Person
//{
//public:
//	string _name;
//};
//
//class Student : public Person
//{
//protected:
//	int _id;
//};
//
//class Teacher : public Person
//{
//protected:
//	int _num;
//};
//
//class Assistant : public Student, public Teacher
//{
//protected:
//	string _majorCourse;
//};
//
//void Test()
//{
//	Assistant a;
//
//	//a._name = "lc";  //请求有歧义
//
//	a.Student::_name = "xxx";
//	a.Teacher::_name = "yyy";
//
//	cout << a.Student::_name << endl;
//	cout << a.Teacher::_name << endl;
//}

//class Person
//{
//public:
//	string _name;
//};
//class Student : public Person
//{
//protected:
//	int _id;
//};
//class Teacher : public Person
//{
//protected:
//	int _num;
//};
//class Assistant : public Student, public Teacher
//{
//protected:
//	string _majorCourse;
//};
//
//void Test()
//{
//	Assistant a;
//	a.Student::_name = "lc";
//	a.Teacher::_name = "yx";
//
//	cout << a.Student::_name << endl;
//	cout << a.Teacher::_name << endl;
//}


#if 0
//虚拟继承可以解决菱形继承的二义性和数据冗余的问题。如上面的继承关系,
//在Student和Teacher的继承 Person时使用虚拟继承,即可解决问题。
//需要注意的是,虚拟继承不要在其他地方去使用。(virtual)

class Person
{
public:
	string _name;
};

class Student : virtual public Person
{
protected:
	int _id;
};

class Teacher : virtual public Person
{
protected:
	int _num;
};

class Assistant : public Student, public Teacher
{
protected:
	string _majorCourse;
};

void Test()
{
	Assistant a;

	a._name = "yx";  //请求有歧义
	cout << a._name << endl;

	cout << a.Student::_name << endl;
	cout << a.Teacher::_name << endl;

	a.Student::_name = "xxx";
	a.Teacher::_name = "yyy";

	cout << a.Student::_name << endl;
	cout << a.Teacher::_name << endl;
}



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;
};


//可以分析出D对象中将A放到的了对象组成的最下面,这个A 同时属于B和C,
//那么B和C如何去找到公共的A呢?这里是通过了B和C的两个指针,指向的一张表。
//这两个指 针叫虚基表指针,这两个表叫虚基表。虚基表中存的偏移量。
//通过偏移量可以找到下面的A

int main()
{
	//Test();

	D d;
	//cout << sizeof(d) << endl;

	d.B::_a = 1;
	d.C::_a = 2;
	d._b = 3;
	d._c = 4;
	d._d = 5;

	return 0;
}
#endif

菱形虚继承对象模型

#if 0
//Car1和BWM  
//Car1/BWM和Benz 构成 is-a 的关系
class Car1
{
protected:
	string _colour = "白色";
	string _num = "陕ABIT00";
};
class BMW : public Car1
{
public:
	void Drive() { cout << "好开-操控" << endl; }
};
class Benz : public Car1
{
public:
	void Drive() { cout << "好坐-舒适" << endl; }
};

//Tire和Car2构成 has-a 的关系
class Tire
{
protected:
	string _brand = "Michelin"; //品牌
	size_t size = 17;  //尺寸
};
class Car2
{
protected:
	string _colour = "白色";
	string _num = "陕ABIT00";
	Tire _t;    //轮胎,  (has-a 的关系)嵌套
};
#endif
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值