Polymorphism
-
Hallo alle zusammen,
habe hier eine Aufgabe, die ich gern lösen möchte. Irgendwie habe ich überhaupt keinen punkt wie ich anfangen soll...bin hier ein Anfänger was cpp angeht...würde mich freuen wenn mir jemand mal helfen könnte.
Aufgabe:
Consider a simple shopping interface where users can order items in 2 categories: Flower (Rose and Daisy) and Gourmet (FruitBasket and CookieBasket). In this system, all the items have following common properties:
- price of the item (double)
- calculate_price() method: to calculate the price of the item
- print() method: to print information about the item
Flower objects have a common property artificial (bool) for specifying if the ordered flower is artificial or fresh. The price of an artificial flower is 1.5 times of its fresh version. Roses have a number (int) attribute for storing the number of ordered roses and the price is 10 TL for each fresh rose. If at least 10 roses are ordered, the unit price becomes 8 TL. Daisies are only sold in a bunch (without a number attribute) and the price is 20 TL for a bunch of fresh daisies.
Gourmet objects have two attributes in common: basket_size (enum {small, medium, large}) for specifying the size of the item and promotion (bool) to determine if there is a promotion with the order or not. FruitBaskets have fruitmix_type (enum: standard, citrus, tropical) for the type of fruits and with_chocolate_sauce (bool) to specify if chocolate sauce will be added or not for an additional cost of 20 TL. The price list of a FruitBasket for different types and sizes is as follows:----------Standard----Citrus----Tropical
Small--------25---------40---------50
Medium------50---------80---------100
Large--------75---------120--------150CookieBasket objects do not have different types and the price is determined by only using the basket size as, 30 TL for a small, 50 TL for a medium and 70 TL for a large basket of cookies. A bunch of daisies are given free as a promotion, when the price of the FruitBasket is at least 80 TL or a large CookieBasket is ordered.
Zusätzlich ist noch eine Main-Funktion gegeben.
#include <iostream> #include "item.h" using namespace std; // max. 5 orders are allowed per session #define MAX_ORDER_PER_SESSION 5 int main(){ // an array of pointers to ordered items Item *orders[MAX_ORDER_PER_SESSION]; int item_count = 0; bool continue_shopping = true; char category, subcategory; // ask for order information in a loop until the user ends shopping or max limit is reached while(continue_shopping && item_count < MAX_ORDER_PER_SESSION){ cout << "-------------------------------------------------------------------------" << endl; cout << "Select category ([f] for flower, [g] for gourmet, [e] to end shopping): "; cin >> category; switch(category){ case 'f': bool is_artificial; cout << endl << "Fresh flower[0] or artificial flower[1]?: "; cin >> is_artificial; cout << endl << "Select subcategory ([r] for rose, [d] for daisy): "; cin >> subcategory; switch(subcategory){ case 'r': int amount; cout << endl << "Amount?: "; cin >> amount; orders[item_count++] = new Rose(is_artificial,amount); break; case 'd': orders[item_count++] = new Daisy(is_artificial); break; default: cout << endl << "Wrong choice!" << endl; } break; case 'g': int selected_size; cout << endl << "Size? ([0] for small/ [1] for medium / [2] for large): "; cin >> selected_size; cout << endl << "Select subcategory ([f] for fruitbasket, [c] for cookiebasket): "; cin >> subcategory; switch(subcategory){ case 'f': int selected_type; cout << endl << "Type? ([0] for standard mix/ [1] for citrus mix / [2] for tropical mix): "; cin >> selected_type; bool with_sauce; cout << endl << "Chocolate sauce? ([0] for no/ [1] for yes): "; cin >> with_sauce; orders[item_count++] = new FruitBasket(basketSize(selected_size),fruitType(selected_type),with_sauce); break; case 'c': orders[item_count++] = new CookieBasket(basketSize(selected_size)); break; default: cout << endl << "Wrong choice!" << endl; } break; case 'e': cout << endl << "Ending the shopping session" << endl; continue_shopping = false; break; default: cout << endl << "Wrong choice!" << endl; } cout << endl; } // Calculating price and checking promotions for the ordered itens and printing out if(item_count > 0){ cout << endl << "ORDER LIST" << endl; cout << "-------------------------------------------------------------------------" << endl; } for(int i=0; i<item_count; i++){ orders[i]->calculate_price(); orders[i]->print(); cout << endl; } // giving allocated space back for(int i=0; i<item_count; i++) delete orders[i]; return 0; }
-
Wenn das ein Tutorial ist, gibt's da wohl besseres - der code in main ist nicht optimal.
Du sollst wohl einfach die benötigten Klassen implementieren. Das interface ist item, also
class Item { double price; public: virtual double calculate_price() = 0; //zur Übung "- print() method: to print information about the item " diese print methode selbst deklarieren virtual ~Item(); };Dann halt von Item halt ableiten, dass du Flower und Gourmet als Klasse hast, und davon dann Rose, Daisy etc ableiten...
-
So hab hier mal was versucht
class Item { protected: double price; public: void setPrice(double price_in); double getPrice() const; virtual void calculate_price() const = 0; virtual void print() const{cout<<price;} } class Flower : public Item { protected: string artificial; public: void setArtifical(string& art_in); const string& getArtifical() const; Flower():Item(){cin>>artificial;} void print() const{Item::print();cout<<artificial;} void calculate_price() const{Item::print();cout<<artificial;} }; class Rose : public Flower{ private: int number; double amount; public: void setNumber(int nr_in); void setAmount(double amount_in);‚ int getName() const; double getAmount() const; Rose():Flower(){cin>>number; cin>>amount;} void print() const{Flower::print(); cout<<number;} void calculate_price() const{Flower::calculate_price();cout<<number; cout<<amount;} }; class Daisy : public Flower{ public: Daisy():Flower(); void print const{Flower::print();} void calculate_price() const{Flower::calculate_price();} }; class Gourmet : public Item{ private: string selected_size; public: void setSelected_size(con string& sSize_in); const string& getSelected_size() const; Gourmet():Item(){cin>>selected_size;} void print() const {Item::print(); cout<<selected_size;} void calculate_price() const {Item::calculate_price();cout<<selected_size;} }; class FruitBasket : public Gourmet{ private: double selected_type; double with_sauce; public: void setSelected_type(double st_in void setwith_sauce(double fmt_in); double getSelected_type const; double getwith_sauce const; FruitBasket():Gourmet(){cin>>selected_type; cin>>with_sauce;} void print() const {Gourmet::print(); cout<<selected_type; cout<<with_sauce;} void calculate_price() const {Gourmet::calculate_price();cout<<selected_type; cout<<with_sauce;} }; class CookieBasket : public Gourmet{ public: CookieBasket():Gourmet(); void print const{Gourmet::print();} void calculate_price() const{Gourmet::calculate_price();} };
-
Und?
-
wollte wissen, ob das so in ordnung ist und was ich noch zu ergänzen habe...
Bekomme komische Fehler angezeigt, wo ich nicht weiterkomme
-
Die da wären?
-
ChillStudent: Kuck Dir mal Zeile 87 an.
Kuck Dir sie noch genauer an, wenns nicht reicht.
-
nach ende von class Flower zeigt das programm mir en fehler mit : multiple types in one declaration
schalalalalaa: in der Zeile wird doch nur die Print-Methode von Gourmet ausgegeben??
Muss die Aufgabe bis 22 Uhr hochladen, wäre nett wenn ihr mir sagen könntet was genau falsch ist und was ich noch zu ergänzen habe.
-
Zeile 87.
-
Was genau soll denn an Zeile 87 nicht stimmen?
-
schau dir mal das Const an 86 und Zeile 35 ist auch was falsch, was sind den das für zeichen da am ende