can't access variable inside protected class

};[/code]
Last edited on
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
class Package
{
protected:
	string name_and_address = "?";

	double cost = 0.0;
	double discount = 0.0;
	double discount_rate = 0.0;

	bool overnight_delivery = false;
	bool insured = false;

	string package_contents = "?";

	double shipcost = 0.0; // Why shipcost is 0.0???

        void printShipCost() { cout << shipcost << endl; }
};

class Video_Games :public Package
{
private:
	int num_games = 0;
	double shipcost = 4.99; 

public:
	Video_Games(string location, int number_of_games, bool express, bool insurance)
	{
		num_games = number_of_games;
		name_and_address = location;
		overnight_delivery = express;
		insured = insurance;

		package_contents = to_string(num_games) + " Video Game(s)";

		cost = calculate_cost();
		discount = calculate_discount();

	}

	~Video_Games() {};

        void printShipCost() { cout << shipcost << endl; }

protected:

	double calculate_cost()
	{
		cost = num_games * 19.99;
		if (overnight_delivery) { cost += shipcost; }
		if (insured) { cost *= 1.06; }

		return cost;
	}
};


Try to use printShipCost() and see the result.
When I try your method, I get an insanely large negative number as the result. -92559631349317830736831783200707727132248687965119994463780864.00 That was the output for the shipcost. Not sure how to fix this so I can get an accurate output of the shipping cost unique for each derived class.
Last edited on
Topic archived. No new replies allowed.