【问题】如何在派生类中给对象的赋值

本文详细介绍了如何在C++中创建派生类CD和Book,它们继承自基类Publication。展示了如何使用构造函数初始化输入参数,以及如何在派生类中访问基类的成员函数进行输入数据和显示对象信息。
摘要由CSDN通过智能技术生成
int main() {
    Book b;
    CD c(1, 2, 3, "Noname", "肖邦钢琴协奏曲", 61, 2018, 8, 1);//给对象赋值
    b.inputdata();
    b.display();
    c.display();
    return 0;
}

如何在派生类中给对象赋值

给基类Publication的派生类CD赋值

1.构造函数的初始化

先对输入参数初始化,后面用“:”将初始化的参数传入基类

CD(int ph = 0, int pm = 0, int ps = 0,string t = "", 
string n = "", float p = 0, int h = 0, int m = 0, int s = 0)
        : Publication(t, n, p, h, m, s), playtime(ph, pm, ps)
    {

    }

tips:访问从基类继承过来的同名成员函数

void inputdata() {
        Publication::inputdata();
        cin >> playtime.hour >> playtime.minute >> playtime.second;
    }

完整代码

#include <iostream>
#include <string>
using namespace std;

// 年月日
struct Date {
    int year;
    int month;
    int day;

    Date(int y = 0, int m = 0, int d = 0) {
        year = y;
        month = m;
        day = d;
    }

    ~Date() { }
};

// 时分秒
struct Time {
    int hour;
    int minute;
    int second;

    Time(int h = 0, int m = 0, int s = 0) {
        hour = h;
        minute = m;
        second = s;
    }

    ~Time() { }
};

// 基类:出版物
class Publication {
private:
    string title; // 出版物的标题
    string name; // 名称
    float price; // 单价
    Date date; // 出版日期

public:
    Publication(string t = "", string n = "", float p = 0, int h = 0, int m = 0, int s = 0)
        :date(h, m, s) {
        title = t;
        name = n;
        price = p;
    }

    ~Publication() { }

    void inputdata() {
        cin >> title;
        cin >> name;
        cin >> price;
        cin >> date.year >> date.month >> date.day;
    }

    void display() {
        cout << "*****display*****" << endl;
        cout << "title:" << title << endl;
        cout << "name:" << name << endl;
        cout << "price:" << price << endl;
        cout << "year-month-day:" << date.year << "-" << date.month << "-" << date.day << endl;
    }
};

class Book :public Publication
{
private:
    int page;
public:
    Book(string t = "", string n = "", float p = 0, int h = 0, int m = 0, int s = 0, int pg = 0)
        : Publication(t, n, p, h, m, s), page(pg)
    {

    }
    ~Book()
    {

    }
    void inputdata() {
        Publication::inputdata();
        cin >> page;
    }

    void display() {
        Publication::display();
        cout << "pages:" << page << endl;
    }
};
class CD :public Publication
{
private:
    Time playtime;
public:
    CD(int ph = 0, int pm = 0, int ps = 0,string t = "", string n = "", float p = 0, int h = 0, int m = 0, int s = 0)
        : Publication(t, n, p, h, m, s), playtime(ph, pm, ps)
    {

    }
    ~CD()
    {

    }
    void inputdata() {
        Publication::inputdata();
        cin >> playtime.hour >> playtime.minute >> playtime.second;
    }
    void display() {
        Publication::display();
        cout << "playtime(h:m:s) " << playtime.hour << ":" << playtime.minute << ":" << playtime.second;
        
    }
};

int main() {
    Book b;
    CD c(1, 2, 3, "Noname", "肖邦钢琴协奏曲", 61, 2018, 8, 1);
    b.inputdata();
    b.display();
    c.display();
    return 0;
}

输入输出用例

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值