赞
踩
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
- #include<iostream>
- #include<string.h>
- using namespace std;
- class Person{
- public:
- string name;
- string sex;
- string nationality;
- int age;
-
- // 构造函数,用来初始化对象的属性
- Person(string na,string se,int ag,string national){
- name= na;
- sex = se;
- age = ag;
- nationality = national;
- }
- void eat()
- {
- cout<<"Everyone need eat."<<endl;
- }
- void sleep(double time)
- {
- if(time>23.30)
- {
- cout<<"You went to bed too late. You should go to bed early."<<endl;
- }
- else
- {
- cout<<"还没到睡觉时间!"<<endl;
- }
- }
- void sleep(int time)
- {
- if(time<7)
- {
- cout<<"你睡的太少了,这样会变傻的!"<<endl;
- }
- else
- {
- cout<<"你的睡眠充足!"<<endl;
- }
- }
- void work()
- {
- cout<<"Everyone need work."<<endl;
- }
- void print()
- {
- cout<<"姓名:"<<name<<endl;
- cout<<"性别:"<<sex<<endl;
- cout<<"年龄:"<<age<<endl;
- cout<<"国籍:"<<nationality<<endl;
- }
- };
- class student:public Person{
- public:string school;
- string number;
- student(string na,string se,int ag,string national,string sch,string num):Person(na,se,ag,national)
- {
- school=sch;number=num;
- }
- void work()
- {
- cout<<"I will good good study, day day up."<<endl;
- }
- void print()
- {
- cout<<"学校:"<<school<<endl;
- cout<<"姓名:"<<name<<endl;
- cout<<"学号:"<<number<<endl;
- cout<<"性别:"<<sex<<endl;
- cout<<"年龄:"<<age<<endl;
- cout<<"国籍:"<<nationality<<endl;
- }
-
- };
- class worker:public Person{
- private:
- string danwei;
- int goling;
- public:worker(string na,string se,int ag,string national,string da,int gl):Person(na,se,ag,national)
- {
- danwei=da;goling=gl;
- }
- void fagongzi(int time)
- {
- if(time<6)
- {
- cout<<"工人的工资为3500元."<<endl;
- }
- else if(time>=6 &&time<8)
- {
- cout<<"工人的工资为4500元"<<endl;
- }
- else if(time>=8 &&time<=10)
- {
- cout<<"工人的工资为6000元"<<endl;
- }
- else
- {
- cout<<"输入的工作时间有误!!!" <<endl;
- }
-
- }
- void print()
- {
- cout<<"姓名:"<<name<<endl;
- cout<<"性别:"<<sex<<endl;
- cout<<"年龄:"<<age<<endl;
- cout<<"国籍:"<<nationality<<endl;
- cout<<"单位:"<<danwei<<endl;
- cout<<"工龄:"<<goling<<endl;
- }
-
- };
- class studentleading:public student{
- private:string job;
- public:
- studentleading(string na,string se,int ag,string national,string sch,string num,string j):student(na,se,ag,national,sch,num)
- {
- job=j;
- }
- void print()
- {
- cout<<"学校:"<<school<<endl;
- cout<<"姓名:"<<name<<endl;
- cout<<"学号:"<<number<<endl;
- cout<<"性别:"<<sex<<endl;
- cout<<"年龄:"<<age<<endl;
- cout<<"工作:"<<job<<endl;
- cout<<"国籍:"<<nationality<<endl;
- }
- void meeting()
- {
- cout<<"学生会经行开会!"<<endl;
- }
-
- };
- int main()
- {
- Person p1("小王","男",18,"中国");//共同属性
- p1.print();//输出共用属性。
- p1.sleep(7) ;//调用公用的时间。
- cout<<"**********************"<<endl; //学生类
- student st1("小天","女",17,"中国","希望中学","1002");
- st1.print();
- cout<<"**********************"<<endl; //工人类
- worker w1("小李","男",25,"中国","流水线部门",3);
- w1.print();
- w1.fagongzi(8);
- cout<<"**********************"<<endl; //学生会部门类
- studentleading s1("小明","男",17,"中国","希望中学","1001","学生会");
- s1.print();
- s1.meeting();
- return 0;
- }
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了(1)定义一个人(Person)类,包括属性:姓名、性别、年龄、国籍;包括方法:吃饭、睡觉、工作。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。