Still Need help splitting a Single .cpp into a header, implantation, and main file

I wrote this code and while it works just fine and all. However I am not quite sure how to properly separate it into a main.cpp, a header.h, and implantation.cpp files

please excuse the extreme noviceness of it all.

Thank you!!

Edit: WTH1? it worked for some reason! all i did was ->build->rebuild solution and it worked!!
thank you everyone!
Edit2: why would doing that fix those errors?

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
  #include <iostream>

using namespace std;

int main()
{
    class invoice 
        {
            public:
            int itemNumber;
            int itemQuantity;
            float itemPrice;
            void set_values(int Number, int Quantity, float Price)
                {
                    itemNumber = Number;
                    itemQuantity = Quantity;
                    itemPrice = Price;
                }

            void check(void)
                {
                    if (itemQuantity < 0)
                    {
                        itemQuantity = 0;
                         cout << "Invalid quantity input. Quantity is set to 0" << endl; 
                     }

                }

            int getInvoiceAmount(void)
                {
                    int invoiceAmount;
                    invoiceAmount =  itemQuantity * itemPrice;
                    return invoiceAmount;
                }

                void printInvoiceAmount(void)
                {
                    cout << "Item " << itemNumber << " " << itemQuantity << " " << itemPrice << endl;
                }
        } item1, item2;

item1.set_values(10141, 5, 100.00);
item2.set_values(10142, -5, 100.00);

item1.check();
item2.check();

cout << endl;

int amount1 = item1.getInvoiceAmount();
int amount2 = item2.getInvoiceAmount();

cout << "The invoice amnout of the first item is: " << amount1 << endl;
cout << "The invoice amnout of the second item is: " << amount2 << endl;
cout << endl;

if (amount1  == 0)
    {
        cout << "The invoice amount of the amount1 is 0 because the item quantity is less than or equal zero" << endl;
    }
else if (amount2 == 0)
    {
        cout << "The invoice amount of the amount2 is 0 because the item quantity is less than or equal zero" << endl;
    }
else
    {
        cout << "The invoice amount of both items is 0 because the item quantities are less than or equal zero" << endl;
    }

cout << endl;

item1.printInvoiceAmount();
item2.printInvoiceAmount();

cout << endl;

return 0;

}
Last edited on
Your header (in your case, header.h) should typically contain your function or class declarations.

The matching .cpp file (in your case, implementation.cpp) should contain the definitions of the functions or classes that were previously declared.

And finally, your main.cpp should provide the main function - or the primary driver program.

Here's an example:

Here's a header named foo.h, which contains a class declaration:
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef _FOO_H_
#define _FOO_H_

class Foo {
public :
	Foo();

	void sayHello();

};

#endif 


Here's the accompanying foo.cpp file, which contains the definitions:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "foo.h"

Foo::Foo() {

}

void Foo::sayHello() {
	std::cout << "Hello!" << std::endl;
}


And here's the main.cpp file, which defines the main function and makes use of our class:

1
2
3
4
5
6
7
8
9
10
#include "foo.h"

int main(int argc, char* argv[]) {

	Foo foo;

	foo.sayHello();

	return 0;
}
Last edited on
header.h to contain the class without the implementation.

header.h:

1
2
3
4
5
6
7
8
9
10
11
class invoice 
        {
            public:
            int itemNumber;
            int itemQuantity;
            float itemPrice;
            void set_values(int Number, int Quantity, float Price);
            void check(void);
            int getInvoiceAmount(void);
            void printInvoiceAmount(void);
        } item1, item2;


implementation.cpp contains the implementation of the functions.

You will need to use :: scope for the functions.

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
 void invoice::set_values(int Number, int Quantity, float Price)
                {
                    itemNumber = Number;
                    itemQuantity = Quantity;
                    itemPrice = Price;
                }

            void invoice::check(void)
                {
                    if (itemQuantity < 0)
                    {
                        itemQuantity = 0;
                         cout << "Invalid quantity input. Quantity is set to 0" << endl; 
                     }

                }

            int invoice::getInvoiceAmount(void)
                {
                    int invoiceAmount;
                    invoiceAmount =  itemQuantity * itemPrice;
                    return invoiceAmount;
                }

                void invoice::printInvoiceAmount(void)
                {
                    cout << "Item " << itemNumber << " " << itemQuantity << " " << itemPrice << endl;
                }


and main.cpp you basically put the use of the class in. Objects and call functions there.

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
item1.set_values(10141, 5, 100.00);
item2.set_values(10142, -5, 100.00);

item1.check();
item2.check();

cout << endl;

int amount1 = item1.getInvoiceAmount();
int amount2 = item2.getInvoiceAmount();

cout << "The invoice amnout of the first item is: " << amount1 << endl;
cout << "The invoice amnout of the second item is: " << amount2 << endl;
cout << endl;

if (amount1  == 0)
    {
        cout << "The invoice amount of the amount1 is 0 because the item quantity is less than or equal zero" << endl;
    }
else if (amount2 == 0)
    {
        cout << "The invoice amount of the amount2 is 0 because the item quantity is less than or equal zero" << endl;
    }
else
    {
        cout << "The invoice amount of both items is 0 because the item quantities are less than or equal zero" << endl;
    }

cout << endl;

item1.printInvoiceAmount();
item2.printInvoiceAmount();

cout << endl;

return 0;


Double check that all of this will work. Make sure to include the files in the top.
Thank you everyone for your help
Unfortunately i cant seem to get it to work

here is the Implimation.cpp (Invoice.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
29
30
31
32
#include <iostream>
#include "Invoice.h"

void invoice::set_values(int Number, int Quantity, float Price)

                {
                    itemNumber = Number;
                    itemQuantity = Quantity;
                    itemPrice = Price;
                }

            void invoice::check(void)
                {
                    if (itemQuantity < 0)
                    {
                        itemQuantity = 0;
                         cout << "Invalid quantity input. Quantity is set to 0" << endl; 
                     }

                }

            int invoice::getInvoiceAmount(void)
                {
                    int invoiceAmount;
                    invoiceAmount =  itemQuantity * itemPrice;
                    return invoiceAmount;
                }

                void invoice::printInvoiceAmount(void)
                {
                    cout << "Item " << itemNumber << " " << itemQuantity << " " << itemPrice << endl;
                }


here is the header.h (Invoice.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <iostream>
#ifndef _Invoice_H_
#define _Invoice_H_
using namespace std;

class invoice 
        {
            public:
            int itemNumber;
            int itemQuantity;
            float itemPrice;
            void set_values(int Number, int Quantity, float Price);
            void check(void);
            int getInvoiceAmount(void);
            void printInvoiceAmount(void);
        } item1, item2;

#endif; 


and here is the main.cpp (InvoiceMain.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include "Invoice.h"
using namespace std;

int main()
{
item1.set_values(10141, 5, 100.00);
item2.set_values(10142, -5, 100.00);

item1.check();
item2.check();

cout << endl;

int amount1 = item1.getInvoiceAmount();
int amount2 = item2.getInvoiceAmount();

cout << "The invoice amnout of the first item is: " << amount1 << endl;
cout << "The invoice amnout of the second item is: " << amount2 << endl;
cout << endl;

if (amount1  == 0)
    {
        cout << "The invoice amount of the amount1 is 0 because the item quantity is less than or equal zero" << endl;
    }
else if (amount2 == 0)
    {
        cout << "The invoice amount of the amount2 is 0 because the item quantity is less than or equal zero" << endl;
    }
else
    {
        cout << "The invoice amount of both items is 0 because the item quantities are less than or equal zero" << endl;
    }

cout << endl;

item1.printInvoiceAmount();
item2.printInvoiceAmount();

cout << endl;

return 0;
}


the errors that i am getting is something along the lines of
"class invoice item2 already defined in invoice.obj"
"class invoice item1 already defined in invoice.obj"
"fatal error lnk1169 one or more multiply defined symbols found"
Last edited on
Remove the declarations of item1 and item2 on line 17 of your invoice.h header. Declare the items in your main function.
Ok now i am getting these errors
Edit: HOLY sh*t it worked for some reason! all i did was ->build->rebuild solution and it worked!!
thank you everyone!
Edit2: why would doing that fix those errors?

Invoice.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
29
30
31
#include <iostream>
#include "Invoice.h"


void invoice::set_values(int Number, int Quantity, float Price)
	{
		itemNumber = Number;
		itemQuantity = Quantity;
		itemPrice = Price;
        }

void invoice::check(void)
	{
		if (itemQuantity < 0)
			{
				itemQuantity = 0;
				cout << "Invalid quantity input. Quantity is set to 0" << endl; 
			}
        }

int invoice::getInvoiceAmount(void)
	{
		int invoiceAmount;
		invoiceAmount =  itemQuantity * itemPrice;
		return invoiceAmount;
        }

void invoice::printInvoiceAmount(void)
	{
		cout << "Item " << itemNumber << " " << itemQuantity << " " << itemPrice << endl;
        }


Error C2628 'invoice' followed by 'void' is illegal (did you forget a ';'?) line 5
Error C2556 'invoice invoice::set_values(int,int,float)': overloaded function differs only by return type from 'void invoice::set_values(int,int,float)' line 6
Error C2371 'invoice::set_values': redefinition; different basic types line 6


Invoice.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#ifndef _Invoice_H_
#define _Invoice_H_
using namespace std;

class invoice 
        {
            public:
            int itemNumber;
            int itemQuantity;
			int getInvoiceAmount(void);
            float itemPrice;
            void set_values(int Number, int Quantity, float Price);
            void check(void);
            void printInvoiceAmount(void);
};

#endif; 


InvoiceMain.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
	
#include <iostream>
#include "Invoice.h"
; using namespace std;
invoice item1;
invoice item2;

int main()
{
item1.set_values(10141, 5, 100.00);
item2.set_values(10142, -5, 100.00);

item1.check();
item2.check();

cout << endl;

int amount1 = item1.getInvoiceAmount();
int amount2 = item2.getInvoiceAmount();

cout << "The invoice amnout of the first item is: " << amount1 << endl;
cout << "The invoice amnout of the second item is: " << amount2 << endl;
cout << endl;

if (amount1  <= 0)
    {
        cout << "The invoice amount of the amount1 is 0 because the item quantity is less than or equal zero" << endl;
    }
else if (amount2 <= 0)
    {
        cout << "The invoice amount of the amount2 is 0 because the item quantity is less than or equal zero" << endl;
    }
else
    {
        cout << "The invoice amount of both items is 0 because the item quantities are less than or equal zero" << endl;
    }

cout << endl;

item1.printInvoiceAmount();
item2.printInvoiceAmount();

cout << endl;

return 0;
}

Last edited on
Hi,

Just some extra things to help you out :+)

Remove the using namespace std; from your header file, you don't need it there, in fact it's best practise to remove it everywhere, put std:: before each std thing. Google as to why that is :+)

Consider using an member initialiser list with a constructor, rather than your set_values function. One only needs set functions if the value of a member variable needs to change after the object has been created:

1
2
3
4
5
6
7
8
9
10
11
invoice::invoice(const std::size_t Number,  // should be positive
                             const std::size_t Quantity,  //and arguments can often be const
                             const double Price) // prefer double rather than float
         :   // colon introduces initialiser list
         itemNumber(Number),
         itemQuantity(Quantity),
         itemPrice(Price);
	{
	// do validation here, check for maximum range
       // Price should be positive 
        }



http://en.cppreference.com/w/cpp/language/initializer_list


Functions that don't change the state of the object should be marked const:

void printInvoiceAmount() const;

This means you are promising not to change the value of any of the member variables. Remember to change it for the function definition, declarations and definitions must match :+)

I like to use .hpp for header files, because it says that the code is c++.

std::endl flushes the buffer every time so that could be inefficient, (not a problem here though). Use "\n" instead:

std::cout << "The invoice amnout of the first item is: " << amount1 << "\n";

One can build up long std::cout statements with several statements:

1
2
std::cout << "The invoice amount of the amount1 is 0 because ";
std::cout << "the item quantity is less than or equal zero\n";


Avoid global variables like you have on lines 5 and 6, put them in main(). It's a good idea to limit the scope of a variable as much as possible, if you can get away with making them local to a function, then do so :+)

When a function has no arguments, there is no need to put void, this has never been a requirement for C++, it was for C though.

getInvoiceAmount should not return an int, double if you take my earlier advice. There should have been a warning about narrowing types float to int.

Hope all this helps, even a little bit :+)

Good Luck !!

Last edited on
thank you!
also, oh god my c is showing
Your code compiles and runs on VC++ 2010 Express.
There is only one problem:
 
invoice.cpp(24):  warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
could you explain this for me? i dont think i have a float anywhere on there
TheIdeasMan wrote:
getInvoiceAmount should not return an int, double if you take my earlier advice. There should have been a warning about narrowing types float to int.


Also, InvoiceAmount could be a class member of type double. Don't mix your types if you can help it :+)
It's a good idea to limit the scope of a variable as much as possible

Related to that, I don't think it's been mentioned (I may have missed it)
The three member variables of the invoice class should be made private rather than public access.

Topic archived. No new replies allowed.