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;
}
|