编译器:C++ (g++)派生类定义:根据所给的基类,完成多重继承下的派生类定义函数接口定义

编译器:C++ (g++)

派生类定义:根据所给的基类,完成多重继承下的派生类定义

函数接口定义:

#include <iostream>

#include <string>

using namespace std;

//定义公共基类Person

class Person                              

{public:

  Person(string nam,char s,int a)              

   {name=nam;sex=s;age=a;}

 protected:                              

   string name;

   char sex;

   int age;

};

//定义类Teacher

class Teacher:virtual public Person              

 {public:                                 

   Teacher(string nam,char s,int a,string t):Person(nam,s,a)       

    {title=t; 

    }

  protected:                                   

    string title;                                

};

//定义类Student

class Student:virtual public Person               

 {public:

   Student(string nam,char s,int a,float sco):   

      Person(nam,s,a),score(sco){}              

  protected:                                     

    float score;                               

 };

 /*这里添加派生类的定义*/

裁判测试程序样例:

int main( )

 {Graduate grad1("Wang-li",'f',24,"assistant",89.5,1234.5);

  grad1.show( );

  return 0;

}

输出样例:

在这里给出相应的输出。例如:

name:Wang-li

age:24

sex:f

score:89.5

title:assistant

wages:1234.5


#include  <iostream>
#include  <string>
#include  <assert.h>
using  namespace  std;

//在此处补充Date类的定义
class Date {
public:
    Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
    int getYear() const { return year_; }
    int getMonth() const { return month_; }
    int getDay() const { return day_; }
    string toText() const {
        string str = to_string(year_) + "-" + to_string(month_) + "-" + to_string(day_);
        return str;
    }
    Date operator+(int n) const {
        int daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
        int days = day_ + n;
        int month = month_;
        int year = year_;
        while (days > daysInMonth[month - 1]) {
            if (month == 2 && isLeapYear()) {
                if (days > 29) {
                    days -= 29;
                    month++;
                } else {
                    break;
                }
            } else {
                days -= daysInMonth[month - 1];
                month++;
            }
            if (month > 12) {
                year++;
                month = 1;
            }
        }
        return Date(year, month, days);
    }
    Date operator-(int n) const {
        int days = day_ - n;
        int month = month_;
        int year = year_;
        while (days <= 0) {
            if (month == 3 && isLeapYear()) {
                days += 29;
                month--;
            } else {
                month--;
                if (month == 0) {
                    year--;
                    month = 12;
                }
                days += daysInMonth(month - 1);
            }
        }
        return Date(year, month, days);
    }
private:
    int year_, month_, day_;
    bool isLeapYear() const {
        if (year_ % 4 == 0 && year_ % 100 != 0 || year_ % 400 == 0) {
            return true;
        } else {
            return false;
        }
    }
    int daysInMonth(int month) const {
        if (month == 1 && isLeapYear()) {
            return 29;
        } else {
            return daysInMonth_[month];
        }
    }
    static const int daysInMonth_[12];
};

const int Date::daysInMonth_[12] = {31,28,31,30,31,30,31,31,30,31,30,31};



int  main()
{
        int  y,  m,  d;
        cin  >>  y  >>  m  >>  d;
        Date  d1(y,m,d);

        int  n;
        cin  >>  n;

        cout  <<  d1.toText()  <<  "  +  "  <<  n  <<  "  =  "  <<  (d1  +  n).toText()  <<  endl;
        cout  <<  d1.toText()  <<  "  -  "  <<  n  <<  "  =  "  <<  (d1  -  n).toText()  <<  endl;
        return  0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

那不勒斯的萤火丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值