#include
#include
class Person
{
public:
Person(const char*n,const char*t,const char*a) //对PERSON对象相关属性附值
{
name=new char[strlen(n)+1];//申请n+1的空间存储姓名
strcpy(name,n);
tel=new char[strlen(t)+1];//申请n+1的空间存储电话
strcpy(tel,t);
add=new char[strlen(a)+1];//申请n+1的空间存储地址
strcpy(add,a);
}
void Print() const//显示某个人的人名,电话,地址
{
cout<
~Person()//释放空间
{
delete[] name;
delete[] tel;
delete[] add;
// cout<<"Destructed class Person!"<
protected:
char*name;
char*tel;
char*add;
};
class Student:virtual public Person//申明student类,虚拟继承 Person类,防止多重继承
{
public:
Student(const char*n,const char*t,const char*a,const char*m):Person(n,t,a)//初始化Student对象,在之前,调用Person构造函数
{
major=new char[strlen(m)+1];
strcpy(major,m);
}
void Print() const//显示Student对象的major属性
{
Person::Print();
cout<<" Major:"<
protected:
char*major;
};
class Staff:virtual public Person//申明Staff类,虚拟继承 Person类,防止多重继承
{
public:
Staff(const char*n,const char*t,const char*a,const char*d,int sal):Person(n,t,a)//初始化
{
dept=new char[strlen(d)+1];
strcpy(dept,d);
salary=sal;
}
void Print() const//显示 dept ,Salary
{
Person::Print();
cout<<" Department:"<
protected:
char*dept;
int salary;
};
class Teacher: public Staff//申明Teacher类
{
public:
Teacher(const char*n,const char*d,const char*t,const char*a,const char*l,int sal)
:Person(n,t,a),Staff(n,t,a,d,sal)
{
lesson=new char[strlen(l)+1];
strcpy(lesson,l);
}
void Print() const
{
Staff::Print();
cout<<" Lesson: "<
protected:
char*lesson;
};
class StudentTeacher:public Student,public Teacher//StudentTeacher类,多重继承Student,Teacher
{
public:
//初始化
StudentTeacher(const char*n,const char *m,const char*d,const char*t,\
const char*a,const char*l,int sal)\
:Person(n,t,a),Student(n,t,a,m),Teacher(n,d,t,a,l,sal){}
void Print() const
{
Student::Print();
cout<<" Department: "<
};
void main()
{
Student stu("Richard","01012345678","EAST_16_1","Software Engineering");//构造Student对像
Staff sta("Volatire","01012345679","WEST_16_2","Management",2000);//构造Staff对像
Teacher te("Churchill","Computer","01012345677","EAST_16_5","C++",2500);//构造Teacher对像
StudentTeacher st("Emerson","Computer Application","Computer","01012345676",\
"EAST_16_7","C++",2200);//构造StudentTeacher对像
cout<<"Student:";
stu.Print();//会先调用People
cout<<"Stsff:";
sta.Print();//同上
cout<<"Teacher:";
te.Print();//同上,然后Staff的Print,最后才是teacher的Print
cout<<"Teacher in studying:";//
st.Print();//原理同上
cout<
#include
#include
class Person//定义people 类
{
public:
Person(const char*n,const char*t,const char*a)//构造函数
{
name=new char[strlen(n)+1];
strcpy(name,n);
tel=new char[strlen(t)+1];
strcpy(tel,t);
add=new char[strlen(a)+1];
strcpy(add,a);
}
void Print() const
{
cout<
~Person()//虚构函数
{
delete[] name;
delete[] tel;
delete[] add;
// cout<<"Destructed class Person!"<
protected:
char*name;
char*tel;
char*add;
};
class Student:virtual public Person//定义学生类,继承people类
{
public:
Student(const char*n,const char*t,const char*a,const char*m):Person(n,t,a)//派生类构造函数
{
major=new char[strlen(m)+1];
strcpy(major,m);
}
void Print() const
{
Person::Print();
cout<<" Major:"<
protected:
char*major;
};
class Staff:virtual public Person//定义职位类,继承people类
{
public:
Staff(const char*n,const char*t,const char*a,const char*d,int sal):Person(n,t,a)//派生类构造函数
{
dept=new char[strlen(d)+1];
strcpy(dept,d);
salary=sal;
}
void Print() const
{
Person::Print();
cout<<" Department:"<
protected:
char*dept;
int salary;
};
class Teacher: public Staff//定义教师类,继承职位类
{
public:
Teacher(const char*n,const char*d,const char*t,const char*a,const char*l,int sal)
:Person(n,t,a),Staff(n,t,a,d,sal)
{
lesson=new char[strlen(l)+1];
strcpy(lesson,l);
}
void Print() const
{
Staff::Print();
cout<<" Lesson: "<
protected:
char*lesson;
};
class StudentTeacher:public Student,public Teacher//多继承
{
public:
StudentTeacher(const char*n,const char *m,const char*d,const char*t,\
const char*a,const char*l,int sal)\
:Person(n,t,a),Student(n,t,a,m),Teacher(n,d,t,a,l,sal){}
void Print() const
{
Student::Print();
cout<<" Department: "<
};
void main()
{
Student stu("Richard","01012345678","EAST_16_1","Software Engineering");//初始化
Staff sta("Volatire","01012345679","WEST_16_2","Management",2000);
Teacher te("Churchill","Computer","01012345677","EAST_16_5","C++",2500);
StudentTeacher st("Emerson","Computer Application","Computer","01012345676",\
"EAST_16_7","C++",2200);
cout<<"Student:";
stu.Print();//函数调用
cout<<"Stsff:";
sta.Print();
cout<<"Teacher:";
te.Print();
cout<<"Teacher in studying:";
st.Print();
cout<
这是类继承与派生的应用。