passing values from one function to another in class implementation file

Hello everyone we have an assignment due tonight which involved creating a class and using functions within it to display a giftwrap invoice.

I have almost completed this assignment but am getting some identifier errors and function called missing arguments and plenty more. I am hoping someone out there who is a better programmer then me(not hard to find) can help me out.

First I need to know what can I do to pass values that one function calculates into another?

I need to pass the value subtotal into calctax, and then add calcsubtotal and calctax to get calctotal.

please help me out.

this code is part of my implementation file named giftwrap.cpp
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
float GiftWrap::calcSubtotal()
{
	float surfacearea;
	float subtotal;
	
	surfacearea==(2*length*width)-(2*length*height)-(2*width*height);

	(surfacearea*pricePerInch)==subtotal;
	
	return subtotal;
}

float GiftWrap::calcTax()
{
	float tax;
	tax==(calcSubtotal*taxRate);

	return tax;
}

float GiftWrap::calcTotal()
{
	float total;

	total== calcSubtotal+calcTax;

	return total;
}
All those == should be =.
Thanks for the input kbw, how can I pass the values of the functions to the others?
For your example, call the function. Here is how I would do your code:
1
2
3
4
5
6
7
8
9
10
11
12
float giftWrap::calcSubTotal() {
    // You can still do your way, I just put all the equations onto a single line
    return 2 * (length*width - length*height - width*height) * pricePerInch;
}

float giftWrap::calcTax() {
    return calcSubTotal() * taxRate;
}

float giftWrap::calcTotal() {
    return calcSubTotal() + calcTax();
}


Also, in your original code, you line 8 is the wrong way round the equals sign (should give you an error like 'assigning to an rvalue').
Last edited on
Hi NT3 I switched my line 8 around, and did the return the way you recommended that and kbw's recommendation helped out. :) Thank you!

Now my implementation is free of errors. If possible can you explain how I could go about using a constructor that takes two arguments to instantiate a class object?

for example in my code:
this is my header file GiftWrap:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class GiftWrap
{

private:
	float length;
	float width;
	float height; 
	float taxRate; 
	float pricePerInch;


public:
	GiftWrap();
	GiftWrap(float x, float y);


I want to use the 2nd constructor, "GiftWrap(float x, float y)" and instantiate a class object named sallys (this will be written in another cpp file with include "giftwrap.h")
Last edited on
Its not hard. Simply specify parenthesis and the arguments to pass when creating the object:
1
2
3
GiftWrap bobs; // default constructor
GiftWrap freds(); // default constructor
GiftWrap sallys(1.0f, 5.0f); // value constructor 


Try looking up some tutorials on classes if you don't really understand, and even if you think you do, they can help to show you what you need to know anyway.
Thanks NT3 you are a godsend! lol. :) Any good tutorials you recommend? This is my first programming language btw so everything is kind of floaty but I'm sure I'll learn with practice. I'm using a book called A First Book of C++ by Bronson.
The ones on this site are pretty good. Also, I like the ones at learncpp (just look it up), they have a large number of examples to help you understand what is going on, and the contrasting style can bring other things to focus. And yes, the only way that anyone becomes good at anything is practice!
Yes that's correct NT3!

practice will make perfect eventually!
Last edited on
Topic archived. No new replies allowed.