Compiling multiple cpp files (abstract factory pattern)

Hi all,

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 during compilation phase. 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:

//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 parameter but I didn't succeed). Additionally, any advice (putting classes to headers etc.) about the model is welcome.

Thanks for your helps in advance :o

Info. about my environment:
Linux 2.6.24-18-generic i686 GNU/Linux
gcc version 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
editor vim

let's start with this:

g++  file1.cpp file2.cpp file3.cpp -o myprogram

The order may be important in terms of external symbols being defined. ld works from left to right. SO -- if an external symbol in file1 also needs symbols in file2, you have a problem.

I think I should add the .cpp files using #include (and also I need to change the extension names to .hpp except run.cpp).

Thanks...

you have a small error :smiley:

you wrote :

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

but it should be :wink:

void say(){
cout<<"HelloNormal class says: Hello!";
}

Oups, thanks :slight_smile: ...