这道C++编程题怎么做?求大神解答,谢谢~~~

 我来答
被主遗弃的人
2017-04-10 · TA获得超过1079个赞
知道小有建树答主
回答量:977
采纳率:50%
帮助的人:374万
展开全部
#include <iostream>
#include <string>
using namespace std;
// 图书类
class Book
{
public:
 Book() :m_nNumber(0), m_strBookName(""), m_strConcern(""), m_dPrice(0.0) {}
 virtual ~Book() {}   // 基类析构函数必须是虚函数
 // 书号的访问
 void SetNumber(int nNumber) { m_nNumber = nNumber; }
 int GetNumber() const { return m_nNumber; }
 // 书名的访问
 void SetBookName(const string &strName) { m_strBookName = strName; }
 string GetBookName() const { return m_strBookName; }
 // 出版社的访问
 void SetConcern(const string &strConcern) { m_strConcern = strConcern; }
 string GetConcern() const { return m_strConcern; }
 // 定价的访问
 void SetPrice(double dPrice) { m_dPrice = dPrice; }
 double GetPrice() const { return m_dPrice; }
 // 打印图书类
 virtual void Print() const
 {
  cout << "Num:" << GetNumber() << endl;
  cout << "BookName:" << GetBookName() << endl;
  cout << "BookConcern:" << GetConcern() << endl;
  cout << "Price:" << GetPrice() << endl;
 }
private:
 int m_nNumber;           // 书号
 string m_strBookName;    // 书名
 string m_strConcern;     // 出版社
 double m_dPrice;         // 定价
};
// 写作时间类
class PrintDate
{
public:
 PrintDate(int nYear, int nMonth, int nDay) :m_nYear(nYear), m_nMonth(nMonth), m_nDay(nDay) {}
 // 打印写作时间
 void Print() const
 {
  cout << "PrintTime:" << m_nYear << "-" << m_nMonth << "-" << m_nDay << endl;
 }
private:
 int m_nYear;
 int m_nMonth;
 int m_nDay;
};
// 作者类
class Author
{
public:
 Author() :m_strAuthorName(""), m_nAge(0), m_stPrintTime(2017, 4, 10) {}
 virtual ~Author() {}  // 基类析构函数必须是虚函数
 // 姓名的访问
 void SetAuthorName(const string &strName) { m_strAuthorName = strName; }
 string GetAuthorName() const { return m_strAuthorName; }
 // 年龄的访问
 void SetAge(int nAge) { m_nAge = nAge; }
 int GetAge() const { return m_nAge; }
 // 写作时间的访问
 void SetPrintTime(const PrintDate &stDate) { m_stPrintTime = stDate; }
 PrintDate GetPrintTime() const { return m_stPrintTime; }
 // 打印作者类
 virtual void Print() const
 {
  cout << "AuthorName:" << GetAuthorName() << endl;
  cout << "AuthorAge:" << GetAge() << endl;
  GetPrintTime().Print();
 }
private:
 string m_strAuthorName;           // 姓名
 int m_nAge;                       // 年龄
 PrintDate m_stPrintTime;          // 写作时间
};
// 图书查询卡类
class Card : public Book, public Author
{
public:
 Card() :Book(), Author(), m_strSysName("") {}
 ~Card() {};    // 子类析构函数可以不必是虚函数
 // 书籍系统名称的访问
 void SetSysName(const string &strSysName) { m_strSysName = strSysName; }
 string GetSysName() const { return m_strSysName; }
 // 打印图书查询卡类
 void Print() const
 {
  cout << "SysName:" << m_strSysName << endl;
  Book::Print();
  Author::Print();
 }
private:
 string m_strSysName;     // 书籍系统名称
};
int main()
{
 // 测试
 
 // 创建一个图书卡类
 Card *pCard = new Card();
 // 输入信息
 // 系统名称
 string strSysName = "";
 cin >> strSysName;
 // 图书编号
 int nNumber = 0;
 cin >> nNumber;
 // 图书名称
 string strBookName = "";
 cin >> strBookName;
 // 出版社
 string strConcern = "";
 cin >> strConcern;
 // 定价
 double dPrice = 0.0;
 cin >> dPrice;
 // 作者姓名
 string strAuthorName = "";
 cin >> strAuthorName;
 // 作者年龄
 int nAge = 0;
 cin >> nAge;
 // 写作日期
 int nYear = 0, nMonth = 0, nDay = 0;
 cin >> nYear >> nMonth >> nDay;
 // 给图书卡对象各个成员赋值
 pCard->SetSysName(strSysName);
 pCard->SetNumber(nNumber);
 pCard->SetBookName(strBookName);
 pCard->SetConcern(strConcern);
 pCard->SetPrice(dPrice);
 pCard->SetAuthorName(strAuthorName);
 pCard->SetAge(nAge);
 pCard->SetPrintTime(PrintDate(nYear, nMonth, nDay));
 cout << "************************************" << endl;
 // 测试输出
 pCard->Print();
 cout << "************************************" << endl;
 // 测试多态输出,book类
 Book *pBook = pCard;
 pBook->Print();
 cout << "************************************" << endl;
 // 测试多态输出,Author类
 Author *pAuthor = pCard;
 pAuthor->Print();
 system("pause");
 // 释放内存
 delete pCard;
    return 0;
}

运行结果:

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式