1.声明一个动物基类Animal,私有整型成员变量年龄age,请定义一个派生类Dog,在其成员函数SetAge(int a)中直接给age赋值,测试下看是否会出问题?如何解决?
#include <iostream>
using namespace std;
class Animal
{
public:
void setAge(int a)
{
age = a;
}
void showAge()
{
cout<<"age = "<<age<<endl;
}
private:
int age;
};
class Dog : public Animal
{
public:
void SetAge(int a)
{
setAge(a);
}
};
int main()
{
int a;
cin>>a;
Dog d;
d.SetAge(a);
d.showAge();
return 0;
}
2. 设计一个单基继承的类层次程序,用Person类派生出Student类,增加属性学号index和年级level。Person类中至少有姓名name、年龄age等数据成员,以及构造函数、输出函数等,其余成员函数根据需要添加。在主函数中进行测试。
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(string na, int a):name(na), age(a){}
string getName() const { return name; }
int getAge() const { return age; }
private:
string name;
int age;
};
class Student : public Person
{
public:
Student(string na, int a, string ind, int le):
Person(na, a), index(ind), level(le){}
void display()
{
cout<<"name : "<<getName()<<endl;
cout<<"age : "<<getAge()<<endl;
cout<<"index : "<<index<<endl;
cout<<"level : "<<level<<endl;
}
private:
string index;
int level;
};
int main()
{
string na, ind;
int a, le;
cin>>na>>a>>ind>>le;
Student s(na, a, ind, le);
s.display();
return 0;
}
3. 定义一个学生类Student和教师类Teacher,学生类有姓名name、学号index等数据成员,教师类有姓名name、工作证号workID、职称title、课程course、周学时hoursPerWeek等数据成员。再定义一个助教类TeachingAssistant,多继承于学生类和教师类,该类可以使用学生类的全部数据成员,以及教师类的课程和周学时数据成员。要求:每个类提供自定义的构造函数和析构函数,并通过同名函数ShowInfo来显示全部数据成员的值。在主函数中进行测试。
#include <iostream>
#include <string>
using namespace std;
class Teacher
{
public:
Teacher(string na, string id, string ttl, string cour, int hours):
name(na), workID(id), title(ttl), course(cour), hoursPerWeek(hours){}
Teacher(string cour, int hours):
course(cour), hoursPerWeek(hours){}
int getHours() const { return hoursPerWeek; }
string getCourse() const { return course; }
virtual void showInfo()
{
cout<<"Teacher's Info : \n";
cout<<"name : "<<name<<endl;
cout<<"workID : "<<workID<<endl;
cout<<"title : "<<title<<endl;
cout<<"course : "<<course<<endl;
cout<<"hoursPerWeek : "<<hoursPerWeek<<endl<<endl;
}
~Teacher(){ cout<<"Teacher DECONSTRUCT"<<endl; }
private:
string name;
string workID;
string title;
string course;
int hoursPerWeek;
};
class Student
{
public:
Student(string sna, string ind):
sname(sna), index(ind){}
string getName() const { return sname;}
string getIndex() const { return index; }
virtual void showInfo()
{
cout<<"Student's Info : \n";
cout<<"name : "<<sname<<endl;
cout<<"index : "<<index<<endl<<endl;
}
~Student(){ cout<<"Student DECONSTRUCT"<<endl; }
private:
string sname;
string index;
};
class TeachingAssistant : public Teacher, public Student
{
public:
TeachingAssistant(string cour, int hours, string sna, string ind):
Teacher(cour, hours), Student(sna, ind){}
void showInfo()
{
cout<<"TeachingAssistant's Info : \n";
cout<<"Teacher's course : "<<getCourse()<<endl;
cout<<"Teacher's hoursPerWeek : "<<getHours()<<endl;
cout<<"Student's name : "<<getName()<<endl;
cout<<"Student's index : "<<getIndex()<<endl<<endl;
}
~TeachingAssistant(){ cout<<"TeachingAssistant DECONSTRUCT"<<endl; }
};
int main()
{
string na, id, ttl, cour, sna, ind;
int hours;
cin>>na>>id>>ttl>>cour>>hours;
cin>>sna>>ind;
Teacher tea(na, id, ttl, cour, hours);
tea.showInfo();
Student s(sna, ind);
s.showInfo();
TeachingAssistant ass(cour, hours, sna, ind);
ass.showInfo();
return 0;
}
4. 声明一个Person,包含姓名name和年龄age等私有数据成员以及相关的成员函数;由它派生出领导类Leader,包含职务position和部门department私有数据成员以及相关的成员函数;再由Person派生出工程师类Engineer,包含职务position和专业speciality私有数据成员以及相关的成员函数;再由Leader和Engineer类派生出主任工程师类Chairman。在主函数中测试各类对象初始化和信息输出,查看是否会出问题?如何解决?
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(){}
Person(string na, int a):name(na), age(a){}
string getName() const { return name; }
int getAge() const { return age; }
private:
string name;
int age;
};
class Leader : virtual public Person
{
public:
Leader(string na, int a, string pos, string dep):
Person(na, a), position(pos), department(dep){}
Leader(string pos, string dep):
position(pos), department(dep){}
string getPosition() const { return position; }
string getDepartment() const { return department; }
virtual void display()
{
cout<<"Leader's Info : \n";
cout<<"name : "<<getName()<<endl;
cout<<"age : "<<getAge()<<endl;
cout<<"position : "<<position<<endl;
cout<<"department : "<<department<<endl<<endl;
}
private:
string position;
string department;
};
class Engineer : virtual public Person
{
public:
Engineer(string na, int a, string pos, string spe):
Person(na, a), position(pos), speciality(spe){}
Engineer(string spe): speciality(spe){}
string getPos() const { return position; }
string getSpeciality() const { return speciality; }
virtual void display()
{
cout<<"Engineer's Info : \n";
cout<<"name : "<<getName()<<endl;
cout<<"age : "<<getAge()<<endl;
cout<<"position : "<<position<<endl;
cout<<"speciality : "<<speciality<<endl<<endl;
}
private:
string position;
string speciality;
};
class Chairman : public Leader, public Engineer
{
public:
Chairman(string na, int a, string pos, string dep, string spe):
Person(na, a), Leader(pos, dep), Engineer(spe){}
void display()
{
cout<<"Chairman's Info : \n";
cout<<"name : "<<getName()<<endl;
cout<<"age : "<<getAge()<<endl;
cout<<"position : "<<getPosition()<<endl;
cout<<"department : "<<getDepartment()<<endl;
cout<<"speciality : "<<getSpeciality()<<endl<<endl;
}
};
int main()
{
string na, pos, dep, spe;
int a;
cin>>na>>a>>pos>>dep>>spe;
Leader l(na, a, pos, dep);
l.display();
Engineer eng(na, a, pos, spe);
eng.display();
Chairman c(na, a, pos, dep, spe);
c.display();
return 0;
}