实验3 继承和派生

#include<iostream>


using namespace std;

class A
{
public:
	A() { cout << "A构" << endl; };
	~A() { cout << "A析" << endl; };
};

class B:public A
{
public:
	B() { cout << "B构" << endl; };
	~B() { cout << "B析" << endl; };
};
class C :public A
{
public:
	C() { cout << "C构" << endl; };
	~C() { cout << "C析" << endl; };
};
class D :public B, public C
{
public:
	D() { cout << "D构" << endl; };
	~D() { cout << "D析" << endl; };
};

int main()
{

	B b;
	C c;
	D d;

}
#include<iostream>


using namespace std;

class Student
{
public:
	Student(char *number) { Number = number; };
	void show() { cout << "code:" << Number << endl; }
private:
	char * Number;
};
class GStudent :public Student
{
private:
	char *Specialty;
public:
	GStudent(char *number, char *specialty) :Student(number) { Specialty = specialty; };
	void show()
	{
		Student::show();
		cout << "学生专业:" << Specialty << endl;
	}
};
class Teacher
{
private:
	char *T_Specialty;
	char *T_Number;
public:
	Teacher(char *t_number, char *t_specialty)
	{
		T_Specialty = t_specialty;
		T_Number = t_number;
	}
	void show() { cout << "教师号:" << T_Number << ",专业" << T_Specialty; }
};
class T_GStudent:public GStudent,public Teacher
{
public:
	T_GStudent(char *number,char *specialty,char *t_number,char *t_specialty):GStudent(number,specialty),Teacher(t_number,t_specialty){}
	void show()
	{
		cout << "在职研究生信息:" << endl;
		GStudent::show();
		Teacher::show();
	}


};

int main()
{
	GStudent gs("1805004425", "通信工程");
	Teacher t("1690089","计算机");
	gs.show();
	t.show();
	T_GStudent tg("1805004425", "通信工程", "1690089", "计算机");
	tg.show();
	

}
#include<iostream>


using namespace std;

class B1
{
public:
	B1(int i) { cout << "constructing B1" << i << endl; };
	~B1() { cout << "destructing B1" << endl; };

};
class B2
{
public:
	B2() { cout << "constructing B2"  << endl; };
	~B2() { cout << "destructing B2" << endl; };

};
class C :public B2, virtual public B1
{
public:
	C(int a, int b,int c):B1(a),memberB1(b),j(c){}
private:
	int j;
	B1 memberB1;
	B2 memberB2;
};

int main()
{
	C obj(1, 2, 3);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值