Inheritance and header file include issues

Hi,

I have a couple of classes derived from a parent class, and each of these classes and the parent have their own header files and cpp's. The derived class headers include the header of the parent. The main cpp file needs to include all headers; parent class, and the two derived class headers. I added an include guard for the parent header file in the main cpp, but since both the derived class headers include the parent header file, I am given an error during compilation. How can I fix this problem without merging the derived classes into one single header file?

Thanks,
Farhan
It's hard to say without seeing you file structure, but you probably should read this for some information:
http://www.cplusplus.com/forum/articles/10627/
Hi Zhuge,

I'm adding various code snippets here which are basically separate files on my system. This is just test code so you may see some weird function implementations.

polygon.h:
1
2
3
4
5
6
7
8
9
10
11
12
#include <string>

class Polygon
{
private:
	int sides;
public:
	Polygon();
	Polygon(int p_sides);
	virtual int Perimeter() = 0;
	virtual double ComputeArea() = 0;
};


polygon.cpp
1
2
3
4
#include "StdAfx.h"
#include "polygon.h"

Polygon::Polygon(int num_sides) : sides(num_sides) {}


triangle.h:
1
2
3
4
5
6
7
8
9
10
11
12
#include "polygon.h"

class Triangle : public Polygon
{
private:
	int base, height;
public:
	Triangle();
	Triangle(int t_base, int t_height, int t_sides);
	int Perimeter();
	double ComputeArea();
};


triangle.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "StdAfx.h"
#include "Triangle.h"
#include <math.h>

Triangle::Triangle(int t_base, int t_height, int t_sides) : 
	base(t_base), height(t_height), Polygon(t_sides) {}

int Triangle::Perimeter()
{
	double d_height = this->height;
	double d_base = this->base;
	double hyp = sqrt((d_height * d_height) + (d_base * d_base));
	int perimeter = (2 * hyp) + d_base;
	return perimeter;
}

double Triangle::ComputeArea()
{
	double area;
	area = 0.5 * this->base * this->height;
	return area;
}


rectangle.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
#include "stdafx.h"
//#ifndef polygon_h
//#define polygon_h
#include "polygon.h"

//class Extrusion;

class Rectangle : public Polygon
{
private:
	int width, height;
public:
	Rectangle();
	Rectangle(int rec_width, int rec_height, int sides);
	void PrintSides();
	Rectangle operator* (int multiplier);
	friend bool operator< (int value, Rectangle& rec);
	friend class Extrusion;
	int Perimeter();
	double ComputeArea();
};

bool operator< (int value, Rectangle& rec);

//#endif 


rectangle.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
#include "stdafx.h"
#include "rectangle.h"
#include <iostream>

using namespace std;

Rectangle::Rectangle(int rec_width, int rec_height, int rec_sides) : 
	width(rec_width), height(rec_height), Polygon(rec_sides) {}

Rectangle Rectangle::operator* (int multiplier)
{
	Rectangle return_rec(1, 1, 4);
	return return_rec;
}

int Rectangle::Perimeter()
{
	int perimeter = (this->height * 2) + (this->width * 2);
	return perimeter;
}

double Rectangle::ComputeArea()
{
	double area;
	area = this->height * this->width;
	return area;
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include "Tweeter.h"
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include "Triangle.h"
#include "rectangle.h"
#include "extrusion.h"
#ifndef _polygon_h
#define _polygon_h

using namespace std;

...
...
...
Topic archived. No new replies allowed.