To evaluate the following equation A=B%3, where A and B are two objects of the same class. Develop a C++ program to implement this using operator overloading with friend function.
#include<iostream>
using namespace std;
class Class
{
public:
int a;
static int count;
Class()
{
++count;
}
void set_a(int a)
{
this->a =a;
}
void get_a()
{
cout<<"Value of object"<<Class::count<<" is : "<< a <<endl;
}
friend float operator %(const Class& ob1, const Class& ob2);
};
int Class :: count =0;
float operator%(const Class& ob1, const Class& ob2)
{
return (int)ob1.a % (int)ob2.a;
}
int main()
{
Class A;
cout<<"Enter value for object"<<Class::count<<" : ";
int val;
cin>>val;
A.set_a(val);
cout<<"Enter value for object"<<Class::count<<" : ";
int val1;
cin>>val1;
cout<<endl;
A.get_a();
Class B;
B.set_a(val1);
int ans = operator %(A,B);
B.get_a();
cout<<endl<<"The value after operator overloading friend function % for object1 and object2 is : ";
cout<<ans;
}
Comments
Leave a comment