simple inheritance

This is a discussion on simple inheritance within the C++ Programming forums, part of the General Programming Boards category; hi, ive just started doing inheritance...and im not sure what exactlty i should put in .h files and .cpp files... ...

  1. #1
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69

    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)

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    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++

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.
    Last edited by 7stud; 06-11-2005 at 12:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 08:42 AM
  2. Inheritance and program structure planning please help a newbie
    By ninjacookies in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2005, 01:18 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM
  5. Simple inheritance example??
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-18-2001, 06:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21