关闭

C++ 实验七

920人阅读 评论(0) 收藏 举报
分类:

实验七
继承与派生
【实验目的】
1、理解继承在面向对象程序设计中的重要作用;
2、理解继承和派生的概念;
3、掌握通过继承派生出一个新类的方法;
4、进一步学习简单面向对象程序的编写;
【实验内容】
1. 设计一个Person类,它有两个派生类Student和Employee,Employee有两个派
生类Faculty和Staff。
•Person类有一个string类型的名字name,string型的身份号id,string型的电话
号码phonenumber, Person类构造函数的对应name的参数不能有默认形参值。
•Student类有一个年级grade属性(Freshman、Sophomore、Junior或Senior),
将属性值定义为常量;
•Employee类有一个string型的办公地点office,一个double型的月薪水salary
和一个MyDate型的雇佣日期dateHired;
•Faculty类有一个级别rank属性(Professor = 3、 AssociateProfessor = 2、
AssistantProfessor = 1),属性值也定义成常量。教师的薪水的计算方式为“薪水
=基本工资×级别”;
•Staff类有一个职务position属性,为string类型,教工的薪水计算方式为“薪
水=基本工资+津贴×工作年数”。工作年数算到2010年1月1日为止,基本工资
BasicWages、津贴Allowance定义为常量。
•上述5个类都有一个print成员函数来输出该类对象除薪水外的基本信息。
•MyDate类有year、month、day三个数据成员,有一个计算两个MyDate对象
间年差的成员函数diffYear(MyDate &),年差计算只精确到月;三个数据成员的
获取器函数。
3.
定义上述类,并合理地补充构造函数和其它需要的函数。
在main函数中,定义一个Person类对象,一个Student类对象和一个Employee
类对象一个Faculty类对象和一个Staff类对象。输出Person类及其派生类对象的基
本信息,并输出Faculty类对象和Staff类对象的薪水。

#include <IOSTREAM>
#include <string>
#include<sstream>
using namespace std;
class Person{
public:
	Person(){}
	Person(string _name,string _id="0",string _phonenumber="0000"):name(_name),id(_id),phonenumber(_phonenumber){
	}
	void print(){
		cout<<"name:"<<name<<endl;
		cout<<"id:"<<id<<endl;
		cout<<"phonenumber:"<<phonenumber<<endl;
	}
	void setName(string _name){
		name=_name;
	}
	void setId(string _id){
		id=_id;
	}
	void setPhonenumber(string _num){
		phonenumber=_num;
	}
private:
	string name,id,phonenumber;
};

class Student :public Person{
public:
	static	enum Grade{Freshman,Sophomore,Junior,Senior};
	Student(Grade g,string _name="none",string _id="0",string _phonenumber="0000"):Person(_name,_id,_phonenumber),grade(g){
	}
	void setGrade(Grade g){
		grade=g;
	}
	void print(){
		Person::print();
		cout<<"Grade:"<<grade<<endl;
	};
private:
	Grade grade;
};
class MyDate{
public:
	MyDate(int y=0,int m=0,int d=0):year(y),month(m),day(d){}
	MyDate diffYear(const MyDate &bd){
		MyDate tmp;	
		int mons;
		if(year>bd.year){
			mons=(year-bd.year)*12+month-bd.month;
		}else{
			mons=(bd.year-year)*12-month+bd.month;
		}
		tmp.year=mons/12;
		tmp.month=mons%12;
		return tmp;
	}
	int getYear(){
		return year;
	}	
	int getDay(){
		return day;
	}
	int getMonth(){
		return month;
	}
	string toString(){
		stringstream ss;
		ss<<"¹ÍÓ¶ÈÕÆÚ£º "<<year<<"Äê"<<month<<"ÔÂ"<<day<<"ÈÕ";
		string tmp=ss.str();
		return tmp;
	}
private:
int year,month,day;
};

class Employee:public Person{
public:
	Employee(string _name="none",string _id="0",string _phonenumber="0000"):Person(_name,_id,_phonenumber){
	}
	void print(){
		Person::print();
		cout<<"office:"<<office<<endl;
		cout<<dataHired.toString()<<endl;
	};
	void setOffice(string off){
		office=off;
	}
	void setSalary(double s){
		salary=s;
	}
	void setDataHired(MyDate d){
		dataHired=d;
	}
protected:
	string office;
	double salary;
	MyDate dataHired;
};
class Faculty:public Employee{
public:
static 	enum Rank{AssistantProfessor=1,AssociateProfessor,Professor};
	Faculty(string _name,string _id,string _phonenumber,Rank r=AssistantProfessor,double bw=3000):Employee(_name,_id,_phonenumber),BasicWages(bw),rank(r){
	
	}
	Faculty(Rank r=AssistantProfessor,double bw=3000):BasicWages(bw),rank(r){
	
	}
	double getSalary(){
		return  BasicWages*rank;
		return 0;
	}
	void print(){
		Employee::print();
		cout<<"rank:"<<rank<<endl;
		cout<<"BasicWages:"<<BasicWages<<endl;
	};
	void setRank(Rank r){
		rank=r;
	}
private:
	Rank rank;
	const double BasicWages;
};
class Staff:public Employee{
public:
	Staff(string _name,string _id,string _phonenumber,double bw=3000,double aw=1000):Employee(_name,_id,_phonenumber),BasicWages(bw),Allowance(aw){
	
	}
	Staff(double bw=3000,double aw=1000):BasicWages(bw),Allowance(aw){
	
	}
	double getSalary(){
		MyDate endDay(2010,1,1);
		MyDate dif=endDay.diffYear(dataHired);
		return BasicWages+Allowance*dif.getYear();
	}
	void print(){
		Employee::print();
		cout<<"BasicWages:"<<BasicWages<<endl;
		cout<<"Allowance:"<<Allowance<<endl;
	};
	void setPosition(string posi){
		position=posi;
	}
private:
	const double BasicWages,Allowance;
	string position;
};
int main(){
	MyDate today(1994,3,1);
	Person person("Wang");
	Student student(Student::Grade::Freshman);
	Employee employee;
	Faculty faculty(Faculty::Rank::AssistantProfessor);
	Staff staff;

	employee.setDataHired(today);
	employee.setOffice("Home");
	faculty.setDataHired(today);
	faculty.setOffice("Home");
	staff.setDataHired(today);
	staff.setOffice("Home");
	person.print();
	student.print();
	employee.print();
	faculty.print();
	staff.print();
	cout<<"#Salary:"<<endl;
	cout<<"faculty:"<<faculty.getSalary()<<endl;
	cout<<"staff:"<<staff.getSalary()<<endl;
	return 0;
}


0
0

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