Compiling multiple cpp files (abstract factory pattern).

Hi,

I have been working with java for awhile and because of my school projects I needed to switch C++. I tried to implement some patterns in C++ but unfortunately I couldn't. Specifically, I tried to implement abstract factory pattern but since I used separated files (habitual behavior from java:)) I got errors in compilation. Could you give me some tips about the compilation and modeling(if I should use .h files instead of .cpp files or not) of my code?

Here is my code:

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
81
82
83
84
//run.cpp
#include <iostream>

using namespace std;

int main(){
        HFact* fact;
#ifdef LINUX
        HFact* fact = new HelloLinFact;
#elif WINDOWS
        //HFact* fact = new HelloWinFact;
        cout<<"At the moment this implementation is not valid..."<<endl;
#endif

        CHello* hello = fact->getHelloNormal();
        hello->say();
        hello = fact->getHelloReversed();
        hello->say();
        hello = fact->getHelloWorld();
        hello->say();

        return 0;

}

//HFact.cpp
class HFact {
        public:
                extern virtual CHello* sayHelloNormal() = 0;
                extern virtual CHello* sayHelloRev() = 0;
                extern virtual CHello* sayHelloWorld() = 0;
};

//CHello.cpp
class CHello{
        public:
                virtual void say() = 0;
};

//HelloLinFact.cpp
class HelloLinFact : public HFact {
        public:
                CHello* getHelloNormal() {
                        return new HelloLinNormal;
                }

                CHello* getHelloReversed(){
                        return new HelloLinReversed;
                }

                CHello* getHelloWorld(){
                        return new HelloLinWorld;
                }
};

//HelloLinNormal.cpp
#include <iostream>

class HelloLinNormal : public CHello(){
        public:
                void say(){
                        cout>>"HelloNormal class says: Hello!";
                }
}

//HelloLinReversed.cpp
#include <iostream>

class HelloLinReversed : public CHello{
        public:
                void say(){
                        cout<<"HelloLinReversed says: olleH";
                }
}

//HelloLinWorld.cpp
#include <iostream>

class HelloLinWorld : CHello{
        public:
                void say(){
                        cout<<"HelloLinWorld says: Hello World!";
                }
}


I put all the classes in separate .cpp files. I will appreciate if you give me the compilation instructions for this code (I think I should link the object files to each other with gcc's -LObjName command but I couldn't do it). Additionally, any advice (putting classes to headers etc.) about the model is welcome.

Thanks for your helps in advance.

The main file should follow this pattern:
1
2
3
4
5
6
7
8
9
10
11
12
#include <standard-stuff>
...

#include "project-stuff.hpp"
...

void stuff_only_main_uses() ...
...

int main() {
  ...
  }


Include files typically follow this pattern:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef MY_INCLUDE_HPP
#define MY_INCLUDE_HPP

#include <standard-stuff-needed-by-the-interface>
...

#include "project-stuff-needed-by-the-interface.hpp"
...

namespace my_include_hpp
  {

  class interface-class-definitions ...
  void interface-function-prototype-definitions ...
  extern const int interface-constant-definitions ...
  ...

  }

#endif 


And finally, module files typically follow this format:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <standard-stuff-needed-by-this-module-but-not-in-the-include-file>
...

#include "project-stuff-needed-by-this-module-but-not-in-the-include-file.hpp"
...

#include "my-include.hpp"

namespace my_include_hpp
  {

  // functions, etc. that are unique to this module
  // (wrap it in a namespace to avoid linker name collisions with other libraries)

  // definitions for everything declared in the hpp file

  }


Finally, to compile everything together, there are several options. But in one shot it is:

gcc -Wall -pedantic -o my-prog my-prog.cpp my-module1.cpp my-module2.cpp [ -llibrary1 ...]

If you use the standard math routines you'll have to link with the math library:
-lm

If you use other specialty libraries you'll have to link with them also. For example, using the PDCurses on Windows:
-lpdcurses

Hope this helps.
Last edited on
Thanks for the response Duoas. Great tips.
I'm glad that helped. :-)
Last edited on
Topic archived. No new replies allowed.