I have a lot of problems.

So basically, our professor told us to do:
Problem 1: Create a class for the fruits and the vegetables. Each class must have a constructor, deconstructor, copy constructor and copy assignment operator. They must also have all relevant attributes(such as name, price and quantity) and functions (such as calculate sum) as presented in the problem description above.

Problem 2: Create an array GroceryList in the driver code that will contain all items in Jenna’s Grocery List. You must then access each saved instance and display all details about the items.

However,

case 1: I was not able to create a copy assignment operator that works and does not break the code. Any ideas on why?

case 2: I wanna use the previous data created in the two separate arrays (Fruit, and Vegetable) and merge them together in a single array which is the Grocery thingy, I even made a new Grocery class to do that but I think it's useless. Whenever I try to run the program, it runs, and the console shows some kind of error. Anyone know what I was doing wrong?

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
  #include <iostream>
#include <string.h>

class Fruit
{
    private:
        std::string fruitName;
        int fruitPrice;
        int fruitQuantity;

    public:
        //Constructor
        Fruit(std::string newFruitName = "", int newFruitPrice = 0, int newFruitQuantity = 0)
        {
            fruitName = std::move(newFruitName);
            fruitPrice = newFruitPrice;
            fruitQuantity = newFruitQuantity;
            //std::cout << "Constructor Called." << std:: endl; // To check if its working
        }

        //Deconstructor
        ~Fruit ()
        {
            //std::cout << "Destructor Called." << std::endl; // To check if its working
        }

        //Copy Constructor
        Fruit (const Fruit &copyFruit)
        {
            //std::cout << "Copy Constructor Called." << std::endl; // To check if its working
            fruitName = copyFruit.fruitName;
            fruitPrice = copyFruit.fruitPrice;
            fruitQuantity = copyFruit.fruitQuantity;
        }

        //Copy Assignment Operator
        /*Fruit& operator = (Fruit&)
        {
            //std::cout << "Copy Assignment Operator was called." << std::endl;
            return *this;
        }*/

        //Display Attributes
        void printFruitDetails ()
        {
            std::cout <<"\t"<< this->fruitName << "\t\t " << this->fruitPrice << "\t " << this->fruitQuantity << std::endl;
        }
};

class Vegetable
{
    private:
        std::string vegName;
        int vegPrice;
        int vegQuantity;

    public:
        //Constructor
        Vegetable (std::string newVegName = "Broccoli", int newVegPrice = 60, int newVegQuantity = 12)
        {
            vegName = std::move(newVegName);
            vegPrice = newVegPrice;
            vegQuantity = newVegQuantity;
            //std::cout<< "Constructor Called." << std::endl; // To check if its working
        }

        //Deconstructor
        ~Vegetable ()
        {
            //std::cout << "Destructor Called." << std::endl; // To check if its working
        }

        //Copy Constructor
        Vegetable (const Vegetable &copyVegetable)
        {
            //std::cout << "Copy Constructor Called." << std::endl;
            vegName = copyVegetable.vegName;
            vegPrice = copyVegetable.vegPrice;
            vegQuantity = copyVegetable.vegQuantity;
        }

        // Copy Assignment Operator
        /*Vegetable &operator = (const Vegetable &copy)
        {
            //std::cout << "Copy Assignment Operator was called." << std::endl;
            return *this;
        }*/


        //Display Attributes
        void printVegDetails()
        {
            std::cout <<"\t"<< this->vegName << "\t\t " << this->vegPrice << "\t " << this->vegQuantity << std::endl;
        }


};

class Grocery : public Vegetable, public Fruit
{
    private:
        int GrocList_count;
        std::string GoodName;
        int GoodPrice, GoodQuantity;
    public:
        Grocery (std::string GoodNamey="", int GoodPricey=0, int GoodQuantityy=0)
        {
            GoodName=std::move(GoodNamey);
            GoodPrice=GoodPricey;
            GoodQuantity=GoodQuantityy;
        }
        ~Grocery(){};

        Grocery (const Grocery &copyGood)
        {
            GoodName = copyGood.GoodName;
            GoodPrice = copyGood.GoodPrice;
            GoodQuantity = copyGood.GoodQuantity;
        }

        void printGroceryDetails()
        {
            std::cout <<"\t"<< this->GoodName << "\t\t " << this->GoodPrice << "\t " << this->GoodQuantity << std::endl;
        }
};

// Function to sum

int main () {
	// driver program
	const size_t j=2;
	const size_t k=4;



	std::cout <<"\t"<< "Good(s)" << "\t\t " << "Price" << "\t " << "Quantity" << std::endl;

	// Fruits
	Fruit fruitlist_fin[j]={};
	std::string fruitNameList[j] = {"Apple", "Banana"};
	int priceFruitList[j] = {10, 10};
	int quantityFruitList[j] = {7, 8};

    for(int i =0; i < j; i++)
        {
            Fruit *ptr = new Fruit (fruitNameList[i], priceFruitList[i], quantityFruitList[i]);
            fruitlist_fin[i]= *ptr;
        }
        for(int i = 0; i < j; i++)
        {
            fruitlist_fin[i].printFruitDetails();
        }

	// Vegetables
	Vegetable veglist_fin[j]={};
	std::string vegNameList[j] = {"Broccoli", "Lettuce"};
	int priceVegList[j] = {60, 50};
	int quantityVegList[j] = {12, 10};

    for(int i = 0; i < j; i++)
    {
		Vegetable *ptr = new Vegetable(vegNameList[i], priceVegList[i], quantityVegList[i]);
		veglist_fin[i]= *ptr;
	}
	for(int i = 0; i < j; i++)
    {
        veglist_fin[i].printVegDetails();
    }

    //Goods List

    Grocery GroceryList_fin[k]={};
    for(int i = 0; i < j; i++)
    {
        veglist_fin[i]=GroceryList_fin[i];
    }
    for(int i = 0; i < j; i++)
    {
        fruitlist_fin[i]=GroceryList_fin[i+3];
    }



    for(int i = 0; i < k; i++)
    {
        GroceryList_fin[i].printGroceryDetails();
    }

    /*
    for (int i=1; i<j+2; i++)
    {
        Vegetable *ptr1 = new Vegetable(vegNameList[i], priceVegList[i], quantityVegList[i]);
        Fruit *ptr2 = new Fruit (fruitNameList[i], priceFruitList[i], quantityFruitList[i]);
        GroceryList_fin[i-1]= *ptr1;
        GroceryList_fin[i]  = *ptr2;
    }
    */
}



P.S. I'm just a beginner, my brain can't cope up with complex commands, even this one is already quite complex for me.
They must also have all relevant attributes(such as name, price and quantity) and functions (such as calculate sum) as presented in the problem description above.

We can't see "above".


#include <string.h>

On your other thread you were guided to not use <string.h>. In C++ the std::string is defined in <string>.
If your compiler does not have <string>, then it is way too old.


class Grocery : public Vegetable, public Fruit

A Grocery IS-A Vegetable AND IS-A Fruit according to that. That is not true, is it?

However, since you do try to use inheritance, are you already on chapter about inheritance? If yes, then perhaps you could have Vegetable IS-A Grocery and Fruit IS-A Grocery?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Grocery
{
    std::string name;
    int price;
    int quantity;
public:
    // interface
};

class Fruit : public Grocery {
public:
    // interface
};

class Vegetable : public Grocery {
public:
    // interface
};
For Fruit and Vegetable classes consider:

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
#include <iostream>
#include <string>
#include <utility>
#include <vector>

class Fruit {
private:
	std::string fruitName;
	int fruitPrice {};
	int fruitQuantity {};

public:
	Fruit(std::string newFruitName = "", int newFruitPrice = 0, int newFruitQuantity = 0) :
		fruitName(std::move(newFruitName)), fruitPrice(newFruitPrice), fruitQuantity(newFruitQuantity) {}

	~Fruit() {}

	Fruit(const Fruit& copyFruit) : Fruit(copyFruit.fruitName, copyFruit.fruitPrice, copyFruit.fruitQuantity) {}
	Fruit(Fruit&& copyFruit) noexcept : Fruit(std::move(copyFruit.fruitName), copyFruit.fruitPrice, copyFruit.fruitQuantity) {}

	Fruit& operator=(Fruit f) noexcept {
		std::swap(f.fruitName, fruitName);
		std::swap(f.fruitPrice, fruitPrice);
		std::swap(f.fruitQuantity, fruitQuantity);
		return *this;
	}

	void printFruitDetails() const {
		std::cout << "\t" << this->fruitName << "\t\t " << this->fruitPrice << "\t " << this->fruitQuantity << std::endl;
	}
};

class Vegetable {
private:
	std::string vegName;
	int vegPrice {};
	int vegQuantity {};

public:
	Vegetable(std::string newVegName = "Broccoli", int newVegPrice = 60, int newVegQuantity = 12) :
		vegName(std::move(newVegName)), vegPrice(newVegPrice), vegQuantity(newVegQuantity) {}

	~Vegetable() {}

	Vegetable(const Vegetable& copyVegetable) : Vegetable(copyVegetable.vegName, copyVegetable.vegPrice, copyVegetable.vegQuantity) {}
	Vegetable(Vegetable&& copyVegetable) noexcept : Vegetable(std::move(copyVegetable.vegName), copyVegetable.vegPrice, copyVegetable.vegQuantity) {}

	Vegetable& operator=(Vegetable copy) noexcept {
		std::swap(copy.vegName, vegName);
		std::swap(copy.vegPrice, vegPrice);
		std::swap(copy.vegQuantity, vegQuantity);
		return *this;
	}

	void printVegDetails() const {
		std::cout << "\t" << this->vegName << "\t\t " << this->vegPrice << "\t " << this->vegQuantity << std::endl;
	}
};

/*
class Grocery : public Vegetable, public Fruit {
private:
	int GrocList_count {};
	std::string GoodName;
	int GoodPrice {}, GoodQuantity {};

public:
	Grocery(std::string GoodNamey = "", int GoodPricey = 0, int GoodQuantityy = 0) {
		GoodName = std::move(GoodNamey);
		GoodPrice = GoodPricey;
		GoodQuantity = GoodQuantityy;
	}
	~Grocery() {};

	Grocery(const Grocery& copyGood) {
		GoodName = copyGood.GoodName;
		GoodPrice = copyGood.GoodPrice;
		GoodQuantity = copyGood.GoodQuantity;
	}

	void printGroceryDetails() {
		std::cout << "\t" << this->GoodName << "\t\t " << this->GoodPrice << "\t " << this->GoodQuantity << std::endl;
	}
};
*/

int main() {
	const size_t j { 2 };
	//const size_t k { 4 };

	std::cout << "\t" << "Good(s)" << "\t\t " << "Price" << "\t " << "Quantity\n";

	const std::string fruitNameList[j] { "Apple", "Banana" };
	const int priceFruitList[j] { 10, 10 };
	const int quantityFruitList[j] { 7, 8 };
	std::vector<Fruit> fruitlist_fin;

	for (int i {}; i < j; ++i)
		fruitlist_fin.emplace_back(fruitNameList[i], priceFruitList[i], quantityFruitList[i]);

	for (const auto& f : fruitlist_fin)
		f.printFruitDetails();

	const std::string vegNameList[j] { "Beans", "Lettuce" };
	const int priceVegList[j] { 60, 50 };
	const int quantityVegList[j] { 12, 10 };
	std::vector<Vegetable> veglist_fin;

	for (int i {}; i < j; ++i)
		veglist_fin.emplace_back(vegNameList[i], priceVegList[i], quantityVegList[i]);

	for (const auto& v : veglist_fin)
		v.printVegDetails();

	/*
	Grocery GroceryList_fin[k] {};

	for (int i = 0; i < j; i++)
		veglist_fin[i] = GroceryList_fin[i];

	for (int i = 0; i < j; i++)
		fruitlist_fin[i] = GroceryList_fin[i + 3];

	for (int i = 0; i < k; i++)
		GroceryList_fin[i].printGroceryDetails();
	*/
}



       Good(s)          Price   Quantity
       Apple            10      7
       Banana           10      8
       Beans            60      12
       Lettuce          50      10


What is supposed to be done with Grocery class???

As Vegetable and Fruit classes are the same, why do you need both?
Last edited on
It might be helpful to see the ENTIRE assignment instead of your interpretation, Silver29. You mention parts of the assignment you don't show us.
Do you mean something like:

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
#include <iostream>
#include <string>
#include <utility>
#include <vector>

class Grocery {
private:
	std::string grocName;
	int grocPrice {};
	int grocQuantity {};

public:
	Grocery(std::string newgrocName = "", int newgrocPrice = 0, int newgrocQuantity = 0) :
		grocName(std::move(newgrocName)), grocPrice(newgrocPrice), grocQuantity(newgrocQuantity) {}

	virtual ~Grocery() {}

	Grocery(const Grocery& copygroc) : Grocery(copygroc.grocName, copygroc.grocPrice, copygroc.grocQuantity) {}
	Grocery(Grocery&& copygroc) noexcept : Grocery(std::move(copygroc.grocName), copygroc.grocPrice, copygroc.grocQuantity) {}

	Grocery& operator=(Grocery f) noexcept {
		std::swap(f.grocName, grocName);
		std::swap(f.grocPrice, grocPrice);
		std::swap(f.grocQuantity, grocQuantity);
		return *this;
	}

	void printDetails() const {
		std::cout << "\t" << this->grocName << "\t\t " << this->grocPrice << "\t " << this->grocQuantity << std::endl;
	}

	virtual void print() const {}
};

class Vegetable : public Grocery {
	using Grocery::Grocery;

	void print() const override {
		std::cout << "V ";
		printDetails();
	}
};

class Fruit : public Grocery {
	using Grocery::Grocery;

	void print() const override {
		std::cout << "F ";
		printDetails();
	}
};

int main() {
	const size_t j { 2 };

	const std::string fruitNameList[j] { "Apple", "Banana" };
	const int pricefruitList[j] { 10, 10 };
	const int quantityfruitList[j] { 7, 8 };

	const std::string vegNameList[j] { "Beans", "Lettuce" };
	const int priceVegList[j] { 60, 50 };
	const int quantityVegList[j] { 12, 10 };

	std::vector<Grocery*> groclist_fin;

	for (int i {}; i < j; ++i)
		groclist_fin.push_back(new Fruit(fruitNameList[i], pricefruitList[i], quantityfruitList[i]));

	for (int i {}; i < j; ++i)
		groclist_fin.push_back(new Vegetable(vegNameList[i], priceVegList[i], quantityVegList[i]));

	std::cout << "\t" << "Good(s)" << "\t\t " << "Price" << "\t " << "Quantity\n";

	for (const auto& v : groclist_fin)
		v->print();

	for (auto& v : groclist_fin)
		delete v;
}



        Good(s)          Price   Quantity
F       Apple            10      7
F       Banana           10      8
V       Beans            60      12
V       Lettuce          50      10

The problem here is illiteracy more than anything:

Create a class for the fruits and the vegetables. (A CLASS MEANS ONE). Each class must have a constructor (EACH MEANS MORE THAN ONE). (WHICH IS IT???).

the problem BEGS for ONE class here. A fruit, a veg, a bag of chicken or bucket of chitlins, whatever it is, its something they sell that has a price and a quantity. If you need to specialize something in there, figure out what it is and virtual it.

Create an array GroceryList in the driver code that will contain all items in Jenna’s Grocery List.
This is really simple looking requirements...
class items...
items jennas[1000];

if you only have 1 class, you don't have the problem of veg arrays and fruit arrays and chitlin arrays.
If you do need more than one class, polymorphism will let you make a container to store all the types together, but that is a large hammer for a small nail here.
Last edited on
Create a class for the fruits and the vegetables. (A CLASS MEANS ONE).
Probably the sentence was meant to be parsed as, "Create a class [for the fruits] [and (a class for) the vegetables]." I agree it's not the best grammar.
Last edited on
"Create a class of vegetables ..."

It's been a long day: I just misread that!
Topic archived. No new replies allowed.