how to instantiate control statement inside virtual function

Pages: 12
I'm trying to instantiate a control statement inside my virtual function program, but I'm hitting a wall. Here's what I have so far:

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
#include <iostream>
#include <string>
#include <iomanip>
#include <ctime>
#include <chrono>

using namespace std;

class Package
{
public:
	Package(const char *, const char *, const char *, const char *, const char *, const char *); //constructor
	~Package();
	virtual float cost() const = 0; //pure virtual function to make abstract base class
	virtual void print() const;
	virtual float tcost() const = 0;

private:
	char *firstName; //char data member for first name (to be entered in by user)
	char *lastName;
	char *address;
	char *city;
	char *state;
	char *zip;
};

Package::Package(const char *first, const char *last, const char *a, const char *c, const char *st, const char *z)
{
	firstName = new char[strlen(first) + 1];
	strcpy(firstName, first);
	lastName = new char[strlen(last) + 1];
	strcpy(lastName, last);
	address = new char[strlen(a) + 1];
	strcpy(address, a);
	city = new char[strlen(c) + 1];
	strcpy(city, c);
	state = new char[strlen(st) + 1];
	strcpy(state, st);
	zip = new char[strlen(z) + 1];
	strcpy(zip, z);
}

void Package::print() const
{
	cout << "\t" << firstName << ' ' << lastName << endl;
	cout << "\t" << "\t" << address << endl;
	cout << "\t" << "\t" << city << ' ' << state << ' ' << zip << endl;
	cout << endl;
}

Package::~Package()
{
	delete[] firstName;
	delete[] lastName;
	delete[] address;
	delete[] city;
	delete[] state;
	delete[] zip;
}

class Game: public Package // Game class derived from Package class
{
public:
	Game(const char *, const char *, const char *, const char *, const char *, const char *, int = 0);
	virtual float cost() const;
	virtual void print() const;

private:
	int numGames;

};

Game::Game(const char *first, const char *last, const char *a, const char *c, const char *st, const char *z, int initNumGames)
	: Package(first, last, a, c, st, z)
{
	numGames = initNumGames;
}

float Game::cost() const
{
	return (numGames * (19.99)); //cost 

	return ((numGames * (19.99)) + (4.99)); //cost + overnight

	return ((numGames * (19.99)) * (1.06)); //cost + insurance (no overnight)

	return (((numGames * (19.99)) * (1.06)) + (4.99)); //cost + insurance (with overnight)

}



void Game::print() const
{
	cout << "Customer: ";
	Package::print(); //call the base-class print function
	
	chrono::system_clock::time_point now = chrono::system_clock::now();
	time_t now_c = chrono::system_clock::to_time_t(now + chrono::hours(24)); //overnight shipping time clock
	cout << "Overnight Shipping Expected Arrival Date: " << put_time(localtime(&now_c), "%F") << '\n';
	
	chrono::system_clock::time_point now2 = chrono::system_clock::now();
	time_t now2_c = chrono::system_clock::to_time_t(now2 + chrono::hours(72)); //standard shipping time clock
	cout << "Standard Shipping Expected Arrival Date: " << put_time(localtime(&now2_c), "%F") << '\n';
}


int main()
{
	Game g("David", "Chalian", "5120 New Bill Street", "East Bumblefuck", "CA", "92604", 4);
	g.print();
	cout << endl;
	cout << "Cost without overnight shipping and insurance: $" << g.cost() << endl;
	cout << "Insurance: " << g.tcost() << endl;

}


As you can see, I am having substantial trouble inside the float Game::cost() const function...I need to essentially have the console ask the user if it's a game they want, how many games they want once they've specified they want games, whether or not they want overnight delivery, whether or not they want insurance, and then the total cost, discount percentage and total cost after discount displayed. Right now, inside the main, I'm spoon feeding the information into the computer so it won't have a hissy fit on me. But ideally, I would like to have the user input the information into the computer and then have it spit back the relevant data (address, shipping ETA, cost, total cost, discount amount, and total cost w/discount).

How would I go about maneuvering from asking the user for input and managing that with the right control statements inside cost() to displaying the right information?
Last edited on
Since you have the option to allow the user to choose overnight shipping and insurance, you can pass 2 bool parameters onto the cost function. Then use if/else and return the price.
My code is a little gross right now, but I'm having massive trouble passing bools into a pure virtual function. I don't even know where to begin. Plus, I have the pseudocode written out for the base-class discount which can be applied to all different types (video games, phones, computers) but I have no idea how to put a function inside a function to get those values and output a proper total cost.

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
#include <iostream>
#include <string>
#include <iomanip>
#include <ctime>
#include <chrono>

using namespace std;

class Package
{
public:
	Package(const char *, const char *, const char *, const char *, const char *, const char *); //constructor
	~Package();
	virtual float cost() = 0; //pure virtual function to make abstract base class
	virtual void print() const;
	//virtual float discount() const = 0;

private:
	char *firstName; //char data member for first name (to be entered in by user)
	char *lastName;
	char *address;
	char *city;
	char *state;
	char *zip;
};

Package::Package(const char *first, const char *last, const char *a, const char *c, const char *st, const char *z)
{
	firstName = new char[strlen(first) + 1];
	strcpy(firstName, first);
	lastName = new char[strlen(last) + 1];
	strcpy(lastName, last);
	address = new char[strlen(a) + 1];
	strcpy(address, a);
	city = new char[strlen(c) + 1];
	strcpy(city, c);
	state = new char[strlen(st) + 1];
	strcpy(state, st);
	zip = new char[strlen(z) + 1];
	strcpy(zip, z);
}

void Package::print() const
{
	cout << "\t" << firstName << ' ' << lastName << endl;
	cout << "\t" << "\t" << address << endl;
	cout << "\t" << "\t" << city << ' ' << state << ' ' << zip << endl;
	cout << endl;
}

Package::~Package()
{
	delete[] firstName;
	delete[] lastName;
	delete[] address;
	delete[] city;
	delete[] state;
	delete[] zip;
}

class Game: public Package // Game class derived from Package class
{
public:
	Game(const char *, const char *, const char *, const char *, const char *, const char *, int = 0);
	virtual float cost() { overnight = this->numGames && insurance = this->numGames; } 
	virtual void print() const;

private:
	int numGames;

};

Game::Game(const char *first, const char *last, const char *a, const char *c, const char *st, const char *z, int initNumGames)
	: Package(first, last, a, c, st, z)
{
	numGames = initNumGames;
}

float Game::cost(bool overnight, bool insurance) 
{
	if (!overnight && !insurance)
		return (numGames * (19.99)); //cost 

	if (overnight && !insurance)
		return ((numGames * (19.99)) + (4.99)); //cost + overnight

	if (overnight && insurance)
		return ((numGames * (19.99)) * (1.06)); //cost + insurance 
	
	if (overnight && insurance)
		return (((numGames * (19.99)) * (1.06)) + (4.99)); //cost + insurance & overnight

}

//float Package::discount() const
//{
//	tcost = 0.0;
//
//	if (cost < 200)
//		tcost = cost - (cost * .019);
//		return tcost;
//
//	else if (cost == 200.01 to 500)
//		tcost = cost - (cost * 0.024);
//		return tcost;
//
//	else if (cost == 500.01 to 1000)
//		tcost = cost - (cost * 0.028);
//		return tcost;
//
//	else 
//		tcost = cost - (cost * 0.033);
//		return tcost;
//}

void Game::print() const
{
	cout << "Customer: ";
	Package::print(); //call the base-class print function
	
	chrono::system_clock::time_point now = chrono::system_clock::now();
	time_t now_c = chrono::system_clock::to_time_t(now + chrono::hours(24)); //overnight shipping time clock
	cout << "Overnight Shipping Expected Arrival Date: " << put_time(localtime(&now_c), "%F") << '\n';
	
	chrono::system_clock::time_point now2 = chrono::system_clock::now();
	time_t now2_c = chrono::system_clock::to_time_t(now2 + chrono::hours(72)); //standard shipping time clock
	cout << "Standard Shipping Expected Arrival Date: " << put_time(localtime(&now2_c), "%F") << '\n';
}


int main()
{
	Game g("David", "Chalian", "4101 Old Mill Street", "Irvine", "CA", "92604", 4);
	g.print();
	cout << endl;
	bool overnight = true;
	bool insurance = true;
	cout << "Cost without overnight shipping and insurance: $" << g.cost(overnight, insurance) << endl;
Last edited on
1
2
3
4
5
if (overnight && insurance)
	return ((numGames * (19.99)) * (1.06)); //cost + insurance 
	
if (overnight && insurance)
	return (((numGames * (19.99)) * (1.06)) + (4.99)); //cost + insurance & overnight 

You have two if-conditions that are exactly the same. I expected them to be different. Better use if-else chains so that your program does less work.
Last edited on
Did you compile your program even once before posting? The error messages are pretty obvious:

1
2
3
4
5
6
7
8
9
10
11
12
13
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
In member function 'virtual float Game::cost()':|
'overnight' was not declared in this scope|
'insurance' was not declared in this scope|
 prototype for 'float Game::cost(bool, bool)' does not match any in class 'Game'|
candidate is: virtual float Game::cost()|
In member function 'virtual void Game::print() const':|
'put_time' was not declared in this scope|
In function 'int main()':|
no matching function for call to 'Game::cost(bool&, bool&)'|
candidate is:|
virtual float Game::cost()|
candidate expects 0 arguments, 2 provided|


You'd also have to consider if Package needs a virtual destructor:
http://stackoverflow.com/questions/270917/why-should-i-declare-a-virtual-destructor-for-an-abstract-class-in-c
So I went through the errors one by one, but that didn't solve the problem, though it did make things a little better. Problem now is the output stops after asking for the boolean "overnight". I need it to take in the bools and pass it appropriately through the "float Game::cost(bool overnight, bool insurance)" ...and also I need the "float Package::discount()" to work, because right now I'm getting redlines under cost, tcost, and the elses. Any help much appreciated.

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
#include <iostream>
#include <string>
#include <iomanip>
#include <ctime>
#include <chrono>

using namespace std;

class Package
{
public:
	Package(const char *, const char *, const char *, const char *, const char *, const char *); //constructor
	virtual float cost(bool overnight, bool insurance) = 0; //pure virtual function to make abstract base class
	virtual void print() const;
	float discount();

private:
	char *firstName; //char data member for first name (to be entered in by user)
	char *lastName;
	char *address;
	char *city;
	char *state;
	char *zip;
	float *tcost;
};

Package::Package(const char *first, const char *last, const char *a, const char *c, const char *st, const char *z)
{
	firstName = new char[strlen(first) + 1];
	strcpy(firstName, first);
	lastName = new char[strlen(last) + 1];
	strcpy(lastName, last);
	address = new char[strlen(a) + 1];
	strcpy(address, a);
	city = new char[strlen(c) + 1];
	strcpy(city, c);
	state = new char[strlen(st) + 1];
	strcpy(state, st);
	zip = new char[strlen(z) + 1];
	strcpy(zip, z);
}

void Package::print() const
{
	cout << "\t" << firstName << ' ' << lastName << endl;
	cout << "\t" << "\t" << address << endl;
	cout << "\t" << "\t" << city << ' ' << state << ' ' << zip << endl;
	cout << endl;
}

class Game: public Package // Game class derived from Package class
{
public:
	Game(const char *, const char *, const char *, const char *, const char *, const char *, int = 0);
	float cost(bool overnight, bool insurance);
	virtual void print() const;

private:
	int numGames;
	bool overnight;
	bool insurance;
	float tcost;

};

Game::Game(const char *first, const char *last, const char *a, const char *c, const char *st, const char *z, int initNumGames)
	: Package(first, last, a, c, st, z)
{
	numGames = initNumGames;
}

float Game::cost(bool overnight, bool insurance) 
{
	if (!overnight && !insurance)
		return (numGames * (19.99)); //cost 

	else if (overnight && !insurance)
		return ((numGames * (19.99)) + (4.99)); //cost + overnight

	else if (!overnight && insurance)
		return ((numGames * (19.99)) * (1.06)); //cost + insurance 
	
	else 
		return (((numGames * (19.99)) * (1.06)) + (4.99)); //cost + insurance & overnight

}

//float Package::discount() 
//{
//	tcost =0.0;
//
//	if (cost < 200)
//		tcost = cost - (cost * .019);
//		return tcost;
//
//	else if (cost == 200.01 to 500)
//		tcost = cost - (cost * 0.024);
//		return tcost;
//
//	else if (cost == 500.01 to 1000)
//		tcost = cost - (cost * 0.028);
//		return tcost;
//
//	else 
//		tcost = cost - (cost * 0.033);
//		return tcost;
//}

void Game::print() const
{
	cout << "Customer: ";
	Package::print(); //call the base-class print function
	
	chrono::system_clock::time_point now = chrono::system_clock::now();
	time_t now_c = chrono::system_clock::to_time_t(now + chrono::hours(24)); //overnight shipping time clock
	cout << "Overnight Shipping Expected Arrival Date: " << put_time(localtime(&now_c), "%F") << '\n';
	
	chrono::system_clock::time_point now2 = chrono::system_clock::now();
	time_t now2_c = chrono::system_clock::to_time_t(now2 + chrono::hours(72)); //standard shipping time clock
	cout << "Standard Shipping Expected Arrival Date: " << put_time(localtime(&now2_c), "%F") << '\n';
}


int main()
{
	bool overnight;
	bool insurance;
	Game g("David", "Chalian", "1527 New Quill Street", "Bumblefuck", "CA", "92604", 4);
	g.print();
	cout << endl;
	cout << "Would you like overnight shipping added? [Y/N]";
	cin >> overnight;
	if (bool overnight = "Y")
		return true;
	else
		return false;
	cout << "Would you like insurance? [Y/N]";
	cin >> insurance;
	if (bool insurance = "Y")
		return true;
	else
		return false;
	cout << "Cost without overnight shipping and insurance: $" << g.cost(overnight, insurance) << endl;
}
line 133: if (bool overnight = "Y")
line 139: if (bool insurance = "Y")

First the comparison operator is ==, also overnight should be a char variable.

float Game::cost(bool overnight, bool insurance)
Why do you want to use local variables when you have member variables?

Line 143 will never be reached.

I think you should really get into the habit of writing and testing your code in small chunks.


Thanks for following up with me Thomas. Appreciate it. Thing is, I've actually done as small of chunks as I can. Just to give you an idea, I have to add 3 more classes to add underneath "Package" ...one for Phones, Computers and Watches. So "Games" is just one component I want to get down before I move to the next ones.

Currently, I followed what you said, but I'm getting a runtime error wherein it won't allow me to input the boolean data for shipping and insurance. It just ends the program after "Would you like overnight shipping?" I feel as though I'm solving more problems and getting rid of more errors, but that's not fixing the underlying structural issues with the program.

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
float Game::cost(const char *overnight, const char *insurance) 
{
	cout << "Would you like overnight shipping added? [Y/N]";
	if (overnight == "Y")
		return true;
	else
		return false;

	cout << "Would you like insurance? [Y/N]";
	if (insurance == "Y")
		return true;
	else
		return false;

	if (!overnight && !insurance)
		return (numGames * (19.99)); //cost 

	else if (overnight && !insurance)
		return ((numGames * (19.99)) + (4.99)); //cost + overnight

	else if (!overnight && insurance)
		return ((numGames * (19.99)) * (1.06)); //cost + insurance 
	
	else 
		return (((numGames * (19.99)) * (1.06)) + (4.99)); //cost + insurance & overnight

}


I want to offload this whole function from the main so that I can keep main clean and focused.
However, when I try to run g.cost(overnight, insurance) in the main, it says there's an error because "the identifiers are undefined." How do I define them?
Last edited on
I would do it like this:
1
2
3
4
cout << "Would you like overnight shipping added? [Y/N]";
char ch;
cin >> ch;
bool overnightShipping = toupper(ch) == 'Y';

Same for insurance.

Could you maybe post the complete assignment?
/
Last edited on
Here's the code. I'm getting an error that says I'm using an "uninitialized local variable 'insurance' and 'overnight' used" ...not sure how to bypass this error which occurs in the main. Overall I'm getting a lot of headaches with this virtual program.

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
class Game: public Package // Game class derived from Package class
{
public:
	Game(const char *, const char *, const char *, const char *, const char *, const char *, int = 0);
	float cost(bool, bool);
	virtual void print() const;

private:
	int numGames;
	bool overnight;
	bool insurance;
	float tcost;

};

float Game::cost(bool overnight, bool insurance) 
{
	cout << "Would you like overnight shipping added? [Y/N]";
	char on;
	cin >> on;
	bool overnightShipping = toupper(on) == 'Y';
	
	cout << "Would you like insurance? [Y/N]";
	char in;
	cin >> in;
	bool insuranceShipping = toupper(in) == 'Y';

	if (!overnight && !insurance)
		return (numGames * (19.99)); //cost 

	else if (overnight && !insurance)
		return ((numGames * (19.99)) + (4.99)); //cost + overnight

	else if (!overnight && insurance)
		return ((numGames * (19.99)) * (1.06)); //cost + insurance 
	
	else 
		return (((numGames * (19.99)) * (1.06)) + (4.99)); //cost + insurance & overnight

}


int main()
{
	bool insurance;
	bool overnight;
	Game g("David", "Chalian", "1527 New Quill Street", "Bumblefuck", "CA", "92604", 4);
	g.print();
	cout << endl;
	cout << "Cost: $" << g.cost(overnight, insurance) << endl;
}
Last edited on
Some other ideas:

use std::string instead of char pointers, it's much easier. It's not cool to mix C and C++ styles. If your teachers are against that, then they need to consider getting themselves into this century. Or more politely, realise that C and C++ are quite different paradigms.

Don't use new and delete. It much better to use an STL container like std::vector<Package> , even if there is only one item in it. Similar to above, if your teachers are against this.

Don't have magic numbers like 1.06 and 4.99 in your code, make them const variables instead.

const double InsuranceRate = 1.06; // use constexpr if your compiler can do it.
With your function declarations, provide names for the parameters - it makes it so much easier to understand. Make them meaningful, not single char variable names.

1
2
3
4
5
6
7
8
Game( std::string& FirstNameArg, 
            std::string& LastNameArg, 
            std::string& AdressArg,
            std::string& SuburbArg, 
            std::string& StateArg, 
            std::string& ZipCodeArg, 
            unsigned int OrderQuantityArg = 0 // unsigned because it should always be positive
           );


I put Arg after all the parameters, this makes it easier to initialise the member variables, which I would name the same without the Arg. I could have put some abbreviation of Parameter, but Arg is fairly easy to understand.

Technically, the declaration parameters should not be const, but the in the implementation they should be where possible:

1
2
3
4
5
6
7
8
9
10
11
12
Game::Game(const std::string& FirstNameArg, 
           const std::string& LastNameArg, 
           const std::string& AdressArg,
           const std::string& SuburbArg, 
           const std::string& StateArg, 
           const std::string& ZipCodeArg, 
           const unsigned int OrderQuantityArg = 0
           ) 
         : // member initialisation list as you already have
{
// ....
}


This is so the user of the function isn't tricked into thinking they have to send a const value to the function.

Prefer double rather than float. The latter's precision is easily exceeded, so that why the former is the default for FP literals.

Good Luck !!
Last edited on
closed account (48T7M4Gy)
Instead of a switch or similar structure for determining the cost of the package, it might be an idea to follow the instructions for the assignment.

What that means is you will have following Package arrangement:

Abstract (or base Package) - with a virtual cost() function - protected address, cost details

This has the following inherited classes
Game_Package with a corresponding inherited cost() function
Watch_Package - ditto -
etc etc for each type of product

You won't need vectors etc because the assignment just says a package has only one type of product so all you need to do is create the type of package, specifying the no of those items, and the address (you'll have to work out how you are going to do that, but as a hint the getAddress() function might be a good job for the base class).

PS discounts can be inherited or base class stuff via a calculate_discount() function. Just work out again the best/simplest place to put it.
Last edited on
kemort, not sure how that's different from what I'm doing currently.

Yes, I agree that I should use a getAddress() function in order to pass the relevant information inside the Game g( INFORMATION). That is ABSOLUTELY true and you basically nailed the head on my main future problem once this all gets resolved.

However, I should note that first I wanted to iron out the main problems so that I could tackle that issue last.

Otherwise I don't see what else I'm doing wrong. Those other packages are not available yet in that I haven't approached them yet. I want to make those other packages after I've finished with games. That is, if I can't get games to work on its own, I won't be able to get any other sub-class to work. It's my philosophy for approaching this problem.

Am I not doing a Game_Package with a corresponding inherited cost() function? I thought that is what I was doing...maybe I'm thoroughly confused. Could you elucidate where I've gone wrong?
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
float Game::cost(bool overnight, bool insurance) 
{
	cout << "Would you like overnight shipping added? [Y/N]";
	char on;
	cin >> on;
	bool overnightShipping = toupper(on) == 'Y';
	
	cout << "Would you like insurance? [Y/N]";
	char in;
	cin >> in;
	bool insuranceShipping = toupper(in) == 'Y';

	if (!overnight && !insurance)
		return (numGames * (19.99)); //cost 

	else if (overnight && !insurance)
		return ((numGames * (19.99)) + (4.99)); //cost + overnight

	else if (!overnight && insurance)
		return ((numGames * (19.99)) * (1.06)); //cost + insurance 
	
	else 
		return (((numGames * (19.99)) * (1.06)) + (4.99)); //cost + insurance & overnight

}


You input overnightShipping and insuranceShipping, but you never used them to do anything particular.

Also, the assignment does not look that tough, you need to make multiple functions to handle specific tasks. Since there may be multiple orders I recommend you use std::vector. Virtual functions are too easy since there are a number of packages you need to create, that means you just need to create a number of classes and link them together.
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
float Game::cost(bool overnight, bool insurance) 
{
	cout << "Would you like overnight shipping added? [Y/N]";
	char on;
	cin >> on;
	bool overnightShip = toupper(on) == 'Y';
	
	cout << "Would you like insurance? [Y/N]";
	char in;
	cin >> in;
	bool insuranceShip = toupper(in) == 'Y';

	if (!overnightShip && !insuranceShip)
		return (numGames * (19.99)); //cost 

	else if (overnightShip && !insuranceShip)
		return ((numGames * (19.99)) + (4.99)); //cost + overnight

	else if (!overnightShip && insuranceShip)
		return ((numGames * (19.99)) * (1.06)); //cost + insurance 
	
	else 
		return (((numGames * (19.99)) * (1.06)) + (4.99)); //cost + insurance & overnight

}

I fixed it. I should note, however, that I need to now instantiate a general discount that is applied once the cost is determined. Thanks everyone for helping.

And no, I cannot use vectors, though to be perfectly honest this is a final project that's not do for 3 months, but I'm doing it early because a.) I can and b.) all the work was given to us at the beginning of the semester to do at our leisure and c.) I can understand it given enough time to bang my head against the wall.

THat being said, I may use vectors to challenge myself.
Last edited on
So how is your output going? Does your program produce the desired output?

And show us the new code you have.
closed account (48T7M4Gy)
not sure how that's different from what I'm doing currently.


On a second look at what you have done I slightly misread what you are attempting with all the decision statements. So we are on the same wavelength and that's great.

Maybe this makes your calculation simpler, keep in mind it is not good practice to combine interface matters inside the class. eg delivery method and insurance questions etc are better sorted out in the main menu.

If you take the Games Package the info required to determine the cost is
(int no_items, bool overnight, bool insurance)
So,
1
2
3
cost = no_items * cost_per_item;
if(overnight){cost += 4.99 }
if(insurance){cost *= 1.06}

And the discount can be applied to that separately elsewhere.

For the Phones Package
( int no_items, bool overnight, bool insurance, int no_cases)
1
2
3
cost = no_items * cost_per_item + no_cases * cost_per_case;
if(overnight){ cost += 25.00 }
if(insurance){cost *= 1.11}

And, again, the discount can be applied to that separately elsewhere.

You can probably see some/all of this can be 'pushed up' higher to the base class and thereby eliminate repetition.

Sorry to make my post reported, but here is the thing :

Your program should use virtual functions including at least one pure virtual function with polymorphism. Your program should have one class for each package type along with a base-class that contains the appropriate data members and methods. An example of sample output for games would be :

You have not followed these instructions. Also the Package base class, you also create Games, Phones, Watches and one package class of your choice as derived classes. Then you link the derived classes to the base class. You may need an array of pointers to iterate though all packages without having to care about the types of classes.
Pattako, I outlined in an earlier post that I will make those other classes as well. Is that what you're saying? Because I will do that. I just need to finish Games first before I move onto those other classes. See kemort's response, he mentioned something similar.
Pages: 12