In this tutorial we shall see what is object slicing in C++
Few points related to object slicing:
- Object Slicing occurs when a derived class object is assigned to the base class object.
- Then, an instance of derived class object is copied into the base class object, omitting the additional attributes of derived class object.
- It means the data members of the derived class will not be present
- This is called as Object Slicing in C++
Example:
#include<iostream> //for more C++ tutorial visit www.ProDeveloperTutorial.com using namespace std; class Base { int x; int y; }; class Derived : public Base { int a; int b; }; int main() { Derived d; Base b = d; // Object Slicing, a and b of d are sliced off }
How to solve object slicing problem?
To solve the object slicing problem, use pointers or references.
Object Slicing will not occur if you pass pointers or reference to a function.
To know the difference, consider below 2 examples:
Example 1;
#include<iostream> //for more C++ tutorial visit www.ProDeveloperTutorial.com using namespace std; #include <iostream> using namespace std; class Base { public: int i; Base(int a) { i = a; } virtual void display() { cout << "In Base class display(), i = " << i << endl; } }; class Derived : public Base { public: int j; Derived(int a, int b) : Base(a) { j = b; } void display() { cout << "In Derived class display(), i = "<< i << ", j = " << j << endl; } }; //a function to display the values , using pass by value void somefunc (Base obj) { obj.display(); } int main() { Base b(10); Derived d(20, 30); somefunc(b); somefunc(d); // Object Slicing, the member j of d is sliced off return 0; }
Output:
In Base class display(), i = 10 In Base class display(), i = 20
As you can see, each time only the base class display() is getting called, and the member “j” is sliced off.
Example 2:
Now we shall pass as reference as arguments to the “somefunc()” function.
#include<iostream> //for more C++ tutorial visit www.ProDeveloperTutorial.com using namespace std; #include <iostream> using namespace std; class Base { public: int i; Base(int a) { i = a; } virtual void display() { cout << "In Base class display(), i = " << i << endl; } }; class Derived : public Base { public: int j; Derived(int a, int b) : Base(a) { j = b; } void display() { cout << "In Derived class display(), i = "<< i << ", j = " << j << endl; } }; //a function to display the values pass by reference void somefunc (Base& obj) { obj.display(); } int main() { Base b(10); Derived d(20, 30); somefunc(b); somefunc(d); // Object Slicing, the member j of d is sliced off return 0; }
Output:
In Base class display(), i = 10 In Derived class display(), i = 20, j = 30
We can also achieve this by using pointers as shown below:
#include<iostream> //for more C++ tutorial visit www.ProDeveloperTutorial.com using namespace std; #include <iostream> using namespace std; class Base { public: int i; Base(int a) { i = a; } virtual void display() { cout << "In Base class display(), i = " << i << endl; } }; class Derived : public Base { public: int j; Derived(int a, int b) : Base(a) { j = b; } void display() { cout << "In Derived class display(), i = "<< i << ", j = " << j << endl; } }; //a functon to display the values void somefunc (Base *objp) { objp->display(); } int main() { Base *bp = new Base(10) ; Derived *dp = new Derived(20, 30); somefunc(bp); somefunc(dp); // No Object Slicing return 0; }
Output:
In Base class display(), i = 10 In Derived class display(), i = 20, j = 30