不同数据类型的数组

Array of different data types

我需要存储一系列报纸和小册子 然后在一个循环中,打印出打印每个产品需要多少纸,但是当我调用价格计算函数时,该函数是从主 class 而不是从子函数调用的,我怎么能调用来自子项的函数 classes 在具有不同对象的数组中?

#include <iostream>
using namespace std;

class PrintedProduct
{
public:
    string name;
    int pages;
    int paperCost()
    {
        return 1;
    }
    PrintedProduct() {}
    PrintedProduct(string name, int pages)
    {
        this->name = name;
        this->pages = pages;
    }
};

class NewsPaper : public PrintedProduct
{
public:
    int pageSize;
    int quantity;
    int period;
    NewsPaper(string name, int pages, int pageSize, int quantity, int period) : PrintedProduct(name, pages)
    {
        this->pageSize = pageSize;
        this->quantity = quantity;
        this->period = period;
    }
    int paperCost()
    {
        return pages * pageSize * quantity * period;
    }
};

class Booklet : public PrintedProduct
{
public:
    int pageSize;
    int quantity;
    Booklet(string name, int pages, int pageSize, int quantity)
        : PrintedProduct(name, pages)
    {
        this->pageSize = pageSize;
        this->quantity = quantity;
    }
    int paperCost()
    {
        return pages * pageSize * quantity;
    }
};

void print(PrintedProduct a)
{
    cout << a.paperCost() << endl;
};

int main()
{
    // Write C++ code here
    size_t size;
    cout << "Please, enter a size of the array: " << endl;
    cin >> size;
    PrintedProduct *array = new PrintedProduct[size];

    cout
        << "Please, enter "
        << size
        << " Printed products: ";
    for (size_t i = 0; i < size; i++)
    {
        cout << "Please enter: 1. Newspaper or 2. Booklet";
        int type;
        cin >> type;
        if (type == 1)
        {
            cout << "Enter name:" << endl;
            string name;
            cin >> name;

            cout << "Enter page count" << endl;
            int pages;
            cin >> pages;

            cout << "Enter page size " << endl;
            int pageSize;
            cin >> pageSize;

            cout << "Enter quantity" << endl;
            int quantity;
            cin >> quantity;

            cout << "Enter period" << endl;
            int period;
            cin >> period;

            array[i] = NewsPaper(name, pages, pageSize, quantity, period);
        }
        else
        {
            cout << "Enter name:";
            string name;
            cin >> name;

            cout << "Enter page count";
            int pages;
            cin >> pages;

            cout << "Enter page size";
            int pageSize;
            cin >> pageSize;

            cout << "Enter quantity";
            int quantity;
            cin >> quantity;

            array[i] = Booklet(name, pages, pageSize, quantity);
        }
    }

    for (size_t j = 0; j < size; j++)
    {
        print(array[j]);
    }
    delete[] array;

    std::system("PAUSE");
    return EXIT_SUCCESS;
}

I need to store an array of newspapers and booklets

这是不可能的。数组只能包含一种类型的对象。在 new PrintedProduct[size] 的情况下,该数组包含 PrintedProduct 类型的对象。数组的元素不是从 PrintedProduct.

派生的 classes 的实例

解决这个问题的方法是使用间接寻址。您可以创建指向 PrintedProduct 的指针数组。这些指针可能指向派生 classes 的基础子对象。在下面的示例中,我将使用 std::vectorstd::unique_ptr 以避免拥有裸指针:

std::vector<std::unique_ptr<PrintedProduct>> array(size);
...
array[i] = std::make_unique<NewsPaper>(
    name, pages, pageSize, quantity, period);

how can I call the function from the child classes

您可以通过覆盖虚函数从派生的 class 调用成员函数。示例:

class PrintedProduct
{
public:
    virtual int paperCost() = 0;
// ...

class NewsPaper : public PrintedProduct
{
public:
    int paperCost() override
    {
        return pages * pageSize * quantity * period;
    }
// ...
a.paperCost(); // virtual dispatch