__皮蛋solo粥 2022-05-13 20:49 采纳率: 100%
浏览 161
已结题

图书类Book(c++)

题目描述
设计一个图书类Book,该类有书名、作者、isbn、价格,出版日期五个数据成员。其中出版日期是Date类的对象。main函数的代码已经给出,请根据main函数代码和对应的输出给出Book类和Date类的代码。(需要提交main函数之外的其他代码部分)

main函数的代码如下:

int main(){

int year,month,day;

string name,author,isbn;

double price;

cin>>year>>month>>day;

Date date(year,month,day);

cin>>name>>author>>isbn>>price;

Book b(name,author,isbn,date,price);

b.printInfo();

return 0;

}

输入描述
输入图书的各种信息

输出描述
输出图书的各种信息

提示
输入:

2018 8 1

C++Programming Mike 12345 35.0

输出:

C++Programming Mike 12345 35

2018/8/1

  • 写回答

1条回答 默认 最新

  • 丨秋水丨 2022-05-13 21:19
    关注
    #include<string>
    #include<iostream>
    using namespace std;
    class Date
    {
    public:
        Date(int y, int m, int d) :year(y), month(m), day(d)
        {
        }
    
        int GetYear()const { return year; }
        int GetMonth()const { return month; }
        int GetDay()const { return day; }
    private:
        int year, month, day;
    };
    
    class Book
    {
    public:
        Book(const string& n, const string& a, const string& i,
            const Date& d, const double p)
            : name(n), author(a), isbn(i), date(d), price(p)
        {
    
        }
    
        void printInfo()
        {
            cout << name << " " << author << " " << isbn << " " << price << endl;
            cout << date.GetYear() << "/" << date.GetMonth() << "/" << date.GetDay() << endl;
        }
    private:
        string name, author, isbn;
        Date date;
        double price;
    };
    
    int main() {
        int year, month, day;
    
        string name, author, isbn;
    
        double price;
    
        cin >> year >> month >> day;
    
        Date date(year, month, day);
    
        cin >> name >> author >> isbn >> price;
    
        Book b(name, author, isbn, date, price);
    
        b.printInfo();
        return 0;
    }
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 5月25日
  • 已采纳回答 5月17日
  • 创建了问题 5月13日

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line