C program for calculator uing inheritance
#include<iostream.h>
#include<conio.h>
class Calculate
{
public:
int n1, n2;
int Add()
{
return n1 + n2;
}
int Product()
{
return n1 * n2;
}
float Quotient()
{
if (n2 == 0)
{
cout << "Division by 0 is not possible";
}
else
return n1 / n2;
}
int Remainder()
{
return n1 % n2;
}
};
class Values : public Calculate
{
public:
void input()
{
cin >> n1 >> n2;
}
};
void main()
{
Values obj;
int choice;
char ch = 'Y';
do
{
cout << "Enter 2 values as input : ";
obj.input();
cout << " \n Enter your choice: \n";
cout << " 1.Addition \n ";
cout << " 2.Multiplication \n ";
cout << " 3.Division \n ";
cout << " 4.Remainder \n ";
cout << " 5.Exit \n ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Addition = " << obj.Add();
break;
case 2:
cout << "Product = " << obj.Product();
break;
case 3:
cout << "Division = " << obj.Quotient();
break;
case 4:
cout << "Remainder = " << obj.Remainder();
break;
case 5:
exit(0);
default:
cout << "Invalid choice !!";
}
cout << "\n Want to continue? [Y/N] ";
cin >> ch;
} while (ch == 'Y' || ch == 'y');
getch( );
}
0 Comments