Operator Overloading
Operator Overloading
#include<iostream.h> #include<conio.h> class score { private: int val; public: score() { val=0; } void operator++() { val=val+1; } int show() { return(val); } }; int main() { score s1,s2; cout<<"\n Initial value of s1 object = "<<s1.show(); cout<<"\n Initial value of s2 object = "<<s2.show(); ++s1; ++s1; ++s2; cout<<"\n Final value of s1 object = "<<s1.show(); cout<<"\n Final value of s2 object = "<<s2.show(); getch(); return 0; }
#include<iostream.h> #include<conio.h> class score { private: int val; public: score() { val=0; } int show() { return(val); } friend void operator++(score &); friend void operator++(score &,int); }; void operator++(score &s) { s.val=s.val+1; }