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;
}
时间: 2024-08-05 03:31:26

C++ 实验七 继承与派生的相关文章

嵌入式linux C++语言(七)——继承与派生

嵌入式linux C++语言(七)--继承与派生 一.继承 在C++编程中软件可重用性(software reusability)是通过继承(inheritance)机制来实现的.类的继承,是新的类从已有类那里得到已有的特性.或从已有类产生新类的过程就是类的派生.原有的类称为基类或父类,产生的新类称为派生类或子类. 派生类的声明:class 派生类名:[继承方式] 基类名{派生类成员声明:};    一个派生类可以同时有多个基类,这种情况称为多重继承,派生类只有一个基类,称为单继承. 继承方式规

实验4 类的继承、派生和多态(1)

实验目的 1. 理解类的继承和派生机制 2. 掌握派生类的定义和使用 3. 理解和掌握派生类成员的标识和访问中同名覆盖原则.二元作用域分辨符和虚基类的用法 4. 掌握派生类构造函数和析构函数的定义及调用次序 5. 理解运算符重载的目的,掌握运算符重载函数的编写方法 实验准备 1. 类的继承和派生 引入继承和派生机制的目的 基本概念:继承.派生.基类.直接基类.间接基类 .派生类 语法 派生类定义的语法格式(单重继承和多重继承) 派生类构造函数及其初始化列表书写形式 派生类成员的标识与访问 同名覆

c++实验四 类的继承、派生和多态

实验目的 1. 理解类的继承和派生机制 2. 掌握派生类的定义和使用 3. 理解和掌握派生类成员的标识和访问中同名覆盖原则.二元作用域分辨符和虚基类的用法 4. 掌握派生类构造函数和析构函数的定义及调用次序 5. 理解运算符重载的目的,掌握运算符重载函数的编写方法 实验内容 一.ElectricCar类 #ifndef BATTERY_H #define BATTERY_H class Battery { public: Battery(int nb = 70); int showbattery

第七章:继承与派生

主要内容: 1.类的继承与派生 2.类成员的访问控制 3.单继承与多继承 4.派生类的构造.析构函数 5.类成员的标识与访问 若为public继承方式: 1.派生类的成员函数可以直接访问基类的public和protected成员,不可访问基类的private成员. 2.派生类的对象只能访问基类的public成员 若为private继承方式: 1.派生类的成员函数可以直接访问基类的public和private成员,不能直接访问基类private成员. 2.派生类的对象不能直接访问基类中的任何成员.

c++第四次实验-继承与派生

一.问题及代码 /* * 文件名称:学生成绩 * 作 者:刘晓龙 * 完成日期:2016年5月6日 * 版 本 号: * 对任务及求解方法的描述部分:继承与派生 * 输入描述:输入学生信息和班长信息 * 问题描述:打印学生信息和班长信息 * 程序输出:输出学生的信息和班长的信息 * 问题分析:运用继承和派生 * 算法设计:无 */ #include <iostream> #include <string> using namespace std; class Stu //声明基类

实验6 继承

一.实验目的与要求: 复习运算符重载并应用. 了解并掌握通过继承派生出一个新的类的方法. 二.实验过程: 完成实验6继承中A-C三道题,见:http://acm.hpu.edu.cn/contest.php?cid=1018,密码:c++06.将答题过程简单记录到实验过程中. 将答题结果写到实验结果中,并根据答题结果进行分析.反思,将其写到实验分析中,并写上实验时间. 请按以上要求认真填写实验报告.

c++学习--继承与派生

继承和派生 1 含有对象成员(子对象)的派生类的构造函数,定义派生类对象成员时,构造函数的执行顺序如下: 1 调用基类的构造函数,对基类数据成员初始化: 2 调用对象成员的构造函数,对对象成员的数据成员初始化: 3 执行派生类的构造函数体,对派生类数据成员初始化. 代码如下:#include<iostream.h> class base{ int x; public: base(int i) { x=i; cout<<"基类的构造函数"<<endl;

黑马程序员-----类的继承和派生概念

------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训..Net培训</a>.期待与您交流! ----- 第一讲 类的继承和派生概念 本小节知识点: 1.[理解]什么是继承? 2.[掌握]类的继承和派生概念 1.什么是继承? 2.OC中的继承与派生 1 #import <Foundation/Foundation.h> 2 3 @in

继承与派生类Circle and Rectangle和The Person, Student, Employee, Faculty and Staff Class

C++为了更加的灵活,更加是程序员自由随性的编写程序,定义了类类型.我们在其中自定义类型,在其中封装函数,甚至在其中重载运算符来改变其原有的属性.类的继承又将类应用的更加开拓.我们用类来使用原有的类,充分体现了C++的开放与包容 派生类(或子类,继承类)拥有基类(或父类,超类)的所有属性及行为,又定义了自己的属性及行为,派生类能够访问基类,但基类就没有访问派生类的权限.二者的关系并不可逆.基类是派生类的抽象,派生类是基类的具体化.从一个基类派生出来的叫做单继承,从多个基类派生出来的叫做多继承.由