Class or Function?

Hi guys sorry im really stucked trying to make a progrm that calculates areas of trianfgles rects and circles but i cant manage to figure out a way of making the main function to call the classes made for each figure.
What i have in mind is making like individual classes for each figure and let the user choose 1, 2, or 3 for Rect, Circ , or Triangle.
how can you help me. Thanks. im waiting :)
The thing is that i dont understand how to call like a class into the main function. Like asking the user which figure it needs to be calcĀ“d so depending of the choice, call Circle Class, TriangleĀ“s etc..
thanks sorry :(

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
#include <iostream>
#include <cmath>
using namespace std;
int main (){
	double choice;
	cout<< ("Please enter: ") << endl;
	cout<< ("1) To calculate a Rectangle's Area.") << endl;
	cout<< ("2) To calculate a Triangles's Area.") << endl;
	cout<< ("3) To calculate a Circle's Area.") << endl;
	cin>> choice;


}

double areas (){
	//Rect
		
		double w, l, h, ArRect;

      cout << "Rectangle Area" << endl << ("Please enter the width, length and height.");
      cin >> w >> l >> h;
	  
	  ArRect = w * l * h;

	  cout << ("The Area of the Box is: ") << ArRect << endl;

system ("Pause");

return 0;

//Triangle
		double l1, l2 ,l3, s, ArTri;
		   

	cout<< ("Please enter the values for the three  sides.") << endl;
		cin >> l1 >> l2 >> l3;
		s = ((l1 + l2 + l3)/2);
		ArTri = (sqrt(s*(s-l1) * (s -l2) *(s-l3)));

		cout << ("The Triangle's Area is: ") << ArTri << endl;

		system ("Pause");
		return 0;


double ACir, R;
		double Pi = 3.1416;

		cout<< ("Please Enter the value of the Radius of the Circle.") << endl;
		cin>> R;

		ACir = (Pi * (pow(R,2)));
		cout<< ("The Circle's Area is: ") << ACir << endl;

system ("Pause");
}
Last edited on
closed account (18hRX9L8)
You need three different functions: mainly triangle(), circle(), and rectangle(). Then from main, you can call these functions based on user input:

Pseudo-Code:
1
2
3
4
5
6
7
8
9
main() {
     while(true) {
          get_option()
          if option == 1: rectangle();
          else if option ==  2: triangle();
          else if option == 3: circle();
          else: std::cout << "Please enter a valid parameter." << std::endl;
     }
}


Similarly, you can use the switch statement instead of a bunch of ifs.
Last edited on
Topic archived. No new replies allowed.