In this post, we will see Menu Driven Programming in C++ using Class with Example.
Applications:
1. Better
User Interaction
2.
Reusability
How to
implement:
By using
1.
Looping Statements
2. Switch Statement
Program Code:
#include<iostream>
using namespace std;
class Calc
{
public:
void show_Choices()
{
cout << "MENU" << endl;
cout << "1: Addition " << endl;
cout << "2: Subtraction" << endl;
cout << "3: Multiply " << endl;
cout << "4: Divide " << endl;
cout << "5: Exit " << endl;
cout << "Enter your choice :";
}
float addition(float num1, float num2)
{
return num1 + num2;
}
float subtraction(float num1, float num2)
{
return num1 - num2;
}
float multiply(float num1, float num2)
{
return num1 * num2;
}
float divide(float num1, float num2)
{
return num1 / num2;
}
};
int main()
{
Calc obj;
float x, y;
int choice;
do
{
obj.show_Choices();
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter the two numbers: ";
cin >> x >> y;
cout << "The Sum is " << obj.addition(x, y) << endl;
break;
case 2:
cout << "Enter the two numbers: ";
cin >> x >> y;
cout << "The Difference is " << obj.subtraction(x, y) << endl;
break;
case 3:
cout << "Enter the two numbers: ";
cin >> x >> y;
cout << "The Product is " << obj.multiply(x, y) << endl;
break;
case 4:
cout << "Enter the two numbers: ";
cin >> x >> y;
cout << "The Quotient is " << obj.divide(x, y) << endl;
break;
case 5:
break;
default:
cout << "Invalid Input" << endl;
}
}
while (choice != 5);
return 0;
}
No comments:
Post a Comment