提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
目录
题目:
1.定义一个人类(Person),包括属性:姓名(name)、性别(sex)、年龄(age);包括方法:吃饭(eat)、睡觉(sleep)、工作(work)、打印个人信息。
2.根据人类,派生一个学生类(Student),增加属性:学校(school)、学号(s_number);方法:工作(学生的工作是学习)、打印个人信息。
根据人类,派生一个工人类(Worker),增加属性:单位,工龄;方法:工作(工人的工作是上班)、打印个人信息。
3.根据学生类,派生一个学生干部类(StudentLeading),增加属性:职务(job);方法:开会(meeting) 、打印个人信息。
创建一个工人对象、一个学生干部对象,设置并输出对象的各项信息,并测试工人对象的工作方法、测试学生干部对象的工作和开会方法等。
代码:
#include<iostream>
#include<string.h>
using namespace std;
class person{
public:
string name,sex;
int age;
person(string na,string s,int a)
{
name=na;sex=s;age=a;
}
void eat();
void sleep();
void work();
void print1();
};
class student:public person{
public:string school;
int number;
student(string na,string s,int a,string sch,int num):person(na,s,a)
{
school=sch;number=num;
}
void work2();
void print2();
};
class worker:public person{
private:
string danwei;
int goling;
public:worker(string na,string s,int a,string da,int gl):person(na,s,a)
{
danwei=da;goling=gl;
}
void work3();
void print3();
};
class studentleading:public student{
private:string job;
public:
studentleading(string na,string s,int a,string sch,int num,string j):student(na,s,a,sch,num)
{
job=j;
}
void meeting();
void work4();
void print4();
};
void person::eat()
{
cout<<"吃饭:食堂吃饭";
}
void person::sleep()
{
cout<<"睡觉:晚睡";
}
void person::work()
{
cout<<"工作:学习";
}
void person::print1()
{
cout<<"姓名:"<<name<<" ";
cout<<"性别:"<<sex<<" ";
cout<<"年龄:"<<age<<endl;
}
void student::work2()
{
cout<<"工作:学习"<<" ";
}
void student::print2()
{
cout<<"姓名:"<<name<<" ";
cout<<"学号:"<<number<<" ";
cout<<"性别:"<<sex<<" ";
cout<<"年龄:"<<age<<"";
cout<<"学校:"<<school<<" ";
}
void worker::work3()
{
cout<<"工作:上班"<<" ";
}
void worker::print3()
{
cout<<"姓名:"<<name<<" ";
cout<<"性别:"<<sex<<" ";
cout<<"年龄:"<<age<<" ";
cout<<"单位:"<<danwei<<" ";
cout<<"工龄:"<<goling<<" ";
}
void studentleading::meeting()
{
cout<<"开会:开班会"<<" ";
}
void studentleading::print4()
{
cout<<"姓名:"<<name<<" ";
cout<<"学号:"<<number<<" ";
cout<<"性别:"<<sex<<" ";
cout<<"年龄:"<<age<<" ";
cout<<"职务:"<<job<<endl;
cout<<"学校:"<<school<<" ";
}
int main()
{
worker w1("小李","男",25,"主管",5);
w1.print3();
w1.work3();
cout<<endl;
cout<<endl;
studentleading s1("小明","男",21,"信息学院",2001,"班长");
s1.print4();
s1.work2() ;
s1.meeting() ;
return 0;
}
结果:
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了创建一个工人对象、一个学生干部对象,设置并输出对象的各项信息,并测试工人对象的工作方法、测试学生干部对象的工作和开会方法等。