首页 > 代码库 > C++多重继承的实现
C++多重继承的实现
// // main.cpp // test // // Created by Jacedy on 14-11-6. // Copyright (c) 2014年 Jacedy. All rights reserved. // #include <iostream> using namespace::std; //数据类 class CData { public: char name[20]; CData(char NAME[]) { strcpy(name, NAME); } void print() { printf("姓名:%s\n", name); } }; //教师类 class CTeacher : virtual public CData { public: int sal; CTeacher(char NAME[], int SAL) : CData(name) //要先初始化基类!!! { strcpy(name, NAME); sal = SAL; } void print() { printf("姓名:%s, 工资:%d\n", name, sal); } }; //学生类 class CStudent : virtual public CData { public: int id; CStudent(char NAME[], int ID) : CData(name) { strcpy(name, NAME); id = ID; } void print() { printf("姓名:%s, 学号:%d\n", name , id); } }; //研究生类 class CPostgrad : virtual public CStudent { public: char dn[40]; CPostgrad(char NAME[], int ID, char DN[]) : CStudent(name, id),CData(name) { strcpy(name, NAME); strcpy(dn, DN); } void print() { printf("姓名:%s, 院系:%s\n", name, dn); } }; //教师中的在职研究生类 class CTpost : public CPostgrad, public CTeacher { public: CTpost(char NAME[], int ID, char DN[], int SAL) : CPostgrad(name, id, dn),CTeacher(name, sal),CStudent(name, id),CData(name) { strcpy(name, NAME); sal = SAL; strcpy(dn, DN); } void print() { printf("姓名:%s, 工资:%d, 院系:%s\n", name, sal, dn); } }; int main(void) { CData A("A"); A.print(); CTeacher B("B", 10000); B.print(); CStudent C("C", 12); C.print(); CPostgrad D("D", 18, "计算机学院"); D.print(); CTpost E("E", 22, "计算机学院", 20000); E.print(); return 0; }
C++多重继承的实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。