【发布时间】:2017-12-21 01:41:40
【问题描述】:
我正在尝试使用基类的数据成员作为派生类的构造函数,但是每当我尝试运行程序时似乎都会出错。以下是我的代码:
#include <iostream>
using namespace std;
class Book
{
protected:
string title;
string author;
public:
Book(string t, string a)
{
title = t;
author = a;
}
};
class MyBook: public Book
{
protected:
int price;
public:
MyBook(string T, string A, int P): Book(title, author)
{
price = P;
}
void display()
{
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "Price: " << price << endl;
}
};
int main()
{
MyBook One("abc", "def", 2);
One.display();
}
我在创建这个派生类的构造函数时似乎有什么错?
【问题讨论】:
-
请勿标记垃圾邮件,除非您试图引起对您的问题的负面关注。
-
Please do not tag multiple languages in your question 除非您的问题专门针对这些语言。您可能认为它会导致更快的答案,但现实情况是它更有可能导致更快的关闭。
-
好的,知道了。实际上我第一次使用堆栈溢出。我并不急于回答,我只是编程方面的初学者,只是自学。生活在多么美好的世界。感谢您的建议。