polymorphism

i am writing this code which involves an eatery type class and 2 derived classes
snackcart and restaurant. how can i implement polymorphism in this? i want to make use of dynamic objects.

eatery.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef EATERY_H
#define EATERY_H
#include <string>
#include <vector>
#include "meals.h"
using std::string;
using std::vector;

class Eatery {
    public:
		Eatery();
		~Eatery();
        virtual void setOwner(string name) = 0;
        virtual string getOwner() = 0;
	
	protected:
		string owner;
		Meal *meals;
		int size;
		
};
#endif 

snackcart.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef _SNACKCART
#define _SNACKCART
#include "eatery.h"

#include <string>
#include <vector>
using std::vector;
using std::string;


class SnackCart: public Eatery {
    public:
        SnackCart();
		~SnackCart();
		void setOwner(string name);
		string getOwner();

        void setLocation(string location);
        string getLocation();
		
		void setMealArray(int);

        void clearMeals();
        void addMeal(string name, float cost,string ratingStr);
		void loadaddMeal(string name, float cost,string ratingStr);
        int mealCount();

        int getMealNames(string names[]);
        float getMealCost(string name);
        string getMealRatingForName(string name);
        string getRatedMeal(string ratingStr);
        RatingType getRating(string str);

        void adjustMealCost(string name, float cost);
        void adjustMealRating(string name, RatingType rating);

        string cheapestMeal();
        string mostPopularMeal();

        void displayCartDetails();

    private:
		vector<Meal> meal;
        int findMeal(string name);
        string location;
        int count;
};

#endif 


restaurant.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "eatery.h"
#ifndef RES_H
#define RES_H

class Restaurant: public Eatery
{
	public:
		Restaurant();
		~Restaurant();

        void setResName(string name);
        string getResName();

        void setAddress(string address);
        string getAddress();

		void setPhoneNo(int phone);
		int getPhoneNo();

		void setOpeningHour(string openHr);
		string getOpeningHr();

		void setMealArray(int);
		
        void clearMeals();
        void addMeal(string name, float cost,string ratingStr = "Unknown");
        int mealCount();

        int getMealNames(string names[]);
        float getMealCost(string name);
        string getMealRatingForName(string name);
        string getRatedMeal(string ratingStr);
        RatingType getRating(string str);

        void adjustMealCost(string name, float cost);
        void adjustMealRating(string name, RatingType rating);

        string cheapestMeal();
        string mostPopularMeal();

        void displayRestaurantDetails();

    private:
		vector<Meal> meal;
        int findMeal(string name);
        string address;
        int count;
	
};

snackcart.cpp
[code]
#include "snackcarts.h"

#include <iostream>
#include <vector>
using std::vector;
using std::cout;
using std::cin;
using std::endl;

SnackCart::SnackCart() : Eatery(){
	meal.resize(100);
	owner = "Unset";
    location = "Unknown";
    count = 0;
}

void SnackCart::setOwner(string name) {
    owner = name;
}

string SnackCart::getOwner() {
	return owner;
}

void SnackCart::setMealArray(int size_of_array)
{
	size = size_of_array;
	meal.resize(size);
}

void SnackCart::setLocation(string name) {
    location = name;
}

void SnackCart::clearMeals() {
    count = 0;
}

void SnackCart::addMeal(string name, float cost,string ratingStr) {
    
    if (count == size) {
        cout << "Done." << endl;
        return;
    }

    meal[count].setName(name);
    meal[count].setCost(cost);
    meal[count].setRating(Meal::ratingType(ratingStr));
    ++count;
}


void SnackCart::loadaddMeal(string name, float cost,string ratingStr) {
    
	meal[count].setName(name);
    meal[count].setCost(cost);
    meal[count].setRating(Meal::ratingType(ratingStr));
    ++count;
}

int SnackCart::findMeal(string name) {
    if (count == 0) { // empty list
        return size; // force return value to be bigger than count
    }
    int i;
    for (i = 0; i < count; ++i) {
        if (meal[i].getName() == name) {
            break;
        }
    }
    return i;
}

void SnackCart::adjustMealCost(string name, float cost) {
    int i = findMeal(name);
    if (i < count) { // found a matching name
        meal[i].setCost(cost);
    } else {
        cout << "Error: invalid meal name for this cart" << endl;
    }
}

void SnackCart::adjustMealRating(string name, RatingType rating) {
    int i = findMeal(name);
    if (i < count) { // found a matching name
        meal[i].setRating(rating);
    } else {
        cout << "Error: invalid meal name for this cart" << endl;
    }
}


string SnackCart::getLocation() {
    return location;
}

int SnackCart::mealCount() {
    return count;
}

int SnackCart::getMealNames(string names[]) {
    for (int i = 0; i < count; ++i) {
        names[i] = meal[i].getName();
    }
    return count;
}

float SnackCart::getMealCost(string name) {
    return meal[findMeal(name)].getCost();
}

string SnackCart::getMealRatingForName(string name) {
    return meal[findMeal(name)].getRatingStr();
}

string SnackCart::cheapestMeal() {
    if (count == 0) {
        return "Unknown";
    }
    float min = meal[0].getCost();
    int minIndex = 0;
    int i = 1;
    while (i < count) {
        float value = meal[i].getCost();
        if (value < min) {
            min = value;
            minIndex = i;
        }
        ++i;
    }

    return meal[minIndex].getName();
}

string SnackCart::getRatedMeal(string ratingStr) {
    string result = "Unknown";
    for (int i = 0; i < count; ++i) {
        if (ratingStr == meal[i].getRatingStr()) {
            result = meal[i].getName();
            break;
        }
    }
    return result;
}

string SnackCart::mostPopularMeal() {
    // of the highest rated meals, find one with the cheapest cost 

    // find highest rating
    RatingType maxRating = UNKNOWN;
    for (int i = 0; i < count; ++i) {
        RatingType value = meal[i].getRating();
        if (value > maxRating) {
            maxRating = value;
        }
    }

    // find indices of highest rated meals
	int *bestMeals = new int[size];
    int j = 0;
    for (int i = 0; i < count; ++i) {
        if (meal[i].getRating() == maxRating) {
            bestMeals[j++] = i;
        }
    }

    // find best meal with best price of the highest rated meals
    float minCost = meal[bestMeals[0]].getCost();
    int bestMeal = 0;
    for (int i = 1; i < j; ++i) {
        float cost = meal[bestMeals[i]].getCost();
        if (cost < minCost) {
            minCost = cost;
            bestMeal = i;
        }
    }
    return meal[bestMeals[bestMeal]].getName();
	delete [] bestMeals;

}

void SnackCart::displayCartDetails() {
    cout << "Owner: " << owner << endl
        << "Where: " << location << endl;
   cout << "Offers " << count << " different meals" << endl;

   
}

SnackCart::~SnackCart()
{
	if (meals != NULL) {
		delete [] meals;
	}
}



#endif[/code]

You've already implemented polymorphism via derived classes and virtual functions, so I don't understand
the question.

[Just looking at your code, though, some of what you've done does not make sense.]

the thing is in my main function i have not created a snackcart object as a pointer. it have created it as a vector. does vector have its own destructor? and which part of the code doesnt make sense? i think the *meals is not required and neither is the destructor. is that it?
vector<> has its own destructor that runs the destructor of all contained elements.


Well, for example, the base class Eatery has a string owner member, but the getter and setter
for that member is pure virtual. And Eatery has a Meal* and a size (an array?), yet all the
derived classes have vectors of Meals. Do the derived classes need both their own vector of
Meals AND an inherited array of Meals?



I get what you mean. I have changed my code accordingly. But without the virtual functions and the meals* is this program still using dynamic objects?
It's still using derivation, but not really polymorphism.
ok thank you :)
Topic archived. No new replies allowed.