what is object slicing?

Answer Posted / p govind rao

if an object of a derived class is assigned to a base class
object,the compiler accepts it.but it copies only the base
portion of the object


#include<iostream>
using namespace std;

class base
{
public:
int i,j;
base(){i=2;j=3;}
virtual void show(){cout<<"i = "<<i<<endl<<"j = "<<j<<endl;}
};

class derived :public base
{
public :
int k;
derived(){k=4;}
void show() {
base::show();
cout<<"k = "<<k<<endl;
}
};

int main()
{
base b;
derived d;
d.show();
b=d;

b.show();

return 0;
}

OutPut is
i = 2
j = 3
k = 4
---------------
i = 2
j = 3

here b contains i and j where as d contains i,j&k.On
assignment only i and j of the d get copied into i and j of
b. k doesnot be copied. on the effect object d got sliced.

Is This Answer Correct ?    12 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is debug class?what is trace class? What differences are between them? With examples.

1846


What is polymorphism and types?

827


Get me a number puzzle game-program

1924


What is polymorphism in oop example?

720


What is polymorphism and why is it important?

761


What are the two different types of polymorphism?

879


What is polymorphism what is it for and how is it used?

763


Why do we use polymorphism in oops?

778


What are main features of oop?

865


Hi friends I have experience of 6 months in website design and maintanence. Now i am looking for other IT jobs.. to switch platform. please post any interview you know in chennai.

1961


What is purpose of inheritance?

842


What does I oop mean?

822


What is difference between multiple inheritance and multilevel inheritance?

867


can inline function declare in private part of class?

3962


What is class and object with example?

804