定义一个人(Person)类,包括属性:姓名、性别、年龄、国籍;包括方法:吃饭、睡觉、工作。吃饭方法没有返回值,输出:Everyone need eat.睡觉方法实现方法的重载。

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

题目:

(1)定义一个人(Person)类,包括属性:姓名、性别、年龄、国籍;包括方法:吃饭、睡觉、工作。吃饭方法没有返回值,输出:Everyone need eat.睡觉方法实现方法的重载,有两个方法,其中一个方法的参数为时间,该方法对睡觉时间进行判断,当通过实参传递过来的时间超过23:30,则程序输出:You went to bed too late. You should go to bed early.另一个方法的参数为数字(表示的是睡眠的时间),当通过实参传递过来的数字(即睡眠时间)小于7个小时,则程序输出:你睡的太少了,这样会变傻的!工作方法输出:Everyone need work.


(2)根据人类,派生一个学生类,增加属性;学校、学号重写工作方法(学生的工作是学习),该方法输出:I will good good study, day day up.


(3)根据人类,派生一个工人类,增加属性:单位、工龄;添加工资发放方法
该方法通过判断工作时间给出工人的工资,如果工作在6小时以内月薪为3500元(即向屏幕输出:工人的工资为3500元),工作时间在6~8小时月薪为4500元(即向屏幕输出:工人的工资为4500元),工作时间在8~10小时月薪为6000元(即向屏幕输出:工人的工资为6000元)。


(4)根据学生类,派生一个学生干部类,增加属性;职务增加方法;开会


(5)编写主函数分别对上述4类具体人物进行测试。
 

代码:

#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)类,包括属性:姓名、性别、年龄、国籍;包括方法:吃饭、睡觉、工作。

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酷酷滴Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值