I am having trouble with polymorphism

I think my classes were coded correctly. I cannot change "User.cpp".

I think I have an issue with my pointers, I am not 100% sure.

The code takes a pre-inputted double value and calculates the area and perimeter. The idea is to use polymorphism.

Circle.h
1
2
3
4
5
6
7
8
9
10
#pragma once
#include "Shape.h"

const class Circle : public Shape
{
public:
	explicit Circle(double num);
	virtual double area(double num);
	virtual double perimeter(double num);
};


Rectangle.h
1
2
3
4
5
6
7
8
9
10
#pragma once
#include "Shape.h"

class Rectangle : public Shape
{
public:
	explicit Rectangle(double length, double width);
	virtual double area(double length, double width);
	virtual double perimeter(double length, double width);
};


Shape.h
1
2
3
4
5
6
7
#pragma once
const class Shape
{
public:
	virtual double area(double num, double num1);
	virtual double perimeter(double num, double num1);
};


Square.h
1
2
3
4
5
6
7
8
9
10
#pragma once
#include "Shape.h"

class Square : public Shape
{
public:
	explicit Square(double num);
	virtual double area(double num);
	virtual double perimeter(double num);
};


Circle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Circle.h"

Circle::Circle(double num)
{
	area(num);
	perimeter(num);
}

double Circle::area(double num)
{
	return (3.14 * (num * num));
}

double Circle::perimeter(double num)
{
	return (3.14 * num * 2);
}


Rectangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Rectangle.h"

Rectangle::Rectangle(double length, double width)
{
	area(length, width);
	perimeter(length, width);
}

double Rectangle::area(double length, double width)
{
	return length * width;
}

double Rectangle::perimeter(double length, double width)
{
	return ((2 * length) + (2 * width));
}


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

#include "Shape.h"

using namespace std;

double Shape::area(double num, double num1)
{
	cout << "The Area is unknown" << endl;
	return 0;
}

double Shape::perimeter(double num, double num1)
{
	cout << "The Perimeter is unknown" << endl;
	return 0;
}


Square.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Square.h"

Square::Square(double num)
{
	area(num);
	perimeter(num);
}

double Square::area(double num)
{
	return num * num;

}

double Square::perimeter(double num)
{
	return (4 * num);
}


User.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include "Circle.h"
#include "Rectangle.h"
#include "Shape.h"
#include "Square.h"

using namespace std;

int main()
{
	double side = 5.0;
	double length = 3.0;
	double width = 2.0;
	double radius = 1.0;

	Square s(side);
	Rectangle r(length, width);
	const Circle unit_circle(radius);

	const Shape* shapes[3] = { &s, &r, &unit_circle };

	for (const Shape* s : shapes)
	{
		cout << "The perimeter is " << s->perimeter() << endl;
		cout << "The area is " << s->area() << endl;
	}
}
1
2
3
const class Shape …
const class Circle … 
//... 

const qualifiers can only be specified for objects and functions
also class Shape might just as well be an abstract base class with 2 pure, virtual functions (area()const, perimeter()const) and a virtual destructor

Topic archived. No new replies allowed.