我是吴京 2021-12-12 17:45 采纳率: 67.4%
浏览 32
已结题

关于c++菱形多继承的一个问题

关于c++菱形多继承的一个问题

```c++
#include <iostream>
#include <string>
//实现多继承
class Person
{
public:
    Person(std::string);
    void introduce();
protected:
    std::string name;
};
Person::Person(std::string thename)
{
    thename = name;
}
void Person::introduce()
{
    std::cout << "我的名字叫:" << this->name << std::endl;
}
class Teacher :public Person
{
public:
    Teacher(std::string, std::string);
    void teach();
protected:
    std::string TeachClass;
};
Teacher::Teacher(std::string thename, std::string TeachClass):Person(thename)
{
    this->TeachClass = TeachClass;
}
void Teacher::teach()
{
    std::cout << "我教的课程叫:" << this->TeachClass << std::endl;
}
class Student :public Person
{
public:
    Student(std::string, std::string);
protected:
    std::string StudyClass;
};
Student::Student(std::string thename, std::string StudyClass):Person(thename)
{
    this->StudyClass = StudyClass;
}
class TeachingStudent:public Student, public Teacher
{
public:
    TeachingStudent(std::string, std::string, std::string);
    void print();
};
TeachingStudent::TeachingStudent(std::string thename, std::string StudyClass, std::string TeachClass) :Student(thename, StudyClass), Teacher(thename, TeachClass)
{
    
}
void TeachingStudent::print()
{
    std::cout << Student::name << "在" << this->StudyClass << "学习并教" << this->TeachClass << std::endl;//此处的Student::name为什么打印出来是空白
}
int main()
{
    TeachingStudent a("小明", "机电6班", "机电5班");
    a.print();
    return 0;
}

```

  • 写回答

3条回答 默认 最新

  • 书山客 2021-12-12 17:51
    关注

    在构造函数中要记录数据

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月20日
  • 已采纳回答 12月12日
  • 创建了问题 12月12日

悬赏问题

  • ¥18 光催化第一性原理计算析氢效率STH怎么计算
  • ¥100 Mac 版foxmail 收邮件问题
  • ¥15 QWebEngineView
  • ¥15 .net core 同时编辑怎么防止数据串了
  • ¥20 微信小程序播放直播流
  • ¥15 关于迷宫自走单片机循迹小车的知识
  • ¥15 python使用selenium工具爬取网站的问题
  • ¥15 visual studio中c语言用ODBC链接SQL SERVER
  • ¥15 关于#python#的问题:如何通过pywinauto获取到图中“窗格”内部的内容
  • ¥15 visionMaster4.3.0 与QT 的二次开发异常