I am learning C++ and i am on the topic classes(Inheritence),my friend told me that the way I am writing code is bad software engineering and said me to divide this code in 3 files main.cpp numbers.cpp and numbers.h but i am confused on how to do it he is out for his project and not responding my messages please help
Right because it IS good software engineering to split larger programs into multiple files. Usually one file per class or one file for a small group of smaller classes that do similar things (like your True and False classes).
Wrong because learning classes is complicated enough that it's unnecessary to deal with multiple files since the classes you create aren't that big.
Also, it's way easier to get a good view of the big picture of your program if everything is in one file.
So: during learning it's OK to keep everything in one file as long as it doesn't exceed hundreds of lines. But if you start programming "real" programs you better split up the code.
A header (.h file) contains no functional code (usually), just declarations and prototypes:
a_header.h:
1 2 3 4 5 6 7 8 9 10 11 12 13
class foo
{
public:
int aMethod( int val );
};
class bar : public foo
{
public:
void anotherMethod( char stuff );
};
The source file contains the code. The header is included, that means all of the text of the header file is just copied into the source file before compiling.
a_source_file.cpp
1 2 3 4 5 6 7 8 9 10 11 12
#include "a_header.h"
#include <iostream>
int foo::aMethod( int val )
{
return val - 42;
}
void bar::anotherMethod( char stuff )
{
std::cout << stuff;
}
The main source file also includes the header so that the compiler knows that the classes exist and what methods they contain.
main.cpp
1 2 3 4 5 6 7 8 9 10
#include "a_header.h"
int main()
{
bar an_object;
an_object.anotherMethod('X');
return an_object.aMethod(42);
}
Well, no, sorry. I explicitly did NOT use your code for my example to give you the learning opportunity to do that yourself.
And since the structure of my example strongly resembles your code, it shouldn't be too hard.
In addition to the safe guards keskiverto mentioned (which I indeed forgot), yet another tip.
It's not wrong, but you don't necessarily need the <iostream> header and the using ... command in all of your files.
Only in the numbers.cpp parts if the iostream lib is used. (cout, endl)