oj继承组合

本文介绍了一个使用C++实现的教授类,该类继承自教师类,并包含出生日期属性。通过具体代码展示了如何定义类、继承及成员函数。
#include <iostream>
#include <string>
using namespace std;
class BirthDate
{
public:
    BirthDate(int,int,int);
    void display();
    void setbirthday(int,int,int);
private:
    int year;
    int month;
    int day;
};
class Teacher
{
public:
    Teacher(int,string,char);
    void display();
private:
    int num;
    string name;
    char sex;
};
class Professor:public Teacher
{
public:
    Professor(int,string,char,BirthDate);//这里要注意
    void display();
    void setbirthday(int,int,int);
private:
    BirthDate birthday;//这里要注意
};
//主函数已给定如下,提交时不需要包含,会自动添加到程序尾部
/* C++代码 */
BirthDate::BirthDate(int y,int m,int d):year(y),month(m),day(d){}
void BirthDate::setbirthday(int y,int m,int d)
{
    year=y;
    month=m;
    day=d;
}
void BirthDate::display()
{
    cout<<"birthday:"<<year<<"/"<<month<<"/"<<day<<endl;
}
Teacher::Teacher(int n,string nam,char s):num(n),name(nam),sex(s){}
void Teacher::display()
{
    cout<<"num:"<<num<<endl;
    cout<<"name:"<<name<<endl;
    cout<<"sex:"<<sex<<endl;
}
Professor::Professor(int n,string nam,char s,BirthDate b):Teacher(n,nam,s),birthday(b){}
void Professor::setbirthday(int y,int m,int d)
{
    birthday.setbirthday(y,m,d);
}
void Professor::display()
{
    Teacher::display();
    birthday.display();
}
int main()
{
    int num;
    string name;
    char sex;
    int year,month,day;
    cin>>num>>name>>sex;
    cin>>year>>month>>day;
    Professor prof(num,name,sex,BirthDate(year,month,day));
    cin>>year>>month>>day;
    prof.setbirthday(year,month,day);
    prof.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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值