Back with some inheritance fun

This is a discussion on Back with some inheritance fun within the C++ Programming forums, part of the General Programming Boards category; I'm having a heckuva time with this one.. basically, I am trying to create a squareShape class that inherits the ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    72

    Back with some inheritance fun

    I'm having a heckuva time with this one.. basically, I am trying to create a squareShape class that inherits the properties, etc of a rectShape class, which in turn inherits the properties of a shape class.

    The book I am using provides the rectShape class (declaration and implementation) and the exercise asks that I create the squareShape class with the inherited properties of the rectShape class, while using inline functions. I'll show the applicable code, then ask my silly questions.

    rectShape class
    Code:
    #ifndef RECTANGLESHAPE_CLASS
    #define RECTANGLESHAPE_CLASS
    
    #include "d_shape.h"
    
    // declaration of rectShape class with base class shape
    class rectShape: public shape
    {
       public:
          rectShape(double x = 0.0, double y = 0.0,
                    double len = 0.0, double wid = 0.0,
                    shapeColor c = darkgray);
    			// constructor. has parameters for the base point,
    			// length, width and color
    
          double getLength() const;
          double getWidth() const;
          void setSides(double len, double wid);
    	      // retrieve or set rectangle dimensions
    
          virtual void draw();
    	      // draw the rectangle
    
       private:
          double length, width;
    			// rectangle dimensions
    
    };
    
    rectShape::rectShape(double x, double y, double len, double wid,
                         shapeColor c):
                shape(x,y,c), length(len), width(wid)
    {}
    
    double rectShape::getLength() const
    {
       return length;
    }
    
    double rectShape::getWidth() const
    {
       return width;
    }
    
    void rectShape::setSides(double len, double wid)
    {
       length = len;
       width = wid;
    }
    
    void rectShape::draw()
    {
       EZDCOLORVAL old_color;
       
       old_color = ezdSetColor(color.convertToEzdColor());
    
       // execute primitive function and draw the rectangle
       shape_handle = ezdDrawRectangle(baseX, baseY,
                                       baseX+length,baseY+width);
    
       ezdSetColor(old_color);
    }
    
    #endif   // RECTANGLESHAPE_CLASS
    Now here's the squareShape class that I have been working on:

    Code:
    include <iostream>
    #include "d_rectsh.h"
    
    class squareShape: public rectShape
    {
    public:
    	squareShape(double xpos = 0.0, double ypos= 0.0, double side = 0.0, shapeColor c = darkgray):
    	  rectShape(xpos, ypos,s,side,c)
    	  { 
    	  }
    
    	double getSide() const
    	{
    		return s;
    	}
    
    	void setSide(double s)
    	{
    	   s=side;
    	}
    		
    private:
    	double s;
    };
    Finally, here's my simple bit of code that should instantiate a squareShape object with the properties shown in the code:

    Code:
    #include <iostream>
    #include "sqshape.h"
    
    int main ()
    {
    	squareShape sq(4.0,4.0,3.0,red);
    	
    	return 0;
    }
    Now for the silly questions:

    1. From all the examples I have seen of inheritance in the books I've read, the derived class' constructor always seems to have the same amount of arguments as the base class. In this example, that is not true. The derived class (squareShape) has 4 arguments, but the rectShape (base) class requires 5 arguments. From what I can tell, you don't ever use the same argument twice, is that correct? And if that is correct, how do I tackle this issue?

    2. The main error that I am getting is that the variable 'side' is an undeclared identifer. I've also tried the following with my class constructor and it still brings me the same error msg:

    Code:
    squareShape(double xpos = 0.0, double ypos= 0.0, double side = 0.0, shapeColor c = darkgray):
    	  rectShape(xpos, ypos,s,side,c), side(s)
    	  { 
    	  }
    I have also tried it with s(side) on the end of it instead of side(s) and that didn't work either.

    3. The other thing (from what I understand) of inhertiance is that the derived class(or classes) should have access to the base class/classes member functions. The rectShape class has a draw function in it that I can't seem to access after I instantiate my squareShape object. That leads me to believe that my inheritance code is off, but I am not sure where I am off.

    Thanks very much for any help/clarification that you all can give!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well a square is going to have length = width. So we can use that for the rectShape ctor.

    Nothing wrong with using that param twice.
    Code:
    squareShape(double xpos = 0.0, double ypos= 0.0, double side = 0.0, shapeColor c = darkgray):
      rectShape(xpos, ypos,side,side,c) { }
    You'll have access to public/protected members and methods in the derived class when you use public inheritance.

    I don't see why you couldn't access the Draw method, that should be no problem. You do know about the virtual keyword and what it does correct? I'll assume yes since you didn't ask about it.

    Let me know if you have some other questions.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    Hello... I am vaguely familiar with the virtual keyword.. I know it's put in front of member functions and (if I understand it correctly) it goes into a derived and acts as though it is really a part of the base class.. Kind of a convoluted answer, I know...

    The main quandry that I am in is the fact that the compiler (VC++ 6.0) complains that 'side' is an undeclared identifier. I know that there's some way to correlate the 's' variable from my private portion of the squareShape class and the 'side' argument from my constructor, but I guess I am not going about it the proper way?

    Thanks!

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Oh I see what you're saying, yea you have it backwards...it should be:

    Code:
    squareShape(double xpos = 0.0, double ypos= 0.0, double side = 0.0, shapeColor c = darkgray):
      rectShape(xpos, ypos,s,side,c), s(side)
      { 
      }
    member_variable(param)
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    I just tried it with the s(side) on the end of it and it still acts as though 'side' is an undeclared identifier. I feel like I am overlooking something really simple, but I can't put my finger on what...

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay, I was looking at your ctor ( and that was wrong also.. ) but you're problem now is in your setSide method...Look what you named the parameter to the function and look what you named your member variable in your class. There is a problem.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    I think I follow you.. just to clarify though, you're saying that the problem is because I am using 's' as my private data member and 's' is also being used as the argument passed to the setSides member function?

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Code:
    // stuff we don't care about....
        
        void setSide(double s)
        {
           s=side;
        }
    
      private:
        double s;
    };
    That's your code. So someone calls setSide and passes in a number. Well you say s = side; What is side? I don't see it anywhere in your class or the base classes. So it's undeclared. Also the param is 's' and your member variable is 's'. Which are you talking about? Although I would strongly suggest renaming either the member variable or your param you can work around this by saying

    Code:
    this->s = s;
    That's ugly and I wouldn't suggest it especially in this case. Hopefully that clears it up, there is no 'side' variable that I can see, maybe that's what you meant the member variable to be named but somehow it got named 's'.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    Thanks for your help... I think that's got me going in the right direction.. The only thing I can't really grasp at this point is why the draw() function isn't available to my instantiated squareShape object? The draw function is in the rectShape class and is a virtual function, which means it should be accessable I think??

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Just try calling the method, it should work no problem. If not, what's the error?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    Here's what I do in my main code:

    Code:
    #include <iostream>
    #include "sqshape.h"
    
    int main ()
    {
    	squareShape sq(4.0,4.0,3.0,red);
        sq.draw();
    
    	return 0;
    }
    It doesn't output anything to the screen,b ut it does compile now. Typically with VC++, when I instantiate the object and then begin to type it in (i.e. sq with a ".") it will bring up the list of member functions and data members. The only data members and member functions on the list for me to choose from are the ones from the sqshape.h file and not from the rectShape class (which is supposed to be the base class). This must be why it's not drawing the shape, but I am not sure where I went wrong with the inheritance. I made the changes that you suggested in the sqshape.h file and it now compiles, so I am a bit mystified...

  12. #12
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    class rectShape: public shape{
    ...
    
    private: <-- change this to protected:  
    
          double length, width;
    also, possibly the private data members in class "shape" have to be protected as well
    Last edited by alphaoide; 01-17-2004 at 12:22 AM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 10:19 PM
  2. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 02:10 AM
  3. Some woman back ended my car today, and back hurts
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 08-20-2003, 01:42 AM
  4. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM

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