### 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 :(

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include #include 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
You need three different functions: mainly `triangle()`, `circle()`, and `rectangle()`. Then from main, you can call these functions based on user input:

Pseudo-Code:
 ``123456789`` ``````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 `if`s.
Last edited on
Topic archived. No new replies allowed.