C++继承,将成员添加到派生类并重新定义构造函数

C++ inheritance, add member to a derived class and redefine constructor

本文关键字:新定义 定义 构造函数 继承 成员 添加 派生 C++      更新时间:2023-10-16

我对C++有一个菜鸟问题。

我有一本课本:

class Book {
public:
    string name;
    Author author;
    double price;
    int qtyInStock;
public:
    Book(const string & name, 
         const Author & author, 
         double price, 
         int qtyInStock = 0);
    string getName() const;
    Author getAuthor() const;
    double getPrice() const;
    void setPrice(double price);
    int getQtyInStock() const;
    void setQtyInStock(int qtyInStock);
    void print() const;
    string getAuthorName() const;
};

使用构造函数:

Book::Book(
    const string & name, 
    const Author & author, 
    double price, 
    int qtyInStock)
: name(name), author(author) 
{ // Init object reference in member initializer list
    // Call setters to validate price and qtyInStock
    setPrice(price);
    setQtyInStock(qtyInStock);
}

现在我想创建一个从它派生的新类"BookDigital",写如下:

class DigitalBook:public Book
{
public:
    string site;
    DigitalBook(const string & name, 
                const Author & author, 
                double price, 
                int qtyInStock = 0,
                string & site);
};

使用构造函数:

DigitalBook:DigitalBook(
    const string & name, 
    const Author & author, 
    double price, int qtyInStock,
    string & site)
: name(name), author(author) { // Init object reference in member initializer list
    // Call setters to validate price and qtyInStock
    setPrice(price);
    setQtyInStock(qtyInStock);
}

我为电子书添加了会员"网站"

当我编译它时,它显示消息错误:

||=== Build: Debug in Test C++ (compiler: GNU GCC Compiler) ===|
C:UsersaalovisiDesktopTest C++Book.h|34|error: default argument missing for parameter 5 of 'DigitalBook::DigitalBook(const string&, const Author&, double, int, std::string&)'|
C:UsersaalovisiDesktopTest C++Book.cpp|14|error: found ':' in nested-name-specifier, expected '::'|
C:UsersaalovisiDesktopTest C++Book.cpp||In constructor 'DigitalBook::DigitalBook(const string&, const Author&, double, int, std::string&)':|
C:UsersaalovisiDesktopTest C++Book.cpp|15|error: class 'DigitalBook' does not have any field named 'name'|
C:UsersaalovisiDesktopTest C++Book.cpp|15|error: class 'DigitalBook' does not have any field named 'author'|
C:UsersaalovisiDesktopTest C++Book.cpp|15|error: no matching function for call to 'Book::Book()'|
C:UsersaalovisiDesktopTest C++Book.cpp|15|note: candidates are:|
C:UsersaalovisiDesktopTest C++Book.cpp|7|note: Book::Book(const string&, const Author&, double, int)|
C:UsersaalovisiDesktopTest C++Book.cpp|7|note:   candidate expects 4 arguments, 0 provided|
C:UsersaalovisiDesktopTest C++Book.h|8|note: Book::Book(const Book&)|
C:UsersaalovisiDesktopTest C++Book.h|8|note:   candidate expects 1 argument, 0 provided|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

为什么?谢谢你的帮助

default argument missing for parameter 5

第四个参数有默认参数,但第五个参数没有。如果一个参数具有默认参数,则其后面的所有参数也必须如此。要么qtyInStock最后一个参数,删除其默认参数,要么为site提供默认参数。

found ':' in nested-name-specifier, expected '::'

你写的是DigitalBook:DigitalBook而不是DigitalBook::DigitalBook.

class 'DigitalBook' does not have any field named 'name'
class 'DigitalBook' does not have any field named 'author'

只能在构造函数的初始化器列表中初始化当前类的成员,而不能初始化基类的成员。与其尝试初始化每个基成员,不如初始化整个基子对象:

DigitalBook::DigitalBook(const string & name, const Author & author, double price, int qtyInStock, string & site)
    : Book(name, author, price, qtyInStock), site(site)
{}

没有必要调用setPricesetQtyInStock,假设Book构造函数对这些参数做了正确的事情。

no matching function for call to 'Book::Book()'

这是因为,由于您没有在初始化器列表中包含 Book 的条目,因此需要对其进行默认初始化;但它没有合适的构造函数。如上所述添加初始化器也可以解决此问题。

如果函数具有某个参数的默认值,则所有其他更进一步的参数也应具有默认值。这是编译器试图在其第一条消息中告诉您的内容。

我认为你的问题是你在构造函数(int qtyInStock = 0)中为第四个参数定义了一个默认值,而不是为第五个参数(字符串和站点)定义了一个默认值。

您应该避免参数 4 的定义,或者为参数 5 提供默认值。

另外,我认为您的构造函数存在问题:DigitalBook::D igitalBook应该调用基类构造函数,我猜

构建一个DigitalBook对象意味着必须构建一个Book对象。但是,您没有用于Book的构造函数(没有参数)

因此,在尝试创建DigitalBook时,无法构建父类,并且出现错误。

你应该做的是

DigitalBook:DigitalBook(const string & name,
                        const Author & author,
                        double price,
                        int qtyInStock,
                        string & _site) :
     Book(name, author, price, qtyInStock),
     site(_site)
{
}

这样,您就可以在派生类的构造函数中指定如何构建它所依赖的固有类的对象

这个问题

解决了,谢谢:)

class Person
{
public:
std::string m_strName;
int m_nAge;
bool m_bIsMale;
std::string GetName() { return m_strName; }
int GetAge() { return m_nAge; }
bool IsMale() { return m_bIsMale; }
void print() const;
Person(std::string strName = "", int nAge = 0, bool bIsMale = false)
    : m_strName(strName), m_nAge(nAge), m_bIsMale(bIsMale)
{
}
};
class BaseballPlayer : public Person
{
public:
double m_dBattingAverage;
int m_nHomeRuns;
BaseballPlayer(double dBattingAverage = 0.0, int nHomeRuns = 0)
   : m_dBattingAverage(dBattingAverage), m_nHomeRuns(nHomeRuns)
{
}
};