编译器: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
    评论
C++中,基类派生类是面向对象编程的重要概念。基类派生类的父类,而派生类则是基于基类进行扩展的类。 在基类中声明的函数可以被派生类继承并且可以被派生类重写。派生类也可以定义自己的函数来扩展基类的功能。当派生类覆盖基类的函数时,可以使用关键字`virtual`来实现多态性。 以下是一些关于基类派生类函数的知识点: 1. 基类中的函数可以被派生类继承。派生类可以通过调用基类中的函数来使用基类的功能。 2. 派生类可以通过重写基类中的函数来改变其行为。当派生类覆盖基类的函数时,必须使用与基类函数相同的函数签名(即函数名称、参数类型和返回类型相同)。 3. 派生类中的函数可以调用基类中的同名函数,使用作用域解析运算符(::)来指定基类函数。 4. 基类中的虚函数可以被派生类覆盖。当一个指向派生类对象的基类指针调用虚函数时,将调用派生类中的实现。 5. 派生类中的虚函数可以覆盖基类中的虚函数。当一个指向派生类对象的基类指针调用该虚函数时,将调用派生类中的实现。 6. 派生类中的非虚函数不能覆盖基类中的同名虚函数。如果派生类声明了一个与基类中的虚函数同名的非虚函数,那么该函数将隐藏基类中的虚函数。 7. 派生类可以通过使用`override`关键字来覆盖基类中的虚函数。这可以帮助编译器检查是否正确地覆盖了基类中的虚函数。 总之,在C++中,基类派生类函数的继承、重写和多态性是实现面向对象编程的重要机制。理解这些概念并正确使用它们可以使程序更加清晰、可读性更高,也可以更加方便地扩展和维护代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

那不勒斯的萤火丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值