www.openskytechnology.comDescription: Description: Description: D:\website\syllabus\study_matreials\c++\logo_pic\logo.jpg

C++ Home

Return Object

  _______________________________________________________________________

 The program below shows the different implementations how the object is returned from the function by value and by reference.

 

#include<iostream>

using namespace std;

class Sample

{

    int data;

public:

    Sample():data(0){cout<<"default constructor"<<endl;}

    Sample(int d):data(d){cout<<"one agr constr.."<<endl;}

    void Display()

    {

        cout<<"data= "<<data<<endl;

    }

    Sample ReturnObjectVal()

    {

        return *this;

    }

    Sample& ReturnObjectRef()

    {

        return *this;

    }

    Sample(Sample& s)

    {

        this->data = s.data;

        cout<<"in the copy constructor.."<<endl;

    }

    void operator ++()

    {

        this->data++;

    }

    void operator ++( int)

    {

        cout<<"post increment"<<endl;

        this->data++;

    }

    ~Sample(){cout<<"in the destructor"<<endl; }

  /*  Sample& operator = (Sample& s)

    {

        this->data = s.data;

        cout<<"inside the overloaded assignment operator function.."<<endl;

        return *this;

    }*/

};

int main()

{

    Sample s, s1(100),s2(200);

    s1 = s.ReturnObjectVal();

    s1.ReturnObjectRef() = s2;

    s2++;

    s.Display();

    s1.Display();

    s2.Display();

   /* s1 = s2=s;

    s++;

    s.Display();

    s1.Display();*/

}

 

_______________________________________________________________________

 

 

Copyright © Open Sky Technology

 

Corporate Office:  #4, RR Complex, 2nd Floor, Munnekolala, Marathahalli, Bangalore – 560037. (Landmark: near Munnekolala Bus Stop)   Mobile: 9886333765,