Monday, February 20, 2012

What is Object Slicing in C++ ?

Object slicing: when a derived class object is assigned to a base class object. only base class data will be copied from derived class and derived data will be sliced or ignored. This will occur in C++ , if you try to use call by value. see the sample code in c++.

Object slice prevention:
  • Object slicing can be prevented by making the base class pure virtual, so that base instance will not be allowed.
  • object slicing can't be occurred, if pointers or reference to the objects used, as both pointers of the same size

class base{

public:
int a;
base(){
a = 10;
}
};
class derive: public base
{
public:
int b;
derive()
{
b=20;
}
};

int main()
{
base b;
derive d;
cout< < "size of base class is "< < sizeof(b)< < endl;
cout< < "size of derive class is  "< < sizeof(d)< < endl;
b=d;
cout< < "size of b class is "< < sizeof(b)< < endl;
}


Output:
size of base class is 4
size of derive class is 8
size of b class is 4


From the above sample code, contents of the base class are one int data type and where as for derived class are two int data types. so size of the derived class is more than the base class. and in the example, derived instance is assigned to base instance, so base instance only takes the base contents and it will slice or ignore the extra int data type in derived class.
From the output, the size of the base object is four and size of the derived object is eight. we can observe that even after assigning the derived object to the base object, the size of the base object is four only. it sliced the extra four bytes from the derived object.

9 comments:

Anonymous said...

Output is not changed when i assigned the derived class object to base class object....

Anonymous said...

hey u can easily see that the size of base class is not changed after the assignment although derived class object is assigned to it... that means only the base class part is copied to base class object and derived class object is ignored.

Goverment of india Parliamentry OR Presidential said...

Where is display()
function.

chanduthedev p said...

display() function not required, for testing I added in my local system, just copy paste error ;-)

Anonymous said...

Find similar answers here:http://sickprogrammersarea.blogspot.in/2014/03/technical-interview-questions-on-c_6.html

Slicing means that the data added by a subclass are discarded when an object of the subclass is passed or returned by value or from a function expecting a base class object.

Explanation: Consider the following class declaration:

class baseclass
{
...
baseclass & operator =(const baseclass&);
baseclass(const baseclass&);
}
void function( )
{
baseclass obj1=m;
obj1=m;
}
As baseclass copy functions don't know anything about the derived only the base part of the derived is copied. This is commonly referred to as slicing.

Santhi P said...

Object slicing has been explain well here. Thanks for the information! You can find more on object slicing with examples here below.

Object slicing in Cpp with examples

sandeep saxena said...

I got a Lot of knowledge from you. I need more to develop my skills. Thanks for this.
javascript training in chennai
javascript class
js class
javascript course
core java training in chennai
C C++ Training in Chennai
Appium Training in Chennai
JMeter Training in Chennai

priya rajesh said...

Such an amazing blog with new updates. I love to learn more about this topic. Waiting for more like this.
DevOps Training Institute in Chennai
DevOps Training in Chennai
Best AWS Training in Chennai
AWS Training in Velachery
AWS Training center in Chennai
Cloud Training in Chennai
Cloud computing certification in Chennai
DevOps Training in Anna Nagar

Anbarasan14 said...

I don’t have any idea about commenting on someone’s blog but it’s the first time I really want to appreciate the hard work of this blog admin for sharing this post with us.
Spoken English Classes in Chennai
Best Spoken English Classes in Chennai
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
English Speaking Classes in Mumbai
English Speaking Course in Mumbai
IELTS Classes in Mumbai
IELTS Coaching in Mumbai
Spoken English Class in Anna Nagar
IELTS Coaching in T Nagar

Popular Posts