【问题标题】:How to make an array contain a class inherited from a base class [closed]如何使数组包含从基类继承的类[关闭]
【发布时间】:2013-10-16 11:53:21
【问题描述】:

我有下一个程序,我有 4 节课: Cake、PiesandSaltyPastries、IceCreaandDesserts 和所有这些类的基类: 面包店。 我需要使一个数组包含从 CBakery 继承的类,例如我尝试过:

CBakery *bought[6]; //Bought food 

Cake cake;
cake.InputCake(); //To fill it with input
bought[1] = new Cake;
bought[1] = cake;

但它不起作用,这里有完整的代码,如果你能帮助我,我将非常感激。

编辑:

#include "stdafx.h"
#include <iostream>

using namespace std;


class  CBakery
{
public:

    virtual void f(void);

    virtual void Input()
    {
        int freeze;
        cout << "Enter Product Name, Type, Price, if it is freezable(y-1 n-2), and expiration time in days\n";
        cin >> ProductName >> ProductType;
        cin >> ProductPrice;

        cin >> freeze;
        if(freeze == 1)
            freezable = true;
        else
            freezable = false;

        cin >> ExpirationTime;

    }
    virtual void Show(void){        
        cout << "Product's Name: " << ProductName;

        cout << "Product's Type: " << ProductType << "\nProduct's Price: " << ProductPrice << "\nIs Freezable? :";

        if(freezable)
            cout << "Yes\n";
        else
            cout << "Not\n";

        cout << "Expiration time (In days): " << ExpirationTime << endl;

    }

private:

    char *ProductName;
    char ProductType; //1-Salty Pastry. 2-Sweet pastry. 3-Non baked dessert.
    float ProductPrice; // To the customer
    bool freezable; 
    int ExpirationTime; //In days
};

class Cake : public CBakery
{
public:

    void Input()
    {
        CBakery::Input();
        int cream;
        cout << "Does it have cream?\n (1-y, 2-n)\n";
        cin >> cream;

        if(cream = 1)
            Cream = true;
        else
            Cream = false;

        cout << "What is the diameter?(22cm - 24cm - 26cm.....)\n What is the ammount of days to reserve the cake\n";

        cin >> Diameter >> daysToReserve;

    }
    void showCake()
    {
        CBakery::Show();
        cout << "Does it have Cream?\n";
        if (Cream)
            cout << "Yes\n";
        else
            cout << "No\n";

        printf("The Diameter is: %s \n Days needed to reserve the cake are: %s\n", Diameter, daysToReserve);


    }

private:
    bool Cream; 
    char *Diameter; //( 22 cm, 24 cm, 26 cm) Not square cakes.
     char *daysToReserve; 

};

class PiesandSaltyPastries : public CBakery
{
public:
    void Input()
    {
        CBakery::Input();

        int weight, paste;

        cout << "Sold based on weight? (y = 1, n = 2), Contains paste (y = 1, n = 2)?\n";
        cin >> weight >> paste;
        if(weight = 1)
            soldBasedonWeight = true;
        else
            soldBasedonWeight = false;

        if(paste = 1)
            containsPaste = true;
        else
            containsPaste = false;
    }
    void showSalty()
    {
        CBakery::Show();

        cout << "Is it sold based on weight or unities?\n";
        if(soldBasedonWeight)
            cout << "Based on weight\n";
        else
            cout << "Based on unities\n";
    }

private:
    bool soldBasedonWeight; //Weight = true, unity = false.
    bool containsPaste;
};

class IceCreamandDesserts : public CBakery
{
public:
    void Input()
    {
        int natural, milk;
        CBakery::Input();
        cout << "How many calories per unity, is it natural(Y = 1, N = 2) ingredients only?, does it contain milk(Y = 1, N = 2)?\n";

        cin >> caloriesPerUnity;

        cin >> natural >> milk;

        if(natural = 1)
            naturalIngredientsOnly = true;
        else
            naturalIngredientsOnly = false;


        if(milk = 1)
            containsMilk = true;
        else
            containsMilk = false;

    }
    void showDesserts()
    {
        CBakery::Show();
        printf("Calories per unity: %s\n", caloriesPerUnity);

        cout << "Does it have natural ingredients only? \n";
        if(naturalIngredientsOnly)
            cout << "Yes";
        else
            cout << "No";

        cout << "Does it contain milk? \n";
        if(containsMilk)
            cout << "Yes";
        else
            cout << "No";
    }

private:
    char *caloriesPerUnity;
    bool naturalIngredientsOnly;
    bool containsMilk;
};

void main()
{
    CBakery *bought[6];
    char category;


    for (int i = 0; i < 6; i++)
    {
        cout << "Is it a cake(1), pies or salty pastry(2), or Ice cream and desserts?(3)\n";
        cin >> category;

        switch(category)
        {
        case 1:

            Cake * cake = new Cake;
            cake -> Input();
            bought[i] = cake;


            break;

        }
    }

}

【问题讨论】:

  • 解释“它不起作用”
  • 不能编译吗?你试过用“bought[1] = &cake;”吗?
  • 还有。 Cake是一种CBakery吗?如果没有,请不要说您错误地使用了继承。
  • 为什么不使用std::vector&lt;CBakery&gt; 而不是处理固定大小的数组(和指针!)?
  • @crashmstr 会发生对象切片,您需要为多态性提供额外的间接。如果您愿意,可以使用智能指针进行间接寻址。

标签: c++ class inheritance polymorphism


【解决方案1】:

由于你没有说明你的程序有什么不好的地方,我随机挑选一个并显示一个可能的错误:

        Cake cake;
        cake.InputCake();
        bought[i] = new Cake;

这段代码将创建一个名为cake的本地Cake对象,将在其上调用InputCake方法,然后将一个完全不同的、新的、动态分配的Cake类型的对象插入@ 987654326@.

试试这个:

case 1:
{
    Cake * cake = new Cake;
    cake->Input();
    bought[i] = cake;
    break;
}

实际初始化并插入同一个对象。

【讨论】:

  • 谢谢,我把所有的输入都叫作Input,我修复了行类Cake : CBakery,并添加了public。但现在它告诉我:“错误 1 ​​error LNK2001: unresolved external symbol "public: virtual void __thiscall CBakery::f(void)" (?f@CBakery@@UAEXXZ)”
  • 如果virtual void f() 似乎无处被调用,您还需要什么?而你没有实现它,这就是编译器在这里抱怨的错误
  • 是的,谢谢,那是我忘记删除的东西,其他的,当它到蛋糕时 -> Input();它不要求任何输入(它只是循环 for)
  • 尝试使用调试器逐步完成它。我认为更有可能您的 switch case 1 甚至没有被输入
  • 它还说:错误 1 ​​错误 C2360:“case”标签跳过了“cake”的初始化。
猜你喜欢
  • 2019-05-14
  • 2019-02-23
  • 1970-01-01
  • 2019-12-29
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 2016-06-05
  • 2014-03-13
相关资源
最近更新 更多