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{

int a;
a = 10;
class derive: public base
int b;

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

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.


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()

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:

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;
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

Chris Hemsworth said...

I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective. Techno-based information has been fetched in each of your topics. Sure it will enhance and fill the queries of the public needs. Feeling so glad about your article. Thanks…!
selenium training in chennai
selenium online courses best selenium online training
selenium testing training
selenium classes

Monisha said...

I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
Web Designing Training Institute in Chennai | web design training class in chennai | web designing course in chennai with placement | Web Designing and Development Course in Chennai | Web Designer Training Course in Chennai
Mobile Application Development Courses in chennai
Data Science Training in Chennai | Data Science courses in Chennai
Professional packers and movers in chennai | PDY Packers | Household Goods Shifting
Web Designing Training Institute in Chennai | Web Designing courses in Chennai
Google ads services | Google Ads Management agency
Web Designing Course in Chennai | Web Designing Training in Chennai

priya said...

I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective.
php online training in chennai
php programming center in chennai
php class in chennnai
php certification course
php developer training institution chennai
php training in chennnai
php mysql course in chennai
php institute in chennnai
php course in chennnai
php training with placement in chennnai
php developer course

Anonymous said...

Thanks for the interesting blog that you have implemented here. Very helpful and innovative. Waiting for your next upcoming article.
Java training in chennai
Java training institute in chennai
Java course in chennai
Java training classes
Java training
Java programming classes
core java coure

w3webschool said...

I feel satisfied to read your blog, you have been delivering a useful & unique information to our vision.keep blogging.
Digital Marketing Course In Kolkata
Web Design Course In Kolkata

subha said...

keep up the good work. this is an Assam post. this to helpful, i have reading here all post. i am impressed. thank you. this is our digital marketing training center. This is an online certificate course keep it up
Ai & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai

divya said...

Well! If you’re not in a position to customize employee payroll in QuickBooks Payroll Tech Support Number which makes the list optimally, in QB and QB desktop, then read the description ahead. Here, you will get the determination of numerous type of information that which you’ve close at hand for assisting the setup process with comfort. keep it up guys.
Ai & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai

Keerthi SK said...

I was following your blog regularly and this one is very interesting and knowledge attaining. Great effort ahead. you can also reach us for
web design company in chennai
web development company in chennai
website designers in chennai
website designing company in chennai
web design in chennai
website development company in chennai

madhukits said...

sap bw on hana online training
sap sd online training
osb online training
oracle scm online training
abinitio online training
spark online training

Keerthi55 said...

python online training
python training

Unknown said...

Fantastic article post.Really thank you! Awesome.
java training
java online training

Vidhyamenon said...

Useful blog, keep sharing with us.

Why You Should Choose Appium Server
Why do we need Appium

Offshore software development company said...

Good Job !!! thanks for sharing.

Popular Posts