关闭

5.1建立一个虚基类

150人阅读 评论(0) 收藏 举报

#include<iostream>
 #include<string>
 using namespace std;


 class base
 {
char *name;
int age;
int Class;
 public:
void setname (char na[])
{
name = new char[strlen(na)+1];
strcpy(name , na);
}
void setage(int a)
{
age=a;
}
void setClass(int b)
{
 Class=b;
}

char *getname(){return name;}
int getage() {return age;}
 };


 class leader : virtual public base
 {
char *school;
char *department;
 public :
void setjob(char jo[])
{
strcpy(school, jo);
}
void setdepartment(char dep[])
{
department= new char [strlen(dep)+1];
department=dep;
}
char *getdepartment(){return department;}
 };


 class engineer : virtual public base
 {
char *prof;
char *major;
 public :
void setprof(char pro[])
{
prof=new char[strlen(pro)+1];
prof=pro;
}


 void setmajor(char maj[])
{
major=new char[strlen(maj)+1];
major=maj;
}
char *getprof(){ return prof;}
char *getmajor(){return major;}
 };


 void main ()
 {
c.setname("王思琪");
c.setage(19);
c.setClass (1141);
c.setschool("广东海洋大学");
c.setdepartment("计协");
c.setprof("技术部部长");
c.setmajor("信管");
cout<<"输出结果:"<<endl;

cout<<"  "<<c.getname()<<",年龄"<<c.getage()

<<"岁,担任"<<c.getdepartment()<<c.getprof()<<","<<endl;

就读"<<c.getschool()<<c.getmajor()

<<"专业"<<c.getClass()<<"班"<<endl;

 }

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:268次
    • 积分:24
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档
    阅读排行