【C++OJ多重继承与虚拟继承】日程安排(多继承+友元函数)

本文介绍如何利用多继承和友元函数创建Schedule类,根据日期和时间对日程进行排序,以找出最早安排的任务。通过实例演示如何构造日程类、比较时间并显示输出结果。

D. 日程安排(多继承+友元函数)

题目描述

已有一个日期类Date,包括三个protected成员数据year,month,day;

另有一个时间类Time,包括三个protected成员数据hour,minute,second,12小时制;

现需根据输入的日程的日期时间,安排前后顺序,为此以Date类和Time类为基类,建立一个日程类Schedule,包括以下新增成员:

int ID;//日程的ID

定义友元函数bool before(const Schedule & s1,const Schedule & s2);//判断日程s1时间是否早于日程s2。

编写主函数,根据输入的各项日程信息,建立日程对象,找出需要最早安排的日程(日期和时间相等时,输出较早建立的日程),并输出该日程对象的信息。

输入

测试输入包含若干日程,每个日程占一行(日程ID日程日期日程时间)。

当读入0时输入结束,相应的结果不要输出。

输出

时间最靠前的日程

输入样例

1 2019 6 27 8 0 1
2 2019 6 28 8 0 1
3 2020 1 1 8 0 0
0

输出样例

The urgent schedule is No.1: 2019/06/27 08:00:01

参考代码

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
class Date //日期类Date
{
protected: //包括三个protected成员数据year,month,day;
    int year, month, day;

public:
    Date(int a = 0, int b = 0, int c = 0) : year(a), month(b), day(c){}; //构造函数,初始化参数列表
};

class Time //时间类Time
{
protected:
    int hour, minute, second; //包括三个protected成员数据hour,minute,second

public:
    Time(int a = 0, int b = 0, int c = 0) : hour(a), minute(b), second(c){}; //构造函数,初始化参数列表
};

//需根据输入的日程的日期时间,安排前后顺序
class Schedule : public Date, public Time //以Date类和Time类为基类,建立一个日程类Schedule
{
protected:
    int ID; //新增成员,日程的ID

public:
    Schedule(const Date &p, const Time &q, int ID = 0) : Date(p), Time(q), ID(ID) {} //构造函数,初始化参数列表
    friend bool before(const Schedule &S1, const Schedule &S2);                      //友元函数
    void display()                                                                   //函数输出
    {
        cout << "The urgent schedule is No." << ID << ": " << year << "/" << setw(2) << setfill('0') << month << "/" << setw(2) << setfill('0') << day << " ";
        cout << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute << ":" << setw(2) << setfill('0') << second << endl;
    }
};

bool before(const Schedule &S1, const Schedule &S2) //判断日程s1时间是否早于日程s2。
{
    int sum1, sum2;
    sum1 = S1.year * 10000 + S1.month * 100 + S1.day + S1.hour * 3600 + S1.minute * 60 + S1.second;
    sum2 = S2.year * 10000 + S2.month * 100 + S2.day + S2.hour * 3600 + S2.minute * 60 + S2.second;
    return sum1 < sum2; //日期和时间相等时,输出较早建立的日程
}

int main()
{
    int id;
    Date D(10000, 0, 0);
    Time T(0, 0, 0);
    Schedule S_MAX(D, T, 10000); //先把时间设置的够大
    while (cin >> id)
    {
        if (id == 0)
            break;
        else
        {
            int y, m, d, h, min, s;
            cin >> y >> m >> d >> h >> min >> s;
            Date D1(y, m, d);
            Time T1(h, min, s);
            Schedule S1(D1, T1, id);
            if (before(S1, S_MAX))
                S_MAX = S1; //找出最小的时间
        }
    }
    S_MAX.display();
    return 0;
}
### 西北农林科技大学 C++ OJ 继承 示例 题目 解法 在西北农林科技大学的C++在线判题系统(OJ)中,继承是一个常见的编程主题。以下是一些继承相关的题目示例以及可能的解决方案。 #### 示例题目 1: 单继承 题目描述:定义一个基类 `Animal` 和派生类 `Dog`,其中 `Dog` 类继承自 `Animal` 类。要求实现以下功能: - 基类 `Animal` 包含一个虚函数 `makeSound()`。 - 派生类 `Dog` 重写该函数以输出 `"Woof!"`。 ```cpp #include <iostream> using namespace std; class Animal { public: virtual void makeSound() { cout << "Some generic sound" << endl; } }; class Dog : public Animal { public: void makeSound() override { cout << "Woof!" << endl; } }; int main() { Animal* myAnimal = new Dog(); myAnimal->makeSound(); // 输出 "Woof!" delete myAnimal; return 0; } ``` 上述代码展示了单继承的基本用法[^1]。 #### 示例题目 2: 多继承 题目描述:定义两个基类 `Swimmer` 和 `Runner`,以及一个派生类 `Dolphin`,要求 `Dolphin` 同时继承自 `Swimmer` 和 `Runner`。实现以下功能: - `Swimmer` 包含一个函数 `swim()`,输出 `"Swimming..."`。 - `Runner` 包含一个函数 `run()`,输出 `"Running..."`。 - `Dolphin` 能够调用这两个函数。 ```cpp #include <iostream> using namespace std; class Swimmer { public: void swim() { cout << "Swimming..." << endl; } }; class Runner { public: void run() { cout << "Running..." << endl; } }; class Dolphin : public Swimmer, public Runner {}; int main() { Dolphin d; d.swim(); // 输出 "Swimming..." d.run(); // 输出 "Running..." return 0; } ``` 这段代码展示了多继承的实现方式[^2]。 #### 示例题目 3: 虚继承 题目描述:定义三个类 `A`、`B` 和 `C`,其中 `B` 和 `C` 都继承自 `A`,而 `D` 同时继承自 `B` 和 `C`。为了避免菱形继承问题,使用虚继承。 ```cpp #include <iostream> using namespace std; class A { public: int x; A(int val) : x(val) {} }; class B : virtual public A { public: B(int val) : A(val) {} }; class C : virtual public A { public: C(int val) : A(val) {} }; class D : public B, public C { public: D(int val) : A(val), B(val), C(val) {} }; int main() { D obj(10); cout << obj.x; // 输出 10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ferry_xie

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

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

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

打赏作者

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

抵扣说明:

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

余额充值