Polymorphism, inheritance, include files

Hello everybody,

I'm just beginning in C++ and was wondering if somebody could help me with a concept that should be fairly simple but is frustrating a newb like me :) Please also let me know if I'm not following style guidelines or conventions (for example, I wasn't sure where to save the class files - .h or .cpp?). Thank you!

If for example I have the following files (renamed to some generic names for clarity):

Base.h
1
2
3
4
5
6
7
8
9
10
11
class Base{
    protected:
        typedef struct {
            int x;
            char *y;
        } mystruct;
    
    public:
        virtual void getStruct(mystruct *structure) = 0;
        virtual void addStruct(int a, int b) = 0;
};


Triangle.h

1
2
3
4
5
6
7
8
9
10
11
12
#include "Base.h"
class Triangle: public Base{
    
  public:
    void getStruct(mystruct *structure) {
    }
    
    void addStruct(int a, int b) {
        printf("in triangle");
        return;
    }
};


Circle.h

1
2
3
4
5
6
7
8
9
10
11
12
#include "Base.h"
class Circle: public Base{
    
  public:
    void getStruct(mystruct *structure) {
    }
    
    void addStruct(int a, int b) {
        printf("in circle");
        return;
    }
};


Main.cpp
1
2
3
4
5
6
7
8
9
10
#include "Triangle.h"
#include "Circle.h"
...
void myfunction(int option) {
    Base *base = new Triangle();
    (*base).addStruct(1);
    Base *base2 = new Circle();
    (*base2).addStruct(2);
}
...



I think my code structure is right, but the problem is that g++ gives me an error about redefinition of the Base class (since it's included in both Triangle.h and Circle.h, and when I includde Triangle.h and Circle.h in Main.cpp then it's referenced twice). But of course if I don't include Base.h in both children, then they won't be able to derive from the Base class.

What am I doing wrong?

Thanks for your help!
There is a good explanation here:
http://www.learncpp.com/cpp-tutorial/110-a-first-look-at-the-preprocessor/
Scroll down to the "Header guards" subsection.
Topic archived. No new replies allowed.