2013-10-16 50 views
0

我有下一個程序,並且我有4個類: Cake,PiesandSaltyPastries,IceCreamandDesserts和所有這些類的基類: CBakery。 我需要一個數組包含從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; 

     } 
    } 

} 
+0

解釋「它不工作」 – codeling

+0

不把它編譯?你試過「買[1] = &cake;」嗎? –

+0

還有。蛋糕是一種CBakery嗎?如果不是,請不要錯誤地使用繼承。 – idipous

回答

2

既然你沒有說明是什麼不是你的程序工作,我隨意挑選一件,並且呈現出可能的錯誤:

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

這件作品的代碼將創建一個名爲cake的本地Cake對象,將調用其上的InputCake方法,然後將bought[i]中插入一個完全不同的,新的動態分配的Cake類型對象。

試試這個:

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

實際初始化並插入相同的對象。

+0

謝謝,我給他們打了所有的輸入輸入,並且我修復了線類Cake:CBakery,並添加了公共。 但現在它告訴我: 「錯誤錯誤LNK2001:無法解析的外部符號 」市民:虛擬無效__thiscall CBakery :: F(無效)「(·F @ CBakery @@ UAEXXZ)」 – Boris

+0

你需要什麼的'虛擬無效f()'無論如何,如果它似乎被稱爲無處?你有沒有它,這就是編譯器抱怨這裏 – codeling

+0

好的,謝謝,這是我忘了刪除,其他的事情,當它到達蛋糕的錯誤 - >輸入();它不要求任何輸入(它只是循環) – Boris