This is not homework, just some training exercises for my C++ class in order to get used to iheritance and stuff. So the first part of the exercise ask us to create a program which has one class name Rectangle and we should do the constructors getters and setters and find the area and perimeters.This part works fine. The second part of the exercise says to make a new class name Square which extends Rectangle and which has a constructor which will have as an argument the width of the square. Then the program should print the area and the perimeter.
#include <iostream>
using namespace std;
class Rectangular {
private:
int width;
int height;
public:
Rectangular () {
width = 5;
height = 5;
}
Rectangular (int w, int h) {
width = w;
height = h;
}
void setWidth (int w) {
width = w;
}
void setHeight (int h) {
height = h;
}
int getWidth () {
return width;
}
int getHeight () {
return height;
开发者_如何学编程 }
int getArea () {
return width*height;
}
int getPerimeter () {
return width+height;
}
};
class Square : public Rectangular{
public:
Square (int w) {
getWidth();
}
};
int main(int argc,char *argv[])
{
Rectangular a, b(10,12);
Square c(5);
cout << "Width for a: " << a.getArea() << " Perimeter for a: " << a.getPerimeter() << endl;
cout << "Width for b: " << b.getArea() << " Perimeter for b: " << b.getPerimeter() << endl;
cout << "Area for c: " << c.getArea() << " Perimeter for c: " << c.getPerimeter() << endl;
}
The program prints out
Width for a: 25 Perimeter for a: 10
Width for b: 120 Perimeter for b: 22
Area for c: 25 Perimeter for c: 10
For some reason c gets the values of a. Any thoughts?
You forgot to chain the constructor for Square
. You want to change the constructor of Square
to read:
Square(int w) : Rectangle(w,w) {}
Your a
does not define any values in it's constructor therefore its 5x5. The same you specified for your c
(you also defined no initial width/height here - note that you do not call any specific constructor of Rectangular
and thus the default is used). Thus it's not "getting values from" but simply initialized the same way.
Your constructor in the Inherited class Square
should be :
Square(int w) : Rectangular (w, w)
{
/* do what ever */
getWidth();
}
what is the use of getWidth ()
call ? remove it.
EDIT1 :
Note: cout<<"Width for a: "<<a.getArea()<<" Perimeter for a: "<<a.getPerimeter()<<endl;
You have written "Width for a: "
but have actually printed the area, by calling a.get_Area ();
What is happening is that b is created with width
and height
you set (10 and 12 accordingly) and a is created by default with 5 and 5.
If you'll look, your Square
is created using your default constructor, which, because of your getWidth()
call(and overall) creates a parent instance(using it's default constructor) which puts it to be 5 on 5.
Thus you get same a and c.
This exercise is actually a good example of a bad use of inheritance :) Although mathematically a square is always also a rectangle, when you try to model it in C++, it can be tricky.
Example: an instance of LuxuryCar
can always be used where a Car
is expected; however a Square
can't always be used instead of a Rectangle
(e.g. this window).
To illustrate this on your implementation - your Square
inherits the setWidth
and setHeight
methods. If you use them on a square, you break it - it is not a square any more. So you need to override them so that you maintain the constraints of a square. But if you do, I am not sure what you gain by inheriting from Rectangle
. In addition, Square
provides an API (the set of methods) that is inconsistent with its nature (there is no width in square obviously).
精彩评论