Write a C++ program which perform the following:
1. Create a class
2. Create two objects and assign the values
3. Now compare the object values by using the Operator
Overloading on the following operator: a. Operator Overloading (>)
4. And print if the value of which object is greater.
#include<iostream>
using namespace std;
class Coder{
private:
int first;
public:
Coder(): first(0) {} // Constructor initializing first and second to 0
Coder(int s){
first = s;
}
//Overload > operator
Coder operator >(const Coder& b){
Coder code;
code.first = first;
if(b.first > first){
code.first = b.first;
}
return code;
}
void display(){
cout<<"Output larger number is\t "<<first;
}
};
int main(){
Coder c1(10), c2(89);
Coder c3 = c1>c2;
c3.display();
}
Comments
Leave a comment