鱼C论坛

 找回密码
 立即注册
查看: 1572|回复: 3

[技术交流] 关于C++多继承 正在学习C++的同学来分享一下自己的建议、疑惑

[复制链接]
发表于 2015-1-21 16:20:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <iostream>
#include <string>
class Person
{
public:
        Person(std::string theName);
        void introduce();
protected:
        std::string name;
};
class Teacher :public Person
{
public:
        Teacher(std::string theName, std::string theClass);
        void teach();
        void introduce();
protected:
        std::string classes;
};
class Student :public Person
{
public:
        Student(std::string theName, std::string theClass);
        void attendClass();
        void introduce();
protected:
        std::string classes;
};
class TeachingStudent :public Student, public Teacher
{
public:
        TeachingStudent(std::string theName, std::string classTeaching, std::string classAttending);
        void introduce();
};
Person::Person(std::string theName)
{
        name = theName;
}
void Person::introduce()
{
        std::cout << "大家好,我是" << name << "。\n";
}
Teacher::Teacher(std::string theName, std::string theClass) :Person(theName)
{
        classes = theClass;
}
void Teacher::teach()
{
        std::cout << name << "教" << classes << "。\n";
}
void Teacher::introduce()
{
        std::cout << "大家好,我是" << name << ",我教" << classes << "。\n";
}
Student::Student(std::string theName, std::string theClass) :Person(theName)
{
        classes = theClass;
}
void Student::attendClass()
{
        std::cout << name << "加入" << classes << "学习。\n";
}
void Student::introduce()
{
        std::cout << "大家好,我是" << name << ",我在" << classes << "学习。\n";
}
TeachingStudent::TeachingStudent(std::string theName,
        std::string classTeaching, std::string classAttending) :Teacher(theName, classTeaching), Student(theName, classAttending)
{

}
void TeachingStudent::introduce()
{
        std::cout << "大家好,我是" << Student::name << "。我教" << Teacher::classes << ","<<"同时,我在"<<Student::classes<<"学习。\n";
}
int main()
{
        Teacher teacher("天天", "初级语言");
        Student student("小白", "初级语言");
        TeachingStudent teachingstudent("飞鱼", "初级语言", "高级语言");
        teacher.introduce();
        teacher.teach();
        student.introduce();
        student.attendClass();
        teachingstudent.introduce();
        teachingstudent.teach();
        teachingstudent.attendClass();
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-1-21 17:04:25 | 显示全部楼层
MD,整得这么复杂,看了半天才有一点点理解了   蛋疼啊。。。。。。。。。。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-1-21 17:07:35 | 显示全部楼层
introduce是覆盖操作吗?子类覆盖基类?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-1-21 17:30:40 | 显示全部楼层
自己动手丰衣足食  
明白了  
明白的差不多了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-21 12:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表