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; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。