Virtual Functions

closed account (zqoE8vqX)
I'm creating a geometric calculator for a class assignment. I've read and read, but I can't seem to understand how to create a function to calculate the area within each inherited class (rectangle, square, etc.) off the virtual function in the base class (GeometricFigure). Can someone explain in simple terms how to do this and show me how to ask a user to input the "width" and "height" and implement it in each inherited class function? Please and thank you!

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
  #include <iostream>
using namespace std;

class GeometricFigure
{
private:
	double height, width, area;
public:
	void setHeight( double h ){ height = h; }
	double getHeight(){ return height; }
	void setWidth( double w ){ width = w; }
	double getWidth(){ return width; }
	void setArea( double a ){ width = a; }
	double getArea(){ return area; }

	virtual void ComputeArea() = 0; //using it polymorphically
};

class Rectangle : public GeometricFigure
{
public:
	void ComputeArea(void)
	{
		cout<<"Enter the width of the rectange: "; 
		cin>>w; //the compiler ells me this is undefined
		cout<<"Enter the height of the rectangle: ";
		cin>>h; //the compiler ells me this is undefined
	}

};

class Square : public Rectangle
{
public:
	double getArea()
	{
		GeometricFigure::ComputeArea();
	}
};

class Triangle : public GeometricFigure
{
double getArea()
	{
		GeometricFigure::ComputeArea();
	}
};

int main()
{
	Rectangle r;
	Square s;
	Triangle t;
	GeometricFigure *shape1 = &r;
	GeometricFigure *shape2 = &s;
	GeometricFigure *shape3 = &t;
	int choice;

	cout<<"Geometric Calculator"<<endl;
	cout<<"1. Calculate the Area of a Rectangle"<<endl;
	cout<<"2. Calculate the Area of a Square"<<endl;
	cout<<"3. Calculate the Area of a Triangle"<<endl;
	cin>>choice;

	switch(choice)
	{
		case 1:
			system("cls");
			//shape1->ComputeArea();
			break;
		case 2:
			cout<<"Enter the width of the square: ";
			cin>>s.setWidth;
			cout<<"Enter the height of the square: ";
			cin>>s.setHeight;
			system("cls");
			//shape2->ComputeArea();
			break;
		case 3:
			cout<<"Enter the width of the triangle: ";
			cin>>t.setWidth;
			cout<<"Enter the height of the triangle: ";
			cin>>t.setHeight;
			system("cls");
			//shape3->ComputeArea();
			break;
		default:
			cout<<"Invalid entry"<<endl;
	}

	system("Pause");
	return 0;
}
This might help to make virtual functions more clear:

http://stackoverflow.com/questions/2391679/can-someone-explain-c-virtual-methods

In your Rectangle class you should be reading in 2 numbers and then using your setWidth and setHeight functions. reading directly into width and height is not defined by your Geometric shape class.
Last edited on
closed account (zqoE8vqX)
Thank you!
Topic archived. No new replies allowed.