求高手帮我解释解释这代码有什么功能(是干什么用的)分析一下结构,最好能逐行注释一下呀 谢了。。

#include<iostream.h>
#include<string.h>
class Person
{
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<<name<<endl;
cout<<" TEL:"<<tel;
cout<<" Address:"<<add;
}
~Person()
{
delete[] name;
delete[] tel;
delete[] add;
// cout<<"Destructed class Person!"<<endl;
}
protected:
char*name;
char*tel;
char*add;
};
class Student:virtual public Person
{
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:"<<major<<endl;
}
protected:
char*major;
};
class Staff:virtual public 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
{
Person::Print();
cout<<" Department:"<<dept;
cout<<" Salary:"<<salary<<endl;
}
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: "<<lesson<<endl;
}
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: "<<dept;
cout<<" Lesson: "<<lesson;
cout<<" Salary: "<<salary;
}
};
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<<endl;
}

#include<iostream.h>
#include<string.h>
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<<name<<endl;
cout<<" TEL:"<<tel;
cout<<" Address:"<<add;
}
~Person()//释放空间
{
delete[] name;
delete[] tel;
delete[] add;
// cout<<"Destructed class Person!"<<endl;
}
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:"<<major<<endl;
}
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:"<<dept;
cout<<" Salary:"<<salary<<endl;
}
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: "<<lesson<<endl;
}
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: "<<dept;
cout<<" Lesson: "<<lesson;
cout<<" Salary: "<<salary;
}
};
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<<endl;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-12-05
#include<iostream.h>
#include<string.h>
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<<name<<endl;
cout<<" TEL:"<<tel;
cout<<" Address:"<<add;
}
~Person()//虚构函数
{
delete[] name;
delete[] tel;
delete[] add;
// cout<<"Destructed class Person!"<<endl;
}
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:"<<major<<endl;
}
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:"<<dept;
cout<<" Salary:"<<salary<<endl;
}
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: "<<lesson<<endl;
}
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: "<<dept;
cout<<" Lesson: "<<lesson;
cout<<" Salary: "<<salary;
}
};
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<<endl;
}
这是类继承与派生的应用。
相似回答