Sunday, June 18, 2017

Forward declaration:: used in friend function of two class.
E Balagurusamy page 128

* Do not confuse between
void fun1(int & a)
{
.....
......
}
and
void fun1(int * a)
{
....
....
}
Both are pass by reference.

Copy constructor::
Program to illustrate the copy constructor.
#include<iostream>
using namespace std;
class ABC
{
    int a,b;
public:
    //int c;
    void show()
    {
        cout<<"a="<<a<<" and b="<<b<<endl;
    }
    ABC(){a=0;b=0;}
    ABC(int x,int y){a=x;b=y;}
    ABC(ABC& obj){ a=obj.a; }
};
int main()
{
    int a=2,b=3;
    ABC obj1,obj2(2,3);
    ABC obj3(obj2);            //Copy constructor called (copy only a, as defined in the constructor)
    ABC obj4 = obj2;          //Copy constructor called (copy only a, as defined in the constructor)
    obj1=obj2;         //default assignment operator of the system called, copy all elements one by one.
    cout<<"obj1(2,3)";
    obj1.show();
    cout<<"\nobj2(2,3)";
    obj2.show();
    cout<<"\nobj3(2,lets see)";
    obj3.show();
    cout<<"\nobj4(2,lets see)";
    obj4.show();

}

Output::
    obj1(2,3) a=2 and b=3
    nobj2(2,3) a=2 and b=3
    obj3(2,lets see) a=2 and b=2293456
    obj4(2,lets see) a=2 and b=0

Shallow Copy and Deep Copy --http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/

No comments:

Post a Comment