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!