Introduction to Inheritance Using Shapes

Hi all, I am taking an Intro to C++ class and we just started learning about inheritance. One of our assignments asks us to make a program that has the user input a shape and then parameters for the shape and the program outputs the area and perimeter of each shape as well as the totals of each. Right now, my code is currently
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
#include <iostream>
#include <cmath>

using namespace std;

class shape{
protected:
	double area;
	double perimeter;
public:
	shape();
	shape operator+(shape s2);
	friend ostream& operator<< (ostream& out, shape s);
	void setup();
};

class circle : public shape{
private:
	double radius;
};

void shape::setup(){
}

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

shape shape::operator +(shape s2){
	shape temp;
	temp.area = area + s2.area;
	temp.perimeter = perimeter + s2.perimeter;
	return temp;
}

ostream& operator<< (ostream& out, shape s){
	out << "Area: " << s.area << endl << "Perimeter: " << s.perimeter << endl;
	return out;
}

int main()
{
	shape** list = new shape*[1000];
	int size=0;
	string choice;
	do
	{
		cout << "Do you want to add a (c)ircle, (t)riangle, (h)exagon, (r)ectangle or (q)uit?\n";
		getline(cin,choice);
		
		shape* type;
		if(choice.length()==0)
		{
			continue;
		}
		else if(tolower(choice[0]) == 'c')
		{
			type = new circle();
		}
		else if(tolower(choice[0]) == 't')
		{
			type = new triangle();
		}
		else if(tolower(choice[0]) == 'h')
		{
			type = new hexagon();
		}
		else if(tolower(choice[0]) == 'r')
		{
			type = new rectangle();
		}
		else
		{
			continue;
		}

		type->setup();
		
		list[size] = type;
		size++;

		string junk;
		getline(cin,junk);
		
	}while(choice.length()==0 || tolower(choice[0]) != 'q');

	shape total;
	for(int i=0; i < size; i++)
	{
		cout << "Shape " << i+1 << ":\n" << *list[i] << endl;
		total = total + *list[i];
	}
	cout << "Totals:\n" << total << endl;


	// memory!
	for(int i=0; i < size; i++)
	{
		delete list[i];
	}
	delete [] list;
}


I understand I am missing classes for the other shapes, I am wondering how I should go about writing the circle class as well as the setup function. We are not allowed to change the main in any way. My main confusion is how the setup function will know which shape I want to use to make the calculation. Any help is very much appreciated!
My main confusion is how the setup function will know which shape I want to use to make the calculation.


setup function doesn't know anything. The real question is really "when I call function setup through a pointer to shape, how does the right setup function get called?"

The answer is that you make setup a virtual function, and then when you call it through pointer to shape, the most derived implementation of setup in that real object gets called.
Last edited on
Topic archived. No new replies allowed.