Skip to main content

*Create and Implement program in C++ to define 2 classes. Addition, subtraction, division, average, power and multiplication of 2 variables must be performed using functions.*


#include <iostream>
using namespace std;
class B;
class A{int a,b;
public:
friend  class B;};
class B{int x,power;
float base_no;
public:
friend  class A;
void calculator (A o1) {
int result = 1;
cout << "\nEnter the two number :" <<endl;
cin>>o1.a>>o1.b;
cout << "Enter Choise \nfor Multiply  (*) press 1:\t\nFor subtraction (-) press 2:\t\n For Addition(+) press 3:\t\n For Division (/)press 4:\nFor Average (avg)press 5:\n" <<endl;
cin>>x;
if (x==1){ cout<< "The Multiply of two no is :" <<o1.a*o1.b<<endl; }
else if (x==2) {cout<< "The subtraction of two no is :" <<o1.a-o1.b<<endl;   }
else if (x==3) {  cout<< "The sum of two no is :" <<o1.a+o1.b<<endl;  }
else if (x==4)  {   cout<< "The division of two no is :" <<o1.a/o1.b<<endl;  }
else if (x==5)  {   cout<< "The Average of two no is :" <<(o1.a+o1.b)/2<<endl;  }
cout << " \t<< Enter base and exponent >>:  "<<endl;
cin >> base_no >> power;
cout << base_no << "^" << power << " = ";
while (power != 0) {
result *= base_no;
--power; }
cout << result;}};
int main(){
int c,time,x;
A obj;
B o;
o.calculator(obj);
cout<< "\nDo you want to Run again Calculator function \n then press 1  \nFor Exit press 0 :" <<endl;
cin>>c;
if (c==1){cout<< "\nHow many time to run calculator function:" <<endl;
cin>>time;
for(int i=0;i<time;i++){
o.calculator (obj);}}
else {return 0;}
return 0;}
 Output:






Comments