64,637
社区成员
发帖
与我相关
我的任务
分享
//4、定义一个基类Person,从它派生出类Student和类Worker,再由类Student和Worker共同派生出WorStu。
//类Person的属性有姓名、年龄、性别,操作有设置姓名、年龄、性别和显示信息等。类Student的属性增加
//系别、专业和学号属性及相应的操作。类Worker新增部门和工作属性及相应操作。在主函数中声明一个
//WorStu最远派生类的对象,显示相关信息。
#include <iostream>
using namespace std;
class Person
{
public:
Person(){}
~Person(){}
void set_name(string);
void set_age(int);
void set_sex(string);
string show_name(){return name ;}
int show_age(){return age ;}
string show_sex(){return sex ;}
void display();
protected:
string name ;
int age ;
string sex ;
};
class Student:virtual public Person
{
public:
Student(){}
~Student(){}
void set_department(string);
void set_major(string) ;
void set_id(long) ;
string show_department(){return department ;}
string show_major(){return major ;}
long show_id(){return id ;}
protected:
string department ;
string major ;
long id ;
};
class Worker:virtual public Person
{
public:
Worker(){}
~Worker(){}
void set_section(string) ;
void set_job(string);
string show_section(){return section ;}
string show_job(){return job ;}
protected:
string section ;
string job ;
};
class WoeStu:public Student ,public Worker
{
public:
void show() ;
};
void Person::set_name(string a)
{
name = a ;
}
void Person::set_age(int a)
{
age = a ;
}
void Person::set_sex(string a)
{
sex = a ;
}
void Person::display()
{
cout<<"name:\n";
show_name();
cout<<"age:\n";
show_age();
cout<<"sex:\n";
show_sex();
}
void Student::set_department(string a )
{
department = a ;
}
void Student::set_major(string a)
{
major = a ;
}
void Student::set_id(long a)
{
id = a ;
}
void Worker::set_section(string a)
{
section = a ;
}
void Worker::set_job(string a )
{
job = a ;
}
void WoeStu::show()
{
display();
cout<<"department:\n";
show_department();
cout<<"major:\n";
show_major();
cout<<"id:\n";
show_id();
cout<<"section:\n";
show_section();
cout<<"job:\n";
show_job();
}
int main()
{
WoeStu WS ;
WS.set_name("Tom");
WS.set_age(25);
WS.set_sex("man");
WS.set_department("computer science ");
WS.set_major("Computer Science and Technology");
WS.set_id(11320088);
WS.set_section("export");
WS.set_job("sales");
WS.show();
return 0 ;
}
void Person::display()
{
cout<<"name:\n";
cout<<show_name();
cout<<"age:\n";
cout<<show_age();
cout<<"sex:\n";
cout<<show_sex();
}
void WoeStu::show()
{
display();
cout<<"department:\n";
cout<<show_department();
cout<<"major:\n";
cout<<show_major();
cout<<"id:\n";
cout<<show_id();
cout<<"section:\n";
cout<<show_section();
cout<<"job:\n";
cout<<show_job();
}
#include<string>
就没问题了。我看了半天呢。。。昨晚看也不知道为什么。。。哎。。。只是少了头文件。。。大家以后都要注意啊!
void WoeStu::show()
{
display();
cout<<"department:\n"<<show_department();
cout<<"major:\n"<<show_major();
cout<<"id:\n"<<show_id();
cout<<"section:\n"<<show_section();
cout<<"job:\n"<<show_job();
}