Classes division

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

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
  #include<iostream>
using namespace std;

class Numbers
{
    public:
               bool Number;

        void ChoiceOfNumbers()
        {
            if (Number)
                cout << "True " << endl;
                else
                cout << "False" <<endl;
        }

        void Choice()
        {
            cout << " ENTER NUMBER 1/0:" <<endl;
            cin >> Number;
        }
};

class True: public Numbers
{
   public:

   True()
   {

             Number = true;
   }
};

class False : public Numbers
{
   public:
   False()
   {
     Number = false;
   }
};


int main()
{
    Numbers TRUE;
    TRUE.Choice();
    TRUE.ChoiceOfNumbers();
    return 0;


}
Your friend is right and wrong.

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.
2nd reply on the 'how'.

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);
}


BTW: don't take that code too serious.
Can you format my code this way please?
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.
No. What you have is apparently "homework", and it does not benefit you in any way if you don't do it yourself.

plexus forgot to mention that header files should have "include guards". See http://www.cplusplus.com/forum/articles/10627/
Thanks for your answers i did it =D .

main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
#include "Numbers.h"


int main()
{
    Numbers TRUE;
    TRUE.Choice();
    TRUE.ChoiceOfNumbers();
}


numbers.h
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
#include <iostream>
using namespace std;


class Numbers
{
    public:
               bool Number;

        void ChoiceOfNumbers();

        void Choice();
};

class True: public Numbers
{
   public:

   True();

};

class False : public Numbers
{
   public:
   False();

};


numbers.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 "Numbers.h"
#include <iostream>
using namespace std;

void Numbers::ChoiceOfNumbers()
        {
            if (Number)
                cout << "True " << endl;
                else
                cout << "False" <<endl;
        }

void Numbers::Choice()
        {
            cout << " ENTER NUMBER 1/0:" <<endl;
            cin >> Number;
        }

True::True()
    {

             Number = true;
   }


False::False()

    {
     Number = false;
   }



Thanks again :)
Nice work.

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)

But again, it's not wrong.
Topic archived. No new replies allowed.