-
simple inheritance
hi,
ive just started doing inheritance...and im not sure what exactlty i should put in .h files and .cpp files... heres what im trying to do...currently im getting compile errors...
Shape.h
Code:
class Shape {
protected:
int length, width;
public:
Shape (int l, int w) { length = l; width = w;}
virtual int area (void) = 0;
};
Square.h
Code:
#include "Shape.h"
class Square: public Shape {
public:
Square(int l, int w): Shape(l,w){}
int area(void);
};
Square.cpp
Code:
#include "Square.h"
#include "Shape.h"
int Square::area (void) {
return length*width;
}
note this doesnt compile...i get the following errors..
In file included from Square.cpp:1:
Square.h:1: error: parse error before `{' token
Square.h:6: error: parse error before `}' token
Square.cpp:3: error: invalid use of undefined type `class Square'
Square.h:1: error: forward declaration of `class Square'
Square.cpp: In member function `int Square::area()':
Square.cpp:4: error: `length' undeclared (first use this function)
Square.cpp:4: error: (Each undeclared identifier is reported only once for each
function it appears in.)
Square.cpp:4: error: `width' undeclared (first use this function)
-
First you're including twice the header Shape, so you need to properly wrap it with precompiler ifdef's to prevent symbol redeclaration.
Second you should declare area in Square also as virtual (although the effect is the same).
Third that source compiles well on my VC++6 and g++
-
Hi,
When you include a header file, the header file is inserted at the spot of the include statement. And, a header file can only appear once in a file. In your program, in the file Square.cpp, you include Square.h. If you take Square.h and insert it in Square.cpp, you get this:
Square.cpp
-------------
Code:
//#include "Square.h":
#include "Shape.h"
class Square: public Shape {
public:
Square(int l, int w): Shape(l,w){}
int area(void);
};
#include "Shape.h"
int Square::area (void) {
return length*width;
}
But, now you are including Shape.h twice, which produces this:
Square.cpp
-------------
Code:
//#include "Shape.h":
class Shape {
protected:
int length, width;
public:
Shape (int l, int w) { length = l; width = w;}
virtual int area (void) = 0;
};
class Square: public Shape {
public:
Square(int l, int w): Shape(l,w){}
int area(void);
};
//#include "Shape.h":
class Shape {
protected:
int length, width;
public:
Shape (int l, int w) { length = l; width = w;}
virtual int area (void) = 0;
};
int Square::area (void) {
return length*width;
}
C++ does not tolerate redeclarations of the same class or variable, so that is the reason you can only include a header file once. As it turns out, including header files can be a tricky business when a program has lots of files: it can be nearly impossible to track what header files are being included by other header files you are including that themselves include yet other header files. C++ has a solution: you can use inclusion guards around all your header files. Then, if a header file has already been included once in a file, it won't be included again. You should make it a habit of putting inclusion guards around the code in your header files, and they look like this:
Code:
#ifndef __MYCLASS_H__
#define __MYCLASS_H__
//your code here
endif //__MYCLASS_H__
This part:
__MYCLASS_H__
is just a name and it can be anything. Normally, you use the name of your header file.