In this, we are going to see a program for Arithmetic Calculator using Single Inheritance in C++ Programming Language.
The Code given below can be used in TURBO C++ Compilers: -
#include<iostream.h>
#include<conio.h>
class Operate
{
public:
int n1, n2;
int Add()
{
return n1 + n2;
}
int Product()
{
return n1 * n2;
}
float Quotient()
{
if (n2 == 0)
{
cout << "Division by 0 NOT possible!";
}
else
return (float)n1 / n2;
}
int Remainder()
{
return n1 % n2;
}
};
class Values : public Operate
{
public:
void input()
{
cin >> n1 >> n2;
}
};
void main()
{
clrscr();
Values obj;
int choice;
char ch = 'Y';
do
{
cout << "Enter 2 numbers: ";
obj.input();
cout << " Enter choice: \n";
cout << " 1.Addition \n 2.Multiplication \n 3.Division \n 4.Find remainder \n 5.Exit \n";
cin >> choice;
switch (choice)
{
case 1:
cout << "Sum = " << obj.Add();
break;
case 2:
cout << "Product = " << obj.Product();
break;
case 3:
cout << "Quotient = " << obj.Quotient();
break;
case 4:
cout << "Remainder = " << obj.Remainder();
break;
case 5:
exit(0);
default:
cout << "Enter valid choice!";
}
cout << "\nDo you want to continue? [Y/N] ";
cin >> ch;
} while (ch == 'Y' || ch == 'y');
getch();
}
//...........Coded by Shreya Idate
The Code given below can be used in gcc/g++ Compilers: -
#include <iostream>
using namespace std;
class Operate
{
public:
int n1, n2;
int Add()
{
return n1 + n2;
}
int Product()
{
return n1 * n2;
}
float Quotient()
{
if (n2 == 0)
{
cout << "Division by 0 NOT possible!";
}
else
return (float)n1 / n2;
}
int Remainder()
{
return n1 % n2;
}
};
class Values : public Operate
{
public:
void input()
{
cin >> n1 >> n2;
}
};
int main()
{
Values obj;
int choice;
char ch = 'Y';
do
{
cout << "Enter 2 numbers: ";
obj.input();
cout << " Enter choice: \n";
cout << " 1.Addition \n 2.Multiplication \n 3.Division \n 4.Find remainder \n 5.Exit \n";
cin >> choice;
switch (choice)
{
case 1:
cout << "Sum = " << obj.Add();
break;
case 2:
cout << "Product = " << obj.Product();
break;
case 3:
cout << "Quotient = " << obj.Quotient();
break;
case 4:
cout << "Remainder = " << obj.Remainder();
break;
case 5:
exit(0);
default:
cout << "Enter valid choice!";
}
cout << "\nDo you want to continue? [Y/N] ";
cin >> ch;
} while (ch == 'Y' || ch == 'y');
return 0;
}
//...........Coded by Shreya Idate
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP