-
Notifications
You must be signed in to change notification settings - Fork 93
/
Area.cpp
56 lines (55 loc) · 1.12 KB
/
Area.cpp
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
// A Template function area that calculates are of different shapes.
#include<iostream>
#include<cmath>
using namespace std;
template<class T>
T area(T l, T w, T h)
{
return 2 * (l * w + w * h + h * l); //for cube
};
template<class T>
T area(T l, T w)
{
return l * w;
};
template<class T>
T area(T r)
{
return M_PI * r * r;
};
int main()
{
int choice = 0;
while(choice != 4)
{
cout << "\nCalculate area for:\n1 -> Cuboid\n2 -> Square/Rectangle\n3 -> Circle\n4 -> Exit\nEnter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
float l, w, h;
cout << "\nEnter length, width and height for the cuboid: ";
cin >> l >> w >> h;
cout << "\nArea: " << area<float>(l, w, h) << endl;
break;
case 2:
float x, y;
cout << "\nEnter length and width for square/rectangle: ";
cin >> x >> y;
cout << "\nArea: " << area<float>(x, y) << endl;
break;
case 3:
float r;
cout << "\nEnter the radius of the circle: ";
cin >> r;
cout << "\nArea: " << area<float>(r) << endl;
break;
case 4:
break;
default:
cout << "\nInvalid Input\n";
break;
}
}
return 0;
}