Saturday, July 2, 2016

Object Slicing in C++ with example

What is Object Slicing? 
When an instance of derived class  is copied to base class object by value , part of members associated with derived object will be lost(sliced) . Here base class object discards the data related to the derived class because base class object don't have information about the derived members

When we copy derived obj by value to base obj,  the base class copy constructor /assignment operator will call (copy constructor will call at assigning object at the time of declaration (or) pass by value argument to function and assignment operator will call when assigning object after declarion), so base class don't have information about the derived members to copy.  This means the base copy/assignment will copy members related to base class only.

The following sample c++ code will explain the above description.


#include<iostream>
using namespace std;
class Base
{
public :
    Base(){
        cout<<"Base default constructor\n "<<endl;
    }
    Base(const Base &obj)
    {
        cout<<"Base copy constructor\n "<<endl;
    }
    Base& operator=(const Base &ob)
    {
        cout<<"Base assignment op overload\n"<<endl;
        return *this;
    }
    virtual void display() {
        cout<<"Base disp "<<"\n\n";
    }
};

class Derived:public Base
{
public :
    Derived():Base()
    {
        cout<<"Derived default constructor\n"<<"\n";
    }
    Derived(const Derived &obj):Base(obj)
    {
        cout<<"Derived copy constructor\n "<<endl;
    }
    Derived& operator=(const Derived &ob)
    {
        cout<<"Derived assignment op overload\n"<<endl;
    }
    virtual void display() {
        cout<<"Derived disp "<<"\n\n";
    }
};

void copyByVal(Base obj)
{
    cout<<"Copy by Val fun:  ";
    obj.display();
}
void copyByRef(Base &obj)
{
    cout<<"Copy by Ref fun:  ";
    obj.display();
}

int main() {

    //Version1: to call copy constructor
    Derived d1;        //Base default constructor
                               // Derived default constructor

    Base b = d1;       //Base copy constructor
    b.display();         //Base disp
    Base &b1 = d1;
    b1.display();       //Derived disp

     //Version1: to call assignment operator
    Derived d2;        //Base default constructor
                              //Derived default constructor

    Base b2;            //Base default constructor
    copyByVal(d2);  //Base copy constructor
                              //Copy by Val fun:  Base disp
    copyByRef(d2); //Copy by Ref fun:  Derived disp
    return 0;
}

OUTPUT:
Base default constructor

Derived default constructor

Base copy constructor

Base disp

Derived disp

Base default constructor

Derived default constructor

Base default constructor

Base copy constructor

Copy by Val fun:  Base disp

Copy by Ref fun:  Derived disp


>And the following program will explain you how object slicing occurs when we copy derived class object by value to base class object
#include<iostream>
using namespace std;
class Base
{
public :
    Base(int a){
        baseVal=a;
        cout<<"Base "<<baseVal<<"\n";
    }

    virtual void display() {
        cout<<"Base disp "<<baseVal<<"\n";
    }

protected:
    int baseVal;
};

class Derived:public Base
{
public :
    Derived(int a,int b):Base(a),derVal(b)
    {
        cout<<"Derived "<<a<<" "<<b<<"\n";
    }

    virtual void display() {
        cout<<"Derived disp base & der vals "<<baseVal<<" "<<derVal<<"\n";
    }
private:
    int derVal;

};

void copyByVal(Base obj)
{
    cout<<"Copy by Val fun:  ";
    obj.display();
}

void copyByRef(Base &obj)
{
    cout<<"Copy by Ref fun:  ";
    obj.display();
}

int main() {
    Base b(1);
    Derived d(4,5);

    copyByVal(d);

    copyByRef(d);

    Base b2(2);
    Derived d2(5,6);
    b2 = d2;
    b2.display();


    return 0;
}
OUTPUT:
Base 1
Base 4
Derived 4 5
Copy by Val fun:  Base disp 4
Copy by Ref fun:  Derived disp base & der vals 4 5
Base 2
Base 5
Derived 5 6
Base disp 5


In the above example
b2 = d2 and copyByVal(d);  statements copied only part of the base members (sliced away derived members) to derived object from derived object.


1 comment:

  1. Harrah's casino site & app review - Lucky Club
    Harrah's is a fun and secure gaming platform that's a little bit too complicated to keep things interesting. It's a small luckyclub.live casino that's operated

    ReplyDelete