by Dinesh Thakur Category: Classes in C++

 

#include <iostream.h>

#include<conio.h>
class Num
{
       friend Num operator+(Num ob, int i);
       friend Num operator+(int i, Num ob);
       int count;
       public:
                     Num(int cc=0)
              {
                     count=cc;
               }
                     Num& operator=(int i);
                     void Show()
              {
                     cout<<count<<endl;
               }
 };
                     Num& Num::operator=(int i)
              {
                     count=i;
                     return *this;
               }
                     Num operator+(Num ob, int i) // This handles ob+int.
              {
                     Num temp;
                     temp.count=ob.count+i;
                     return temp;
               }
                     Num operator+(int i, Num ob) // This handles int+ob ..
               {
                     Num temp;
                     temp.count=ob.count+i;
                     return temp;
               }
                     void main()
               {
                     clrscr();
                     Num obj;
                     obj=10;
                     obj.Show();
                     obj=10+obj;
                     obj.Show();
                     obj=obj+ 12;
                     obj.Show();
                     getch();
               }
Output:
10
20
32
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content







Popular Article