C++ Programming Blog

 
 
 

Image

 #include<iostream.h>
 #include<conio.h>


 //------------------------------         --------------------------------//


 class number
    {
       private:
        int data_1;
        int data_2;

       public:
        number()
           {
              data_1=0;
              data_2=0;
           }

        number(int num_1,int num_2)
           {
              data_1=num_1;
              data_2=num_2;
           }

        friend ostream &operator<<(ostream &s,number &obj);
        friend istream &operator>>(istream &s,number &obj);
    };



 //-----------------  &operator<<(ostream &,number &)  -------------------//


 ostream &operator<<(ostream &s,number &obj)
    {
       s<<\"(\"<<obj.data_1<<\",\"<<obj.data_2<<\")\";
       return s;
    }


 //-----------------  &operator>>(istream &,number &)  -------------------//


 istream &operator>>(istream &s,number &obj)
    {
       s>>obj.data_1>>obj.data_2;
       return s;
    }



 //-----------------------------  Main( )  -------------------------------//



 main()
    {
       clrscr();

       number obj_1(5,6);
       number obj_2(7,8);
       number obj_3;

       cout<<\"\\n Value of obj_1 = \"<<obj_1<<endl;
       cout<<\"\\n Value of obj_2 = \"<<obj_2<<endl;

       cout<<\"\\n Enter the value of obj_3 : \"<<endl;
       cin>>obj_3;
       cout<<\"\\n Value of obj_3 = \"<<obj_3;

       getch();
       return 0;
    }

 
 
Didn't find what you were looking for? Find more on Program to illusrate the use of stream insertion and extraction operators