How to change the derived class variable using the base class object?

Hello!

So i'm trying to make this Product purchase small little program.

And the Purchase is inherit from the Product, it all runs fine for now, except

im kinda lost how to make it so that, i can create a object for Purchase and use the derived class(purchase) function that adds 1 to the quantity.

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

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;


class Product {

private:
	string name;
	double price;
	double weight;

public:
	Product(string, double, double);

	void stock();

	void set_name(string N);
	void set_price(int P);
	void set_weight(int W);

	string get_name();
	double get_price();
	double get_weight();
};

class Purchase : public Product {
private:
	int quantity;
	int weight_bought;
public:

	Purchase();

	void buy();
	void set_quantity(int Q);
	void set_weightBought(int WB);

	int get_quantity();
	int get_weightBought();
};

class Verefication : public Purchase {

};

Product::Product(string n, double p, double w) {
	name = n;
	price = p;
	weight = w;

};

Purchase::Purchase() : Product(get_name(), get_price(), get_weight()){
	
}
int main()
{
	string choice;


	Product A("apple", 1.20, 0.5);
	Product B("pizza", 3.20, 1.2);
	Product C("olive oil", 2.10, 1);
	Product D("salami", 1.45, 1.25);
	Product E("BBQ Sauce", 2.35, 1.25);



	cout << "Hello to the interactive shopping center, would you like to see our stock ?(y/n) " << endl;
	cin >> choice;
	if (choice == "y")
	{
		A.stock();
		B.stock();
		C.stock();
		D.stock();
		E.stock();

		cout << "############################################" << endl;
		cout << "Please write the name of the product you would like to buy : " << endl;

		while (true) {
			cin >> choice;
			if (choice == A.get_name()) {
				cout << 1;

			}
			else if (choice == B.get_name()) {
				cout << 2;
			}
			else if (choice == C.get_name()) {
				cout << 3;
			}
			else if (choice == D.get_name()) {
				cout << 4;
			}
			else if (choice == E.get_name()) {
				cout << 5;
			}
		};

		system("Pause");
		return 0;

	}
}

void Product::set_name(string N) {
	name = N;
}

void Product::set_price(int P) {
	price = P;
}

void Product::set_weight(int W) {
	weight = W;
}

string Product::get_name() {
	return name;
}

double Product::get_price() {
	return price;
}

double Product::get_weight() {
	return weight;
}

void Purchase::set_quantity(int Q) {
	quantity = Q;
}

void Purchase::set_weightBought(int WB) {
	weight_bought = WB;
}

int Purchase::get_quantity() {
	return quantity;
}

int Purchase::get_weightBought() {
	return weight_bought;
}

void Product::stock() {
	cout << "Product Name: " << get_name() << " ";
	cout << "Product Price: " << get_price() << " leva ";
	cout << "Product Weight: " << get_weight() << endl;
}

void Purchase::buy() {
	quantity += 1;
}


I'm kinda lost.. I'm doing something wrong, maybe i wrong constructor for Purchase. I checked some stuff online that adviced a pointer, but i tried that and it didn't work well either.

In general i just want so that when he writes for example - "pizza" , purchase happens and he gets +1 quantity from the buy function inside the Purchase class and then display it all through the Verify class, but that is another deal.

Just kinda lost what im doing wrong.
Thanks in advance!
Last edited on
You say that every Verefication object is a Product.
You say that every Verefication object is a Purchase.
You say that every Purchase object is a Product.

Is that logical?

Would it not be more intuitive, if a Purchase has some amount of Products?


Even then, there are two paths.

1. One can have type and amount. The "stock" and "customer" would have amount for a product. A purchase of x apples would decrease the stock amount by x and increase the customer amount by x. Simple math.

2. Products are "real objects". Each apple is an individual. On purchase, select apples are moved from shopping centers container into the customers container.
I made this change and i do agree it was quite unlogical and the code is messy.

So therefore i made this grand change.
Just have to sort the sale part as you mentioned.

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

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

class Product {

public:
	Product(string, double, double);
	string get_name() const {
		return name;
	}
	double get_price() const {
		return price;
	}
	double get_weight() const {
		return weight;
	}

	void write() const {
		cout << "Product name: " << name << " " << "Price: " << price << " Weight: " << weight << endl;
	}


private:
	string name;
	double price;
	double weight;

};

class Purchase : public Product {
public:
	Purchase(string, double, double, int, int);
	string get_name() const {
		return Product::get_name();
	}
	double get_price() const {
		return Product::get_price();
	}
	double get_weight() const {
		return Product::get_weight();
	}

	int get_quantity() const {
		return quantity;
	}
	int get_weightbought() const {
		return weightbought;
	}
	void write() const {
		cout << "Product name: " << get_name() << " Price: " << get_price() << " Weight: " << get_weight() << quantity << endl;
	}
	void buy() {
		quantity += 1;
		weightbought += get_weight();
	}
private:
	int quantity;
	int weightbought;
};

class Verification : public Purchase {

};
int main()
{


	Product A("pizza", 2.35, 0.350);
	Product B("olive oil", 1.15, 0.800);
	Product C("apple", 0.50, 0.25);
	

	A.write();
	B.write();
	C.write();

	cout << "Which product would you like to buy ?" << endl;
	cout << "######################################" << endl;
	string choice;

	if (choice == "pizza") {
	Purchase A1(A.get_name(), A.get_price(), A.get_weight(), 1, 0);
	}

	system("Pause");
	return 0;
}


Product::Product(string n, double p, double w) {
	name = n;
	price = p;
	weight = w;
}

Purchase::Purchase(string n, double p, double w, int q, int wb)
	: Product(n, p, w) {
	quantity = q;
	weightbought = wb;
}
Last edited on
Topic archived. No new replies allowed.