Inheritance with classes of shapes?

Hi! I'm trying to write code where I can get the perimeter of a rectangle, and then the area of a rectangle, triangle, and square using the get_area() functions.
The classes Triangle and Rectangle are derived from Shape, and the class Square is derived from Rectangle.
My issue is that when I run my code as is, I get the error, in Rectangle.h, Rectangle::Rectangle(float,float) candidate expects 2 arguments, 0 provided
and the error Rectangle::Rectangle(const Rectangle&) candidate expects 1 argument, 0 provided
along with an error, in Square::Square(float) saying, no matching function for call to Rectangle::Rectangle()

I thought I had 2 arguments in my Rectangle constructor. And I'm not sure where I have Rectangle::Rectangle(const Rectangle&).

Another problem is that I'm not sure if I'm using inheritance correctly as I haven't used area or perimeter from Shape.

Here is my code thus far:

Shape.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef SHAPE_H
#define SHAPE_H

class Shape{	// This is the base class!

	public:
		Shape();
		float get_area();

	private:
		float area, perimeter;

};

#endif 


Shape.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Shape.h"
#include "Triangle.h"
#include "Rectangle.h"
#include "Square.h"
#include <iostream>

using namespace std;


Shape::Shape(){
	area = 0;
	perimeter = 0;
}

float Shape::get_area(){
	return area;
}


Triangle.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef TRIANGLE_H
#define TRIANGLE_H

class Triangle : public Shape{

	public:
		Triangle(float base, float height);
		float get_area();

	private:
		float base, height;

};


#endif 


Triangle.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Shape.h"
#include "Triangle.h"
#include "Rectangle.h"
#include "Square.h"
#include <iostream>

using namespace std;


Triangle::Triangle(float base, float height){
	base = base;
	height = height;
}

float Triangle::get_area(){
	return (0.5*base*height);
}


Rectangle.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef RECTANGLE_H
#define RECTANGLE_H


class Rectangle : public Shape{

	public:
		Rectangle(float width, float length);
		float get_area();
		float get_perimeter();

	private:
		float width, length;

};

#endif 


Rectangle.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Shape.h"
#include "Triangle.h"
#include "Rectangle.h"
#include "Square.h"
#include <iostream>

using namespace std;


Rectangle::Rectangle(float width, float length){
	width = width;
	length = length;
}

float Rectangle::get_area(){
	return width*length;
}

float Rectangle::get_perimeter(){
	return 2*(length+width);
}



Square.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef SQUARE_H
#define SQUARE_H


class Square : public Rectangle{

	public:
		Square(float side);

	private:

};

#endif 



Square.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
#include "Shape.h"
#include "Triangle.h"
#include "Rectangle.h"
#include "Square.h"
#include <iostream>

using namespace std;


Square::Square(float side){
	Rectangle r(side,side);
}



main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Shape.h"
#include "Rectangle.h"
#include "Triangle.h"
#include "Square.h"
#include <iostream>

using namespace std;


int main(){


	Rectangle rectangleObject(2,5);
	Triangle triangleObject(1,10);
	Square squareObject(5);

	cout << "The area of the rectangle is" << rectangleObject.get_area() << '\n';
	cout << "The perimeter of the rectangle is " << rectangleObject.get_perimeter() << '\n';
	cout << "The area of the triangle is " << triangleObject.get_area() << '\n';
	cout << "The area of the square is " << squareObject.get_area() << '\n';

	return 0;
}
Last edited on
Anyone? D:
closed account (D80DSL3A)
I wonder if it's your Square class constructor:
1
2
3
Square::Square(float side){
	Rectangle r(side,side);
}

Here, r is a local variable not the Rectangle base object that Square is derived from. Try this:
Square::Square(float side): Rectangle(side,side) {}// calling the 2-arg constructor for the base object
Oh, I see! I did not know about that. It compiles now! Thanks! :D

Except, my output is:
The area of the rectangle isnan
The perimeter of the rectangle is nan
The area of the triangle is 0
The area of the square is 0


I'm really stumped as to why the areas are not calculating in get_area
closed account (D80DSL3A)
It may be your Rectangle and Triangle constructors:
1
2
3
4
Rectangle::Rectangle(float width, float length){
	width = width;// may not be assigning the class member values here
	length = length;// may be assigning the parameter to itself instead
}

I would use different names for the parameters, maybe:
1
2
3
4
Rectangle::Rectangle(float Width, float Length){
	width = Width;
	length = Length;// no confusion now
}

Similarly for base and height in the Triangle constructor.

EDIT: Re the class inheritance. I haven't seen the assignment description, but I'm guessing that the 2 functions get_area() and get_perimeter() are supposed to be virtual. Probably should be pure virtual in the base class (because no definitions can be written for the Shape class). So in the Shape class:
1
2
virtual float get_area() = 0;
virtual float get_perimeter() = 0;

I don't think the float area, perimeter; members are needed there either.

You then define virtual versions of get_area() and get_perimeter() in each derived class, except for Square because the definitions inherited from the Rectangle class are correct already for it.
Just add the virtual keyword before the function defs. in Triangle and Rectangle. Example:
1
2
3
virtual float Rectangle::get_area(){
	return width*length;
}
Last edited on
Oh, whoops.
It all works now!

We haven't learned about virtual functions, yet. But I know we are soon so I'm excited :D

Thanks for your help and explanations! It's uber appreciated.
You could alternatively do
1
2
3
4
Rectangle::Rectangle(float width, float length){
	this->width = width; //this is a pointer to the current object
	this->length = length; //-> is the access operator
}
though it is best if you have unique names or use initalizer list like:
1
2
Rectangle::Rectangle(float width, float length)
: width(width), length(length){}


*fixed code tags
Last edited on
Topic archived. No new replies allowed.