|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <iostream>
#include <string>
/* 基类 --------------------------------------------------------------------*/
class Person
{
public:
Person(std::string theName);
void introduce();
protected:
std::string name;
};
/* 子类 --------------------------------------------------------------------*/
//Teacher
class Teacher: public Person
{
public:
Teacher(std::string theName,std::string theClasses);
void introduce();
void teach();
protected:
std::string classes;
};
//Student
class Student: public Person
{
public:
Student(std::string theName,std::string theClasses);
void introduce();
void learn();
protected:
std::string classes;
};
//TeachingStudent
class TeachingStudent : public Teacher, public Student
{
public:
TeachingStudent(std::string theName,std::string theLearnClasses,std::string theTeachClasses);
void introduce();
};
/* 函数 --------------------------------------------------------------------*/
//Person
Person::Person(std::string theName)
{
name = theName;
}
void Person::introduce()
{
std::cout << "My name is " << name << "\n";
}
//Teacher
Teacher::Teacher(std::string theName,std::string theClasses):Person(theName) //:Person(theName)--告诉编译器, Teacher构造前先调用 Person构造函数,并传递给其一个参数
{
classes = theClasses;
}
void Teacher::introduce()
{
std::cout << "I am " << name << "\n";
}
void Teacher::teach()
{
std::cout << "I am the techer of class " << classes << "\n";
}
//Student
Student::Student(std::string theName,std::string theClasses):Person(theName)
{
classes = theClasses;
}
void Student::introduce()
{
std::cout << "I am " << name << "\n";
}
void Student::learn()
{
std::cout << "I am the student of class " << classes << "\n";
}
//TeachingStudent
TeachingStudent::TeachingStudent(std::string theName,std::string theLearnClasses,std::string theTeachClasses):Student(theName,theLearnClasses),Teacher(theName,theTeachClasses)
{
}
void TeachingStudent::introduce()
{
std::cout << "I am " << Student::name << "\n";
}
int main()
{
Teacher bob("Bob","6班");
Student jack("Jack","5班");
TeachingStudent james("James","7班","8班");
bob.introduce();
bob.teach();
jack.introduce();
jack.learn();
james.introduce();
james.learn();
james.teach();
return 0;
} |
|