Classes in Separate Files

A total beginner here, I need help with classes in separate files.

I'll simplify my code here, so that you don't have to scour through the lines to find my problem.

I am making this all encompassing program filled with all I have learned in C++ and adding features to it as time goes by.

What I mean, is that it has a main menu, with categories, such as Arithmetic, Games those kind of functions... Instead of making all of that code into one single (main.cpp) I wanted to separate it into different files (classes).

Here is what I want to achieve...
- Main Menu (DONE)
- Menu Select (DONE)
- Separating Functions in Different Files (NEED HELP)
- Calling Classes into int main() (NEED HELP)

For the different functions, may I have a link or an example code for how I may achieve this?


Basically I have a class called Arithmetic, and within it, there are functions for Addition, Subtraction, Multiplication and so on...

Any helpful links/examples would be appreciated.
Thank You, The Resonance.

If I understand your questions correctly, you want to add classes in different files. For this, you need a special file called a header file. The .cpp file contains the actual declaration of the class or functions, a header (.h, .hxx,.hpp) will contain the definitions. For example:


main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "secondFile.h"

using namespace std;

int main()
{
cout << getNumber() <<endl;
Func();
return 0;
}


notice how I included secondFile.h with quotations. You have to do this for each of the .cpp files that you want to link with the header.

secondFile.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
#pragma once

class myClass 
{
public:
void Func();
int getNumber();
private:
const int number = 5 ;
};


secondFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "secondFile.h"
using namespace std;


myClass::func()
{
cout << "this is a test function :)" << endl;
}

myClass::getNumber()
{
return number;
}


This is the header file. notice how there is a special directive #pragma once . This is just to protect the file from being included more than once and have a name ambiguity problem. if your compiler doesn't support this, you could use include guards:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
#ifndef SECONDFILE_H
#define SECONDFILE_H

class myClass 
{
public:
void Func();
int getNumber();
private:
const int number = 5 ;
};
#endif //SECONDFILE_H 


with the name of the file and extension in all caps. Hope this helps :)
Last edited on
@IceStorm +1

Thank you so much! This was truly helpful ^-^

IceStorm wrote:
The .cpp file contains the actual declaration of the class or functions, a header (.h, .hxx,.hpp) will contain the definitions.

You've got that backwards. Declarations go in the .h file. Definitions go in the .cpp file.

That leads to several problems with your post.
1) You've reversed the labels on the secondfile.h and secondfile.cpp files.
2) Line 13 of secondfile.h needs a ; to terminate the class declaration.
3) secondfile.cpp needs to include secondfile.h
4) You don't put #pragma once or include guards in a .cpp file. Those belong in a .h file.



Last edited on
Ah yes, I caught that one, thanks ^-^
Ahh, whoops sorry. I will fix it :)

EDIT:
Also, you should look up Bucky Roberts on YouTube, he has beginner tutorials on his channel.
Last edited on
Topic archived. No new replies allowed.