首页 > 代码库 > C++ 实验七 继承与派生

C++ 实验七 继承与派生

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 &),年差计算只精确到月;三个数据成员的获取器函数。

2.  定义上述类,并合理地补充构造函数和其它需要的函数。在main函数中,定义一个Person类对象,一个Student类对象和一个Employee类对象一个Faculty类对象和一个Staff类对象。输出Person类及其派生类对象的基本信息,并输出Faculty类对象和Staff类对象的薪水。

 

#include <iostream>#include <math.h>#include <string>using namespace std;/* Person */class Person{public:/*    Person(string name1,string id1,string phonenumber1)        {name=name1;id=id1;phonenumber=phonenumber1;}*/    void person(string a,string b,string c)    {        name=a;        id=b;        phonenumber=c;    }    void print_person()    {        cout << "Name: "<<name << " Id: " << id << " Phonenumber: " << phonenumber << endl;    }    ~Person(){}private:    string name,id,phonenumber;};/* MyDate */class MyDate{public:    double diffYear(MyDate &a)    {        int sum;        sum = 12 * (year-a.year-1) + fabs(12-a.month);  //¾«È·µ½Ô        return double(sum);    }    void Input_year(int y)    {        year=y;    }    void Input_month(int m)    {        month=m;    }    void Input_day(int d)    {        day=d;    }    int Output_year()    {        return year;    }    int Output_month()    {        return month;    }    int Output_day()    {        return day;    }    void print_MyDate()    {        cout << "MyDate: " << year << "/" << month << "/" << day << endl;    }private:    int year,month,day;};/* Student */class Student:public Person{public:    Student(string a):grade(a){}    void print_student()    {        cout << "grade: " << grade << endl;    }private:    const string grade; };/* Employee */class Employee:public Person{public:/*    Employee(string name1,string id1,string phonenumber1,string office1,double salary1,MyDate dateHired1):Person(name1,id1,phonenumber1)    {        office=office1;        salary=salary1;        dateHired=dateHired1;    }*/    void Input_office(string a)    {        office=a;    }    void Input_salary(double b)    {        salary=b;    }    void Input_dateHired(MyDate &d)    {        dateHired = d;    }    void print_employee()    {        cout << "office: " << office << "  ";        cout << "雇佣日期: " << dateHired.Output_year() << "/" << dateHired.Output_month() << "/" << dateHired.Output_day() << endl;    }private:    string office;    double salary;    MyDate dateHired;};/* Faculty */class Faculty:public Employee{public:    Faculty(int a)    {        rank = a;    }private:    int rank;};/* Staff */class Staff:public Employee{public:    void Input_Position(string a)    {        position = a;    }    void Input_BasicWages(int b)    {        BasicWages=b;        }    void Input_Allowance(int c)    {        Allowance = c;        }    int Output_BasicWages()    {        return BasicWages;        }    int Output_Allowance()    {        return Allowance;        }    void print_staff()    {        cout << "Position: " << position << endl;    }private:    string position;    int BasicWages,Allowance;};int main(){    int rank1,year1,month1,day1;    int faculty_wages,staff_wages;    MyDate date,m1,m2;    Person person1;    //Person person1("HangZhou","110","10086");    Student student1("Freshman");    Employee employee1;    Faculty faculty1(2);      Staff staff1;        person1.person("HangZhou","110","10086");    cout << "输入雇佣日期,year/month/day: " << endl;    cin >> year1 >> month1 >> day1 ;    date.Input_year(year1);    date.Input_month(month1);    date.Input_day(day1);    employee1.Input_office("行政楼");    employee1.Input_salary(3000.0);    employee1.Input_dateHired(date);    staff1.Input_BasicWages(5000);    staff1.Input_Allowance(1000);    cout << "输入教师级别,3/2/1: " << endl;    cin >> rank1;    cout << "输入教工开始工作时间(小于2010),year/month : " << endl;    cin >> year1 >> month1 ;    m1.Input_year(year1);    m1.Input_month(month1);    m2.Input_year(2010);    m2.Input_month(1);    faculty_wages = (staff1.Output_BasicWages()) ;    staff_wages = staff1.Output_BasicWages() + (staff1.Output_Allowance() * (m2.diffYear(m1)/12));    person1.print_person();      //Person    cout << "Student: " ;        //Student    student1.print_student();     cout << "Employee:  "  ;     //Employee    employee1.print_employee();    cout << "教师工资: " << faculty_wages << endl;  //Faculty    cout << "教工工资: " << staff_wages << endl;    //Staff    return 0;}

 

C++ 实验七 继承与派生