Object Slicing in C++
when a derived class object is assigned to a base class object, additional attributes of a derived class object are sliced off to form the base class object.
We can avoid above unexpected behavior with the use of pointers or references. Object slicing doesn’t occur when pointers or references to objects are passed as function arguments since a pointer or reference of any type takes same amount of memory. For example, if we change the global method myfunc() in the above program to following, object slicing doesn’t happen.
Sol : Object Slicing:
We can avoid above unexpected behavior with the use of pointers or references. Object slicing doesn’t occur when pointers or references to objects are passed as function arguments since a pointer or reference of any type takes same amount of memory. For example, if we change the global method myfunc() in the above program to following, object slicing doesn’t happen.
Sol : Object Slicing:
In C++, a derived class object can be assigned to a base class object, but the other way is not possible.
| classBase { intx, y; };classDerived : publicBase { intz, w; };intmain() {    Derived d;    Base b = d; // Object Slicing,  z and w of d are sliced off} | 
Object slicing happens when a derived class object is assigned to a base class object, additional attributes of a derived class object are sliced off to form the base class object.
| #include <iostream>usingnamespacestd;classBase{protected:    inti;public:    Base(inta)     { i = a; }    virtualvoiddisplay()    { cout << "I am Base class object, i = "<< i << endl; }};classDerived : publicBase{    intj;public:    Derived(inta, intb) : Base(a) { j = b; }    virtualvoiddisplay()    { cout << "I am Derived class object, i = "           << i << ", j = "<< j << endl;  }};// Global method, Base class object is passed by valuevoidsomefunc (Base obj){    obj.display();}intmain(){    Base b(33);    Derived d(45, 54);    somefunc(b);    somefunc(d);  // Object Slicing, the member j of d is sliced off    return0;} | 
Output:
I am Base class object, i = 33 I am Base class object, i = 45
We can avoid above unexpected behavior with the use of pointers or references. Object slicing doesn’t occur when pointers or references to objects are passed as function arguments since a pointer or reference of any type takes same amount of memory. For example, if we change the global method myfunc() in the above program to following, object slicing doesn’t happen.
| // rest of code is similar to abovevoidsomefunc (Base &obj){    obj.display();}           // rest of code is similar to above | 
Output:
I am Base class object, i = 33 I am Derived class object, i = 45, j = 54
We get the same output if we use pointers and change the program to following.
| // rest of code is similar to abovevoidsomefunc (Base *objp){    objp->display();}intmain(){    Base *bp = newBase(33) ;    Derived *dp = newDerived(45, 54);    somefunc(bp);    somefunc(dp);  // No Object Slicing    return0;} | 
Output:
I am Base class object, i = 33 I am Derived class object, i = 45, j = 54
Object slicing can be prevented by making the base class function pure virtual there by disallowing object creation. It is not possible to create the object of a class which contains a pure virtual method.
