C++继承和派生

本文详细讨论了C++中的继承方式和权限问题,包括继承的写法、继承实质、权限显示以及私有继承的特点。同时,介绍了继承中的构造函数,强调子类构造函数必须调用父类构造函数。还涉及了同名问题的处理,如数据成员和成员函数的同名冲突,并提到了解决菱形继承问题的虚继承。最后,总结了构造顺序,无论单继承还是多继承,都是先构造父类再构造子类。
摘要由CSDN通过智能技术生成

继承方式与权限问题

1.继承的写法

2.继承(子类中没有产生新的属性或者行为)派生(子类中有新的属性和行为产生)

3.继承实质与权限问题

(1)实质:父类的数据和成员子类有一份

(2)权限问题:继承方式只会增强父类属性在子类中的权限显示(父类的私有属性不能直接访问)

4.私有继承再继承无意义

5.子类构造无参函数,父类必须要有无参的构造函数

#include <iostream>
#include <string>
using namespace std;
class parent//父类    基类
{
public:
	void print()
	{
		cout << name << "\t" << num << endl;
	}
protected:
	int num;
	string name;
private:
	string name1;//可以被继承,但不可被直接访问,可以间接访问
};
//子类   派生
class son1 :public parent//公有继承
{
public:
	void printS()
	{
		cout << name << "\t" << num << endl;
		print();
	}
protected:
};
class son2 :protected parent//保护继承
{
public:
protected:
};
class son3 :private parent//私有继承
{
public:
protected:
};

继承中的构造函数

1.父类的属性通过父类的构造函数初始化

2.子类中的构造函数,必须调用父类构造函数,必须采用初始化参数列表的方式

3.单继承:只有一个父类

4.多继承:有两个及以上的父类

5.无论被继承多少次,类的属性都在,所以继承一般不会有很多层,会导致臃肿

#include <iostream>
#include <string>
using namespace std;
//单继承
class parent
{
public:
	parent(string fsurname, string fname) :fsurname(fsurname), fname(fname){}
protected:
	string fsurname;//父姓
	string fname;//父名
};
class son :public parent
{
public:
	son(string fsurname, string fname, string sname) :parent(fsurname, fname), sname(sname)
	{
		this->ssurname = fsurname;
	}
	void print()
	{
		cout << "父类" << fsurname + fname << endl;
		cout << "子类" << ssurname + sname << endl;
	}
protected:
	string ssurname;//子姓
	string sname;//子名
	//string fname;继承属性
	//string sname;
};
//多继承
class parent1
{
public:
	parent1(string name1) :name1(name1){}
protected:
	string name1;
};
class parent2
{
public:
	parent2(string name2) :name2(name2){}
protected:
	string name2;
};
class son1 :public parent1, public parent2
{
public:
	son1(string name1, string name2, string sname) :parent1(name1), parent2(name2), sname(sname){}
protected:
	string sname;
};
int main()
{
	son a("姓", "名1", "名2");
	a.print();
	son1 a1("1", "2", "3");
	return 0;
}

继承中的同名问题

1.数据成员同名

2.成员函数同名

3.无特殊说明,采用就近原则

#include <iostream>
#include <string>
using namespace std;
class parent
{
public:
	parent(string name) :name(name){}
	void print()
	{
		cout << name << endl;
	}
protected:
	string name;
};
class son :public parent
{
public:
	son(string name) :parent("父类"), name(name){}
	void print()
	{
		cout << name << endl;
		cout << parent::name <<"\t"<< name<<endl;//类名限定
		parent::print();
	}
protected:
	string name;
};
int main()
{
	son a("子类");
	a.print();
	parent A("父类");
	A.print();
	son* ps = new son("子类");
	ps->print();//子类
	parent* pp = new parent("父类");
	pp->print();//父类
	//非正常指针
	parent* pp1 = new son("子类");//允许子类对象初始化父类指针
	pp1->print();//父类
	//在没有virtual情况下看指针类型    在有virtual情况下看赋值对象
	//父类对象初始化子类指针,不安全不被允许
    delete ps;
    ps=nullptr;
    delete pp;
    pp=nullptr;
    delete pp1;
    pp1=nullptr;
	return 0;
}

4.虚继承(菱形继承)

#include <iostream>
using namespace std;
class A
{
public:
	A(int a) :a(a){}
protected:
	int a;
};
class B:virtual public A
{
public:
	B(int a, int b) :A(a), b(b){}
protected:
	int b;
};
class C:virtual public A
{
public:
	C(int a, int c) :A(a), c(c){}
	void printc()
	{
		cout << a << endl;
	}
protected:
	int c;
};
class D :public B, public C
{
public:
	D() :B(1, 2), C(2, 3), A(999){}
	void print()
	{
		cout << a << endl;//999
		cout << B::a << endl;//999
		cout << C::a << endl;//999
		C::printc();//999
	}
protected:
};
int main()
{
	D d;
	d.print();
	return 0;
}

构造顺序问题

注:与初始化参数列表无关

1.单继承中构造顺序问题(先构造父类,再构造子类)

2.多继承中构造顺序问题(与继承顺序一致)

include <iostream>
using namespace std;
class A
{
public:
	A(){ cout << "A" ; }
	~A(){ cout << "A" ; }
};
class B:public A
{
public:
	B(){ cout << "B" ; }
	~B(){ cout << "B" ; }
};
class C
{
public:
	C(){ cout << "C"  ; }
	~C(){ cout << "C" ; }
};
class D
{
public:
	D(){ cout << "D" ; }
	~D(){ cout << "D" ; }
};
class F:public A,public D,public C
{
public:
	F(){ cout << "F" ; }
	~F(){ cout << "F" ; }
};
int main()
{
	{
		B b;
	}
	cout << endl;
	F f;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值