Inheritance and Polymorphism

I need help writing the formulas in my implementations. I also need help instantiating and populating random Circle and Rectangle objects into an array of “Shape pointer” type, making the array a size of 5.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  #include <iostream>
#include <string>
#include "Classes.h"
using namespace std;

int main()
{
	Shape* ptr1[5];
	/*ptr1[0] = new Circle();
	ptr1[1] = new Circle();
	ptr1[2] = new Circle();
	ptr1[3] = new Rectangle();
	ptr1[4] = new Rectangle();*/

	for (int i = 0; i < 5; i++)
	{
		cout << "-------" << endl;
		ptr1[i]->getArea();
	}
	return 0;
}





#include <string>
#include <iostream>
using namespace std;

//Shape
class Shape
{
public:
	virtual void getArea() const = 0;
};



//Circle
class Circle : public Shape
{
public:
	void getArea() const;
	Circle();
	Circle(double);
protected:
	double m_radius;
};



//Rectangle
class Rectangle : public Shape
{
public:
	void getArea() const;
	Rectangle();
	Rectangle(double, double);
protected:
	double m_height;
	double m_width;
};





#include <string>
#include <iostream>
#include "Classes.h"
using namespace std;


void Shape::getArea() const
{
	cout << "Area: " << endl;
}



Circle::Circle() :Shape()
{
	m_radius = 1;
}
Circle::Circle(double p_radius) :Shape()
{
	m_radius = p_radius;
}
void Circle::getArea() const
{
	Shape::getArea();
	cout << "Radius: " << m_radius << endl;
}



Rectangle::Rectangle() :Shape()
{
	m_height = 1;
	m_width = 1;
}
Rectangle::Rectangle(double p_height, double p_width) :Shape()
{
	m_height = p_height;
	m_width = p_width;
}
void Rectangle::getArea() const
{
	Shape::getArea();
	cout << "Height: " << m_height << endl;
	cout << "Width: " << m_width << endl;
}
Topic archived. No new replies allowed.