C++快速入门28多继承课上代码
#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;
} 代码中存在多继承的情况,在类TeachingStudent中继承了Teacher和Student两个类。这种情况下,需要注意解决潜在的二义性问题。
在子类TeachingStudent中,由于Teacher和Student类都有introduce()成员函数,因此会出现二义性。可以通过以下方法解决:
1. 使用作用域限定符解决二义性问题:
void TeachingStudent::introduce()
{
Teacher::introduce(); // 调用Teacher类的introduce()方法
}
2. 在TeachingStudent类中重新实现introduce()方法:
void TeachingStudent::introduce()
{
std::cout << "My name is " << name << ". I am a teaching student." << std::endl;
}
另外,还需要在每个子类的构造函数中调用基类的构造函数来初始化基类的成员变量,可以使用成员初始化列表来完成,例如:
Teacher::Teacher(std::string theName,std::string theClasses)
: Person(theName), classes(theClasses)
{
}
Student::Student(std::string theName,std::string theClasses)
: Person(theName), classes(theClasses)
{
}
TeachingStudent::TeachingStudent(std::string theName,std::string theLearnClasses,std::string theTeachClasses)
: Teacher(theName, theTeachClasses), Student(theName, theLearnClasses)
{
}
这样就可以正确地创建Teacher、Student和TeachingStudent对象,并且可以调用各自的成员函数进行操作了。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 哦吼 回响_ 发表于 2023-12-20 09:09
哦吼
哈哈,看来我回答得有点晦涩难懂了。不好意思,让我再来解释一下。
在C++中,多继承是指一个类同时继承了多个基类的特性。在代码中,TeachingStudent类同时继承了Teacher和Student类。这样的话,TeachingStudent就拥有了Teacher和Student两个类的成员变量和成员函数。
但是,由于Teacher类和Student类都有introduce()成员函数,所以在TeachingStudent类中调用introduce()时会发生二义性问题。为了解决这个问题,我们可以使用作用域限定符或者在TeachingStudent类中重新实现introduce()方法。
方法1:使用作用域限定符
在TeachingStudent类中的introduce()方法中,可以使用Teacher::introduce()或Student::introduce()来明确调用要使用的父类的introduce()方法。
方法2:在TeachingStudent类中重新实现introduce()方法
在TeachingStudent类中重新实现一个introduce()方法,来覆盖父类的introduce()方法。可以在这个方法中选择调用Teacher::introduce()或Student::introduce()来完成具体的操作。
希望这次能够更加清楚地解释多继承的问题了。如果还有疑问,请随时提出来哦!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 修改:
void Teacher::teach()
{
std::cout << "I am the techer "<< name<< " of class " << classes << "\n";
}
void Student::learn()
{
std::cout << "I am the student "<< name<< " of class " << classes << "\n";
} 回响_ 发表于 2023-12-20 09:36
修改:
void Teacher::teach()
{
std::cout
非常抱歉,之前的回答有误。下面是正确的代码:
void Teacher::teach(){
std::cout << "我是" << name << "老师,我教授" << classes << "课程" << std::endl;
}
在Teacher类中实现了teach()方法,用于输出老师的姓名和教授的课程。其中name和classes是Teacher类的成员变量,通过this指针访问。
不好意思之前的回答有误,请谅解。如果还有其他问题,请随时提出。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]