views:

70

answers:

2

if i want to have 3 classes, which have common fields (and i want them to be static) and they have a common function (which needed to be overridden, i.e virtual)

  • what the best design to do this?
  • do i need to create an interface in a header file
    and then create it's .cpp file and get the 3 classes inheritance from it?
  • what about the static members?
  • can i declare them in the header file?
  • when creating header file which representing interface, do i have to create it's .cpp file?
+1  A: 
DyP
+3  A: 

Declare the classes in header files.
This is so that the declaration can be shared between multiple source files (with #include) and thus obey the (One definition rule).

It is traditional (though not required) that each class has its own file. To make it consistent and easy to find things you should name the file after the class. So Class A should be declared in A.h and defined in A.cpp.

MyInterface.h

class MyInterface
{
    protected:
        static int X;
        static int Y;
        static int Z;
    public:
        // If a class contains virtual functions then you should declare a vritual destructor.
        // The compiler will warn you if you don't BUT it will not require it.
        virtual ~MyInterface() {}   // Here I have declared and defined the destructor in
                                    // at the same time. It is common to put very simplistic
                                    // definitions in the header file. But for clarity more
                                    // complex definitions go in the header file. C++ programers
                                    // dislike the Java everything in one file thing because it
                                    // becomes hard to see the interface without looking at the
                                    // documentaiton. By keeping only the declarations in the
                                    // header it is very easy to read the interface.

        virtual int doSomthing(int value) = 0; // Pure virtual
                                               // Must be overridden in derived
 };

A.h

 #include "MyInterface.h"

 class A: public MyInterface
 {
     public:
        virtual int doSomthing(int value);
 };

B.h

 #include "MyInterface.h"

 class B: public MyInterface
 {
     public:
        virtual int doSomthing(int value);
 };

C.h

 #include "MyInterface.h"

 class C: public MyInterface
 {
     public:
        virtual int doSomthing(int value);
 };

Now you define the implementation in the source files:

MyInterface.cpp

#include "MyInterface.h"

// Static members need a definition in a source file.
// This is the one copy that will be accessed. The header file just had the declaration.
int MyInterface::X = 5;
int MyInterface::Y = 6;
int MyInterface::Z = 7;

A.cpp

#include "A.h"

// Define all the methods of A in this file.
int A::doSomthing(int value)
{
    // STUFF
}

B.cpp

#include "B.h"

int B::doSomthing(int value)
{
    // STUFF
}

C.cpp

#include "C.h"

int C::doSomthing(int value)
{
    // STUFF
}
Martin York
+1 I just didn't want to write all that out ^^
DyP
about the static members, can i declare one static member in MyInterface.cpp and initialize it differently in each derived class i have? or i have to declare them inside the derived classes?
or.nomore
@or.nomore: No. Static means that there is only of the object. Thus all instances share the same value.
Martin York