what is object slicing?

Answers were Sorted based on User's Feedback



what is object slicing? ..

Answer / rsn

When a Derived Class object is assigned to Base class, the
base class' contents in the derived object are copied to
the base class leaving behind the derived class specific
contents. This is referred as Object Slicing.

Class Base
{
public:
int i;
};

class Derived : public Base
{
public:
int j;
};

int main()
{
Base Bobj;
Derived Dobj;
Bobj = Dobj; //Here Dobj contains both i and j.
//But only i is copied to Bobj.
}

Is This Answer Correct ?    126 Yes 10 No

what is object slicing? ..

Answer / debashis mishra,india,orissa

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
class base
{
public:
int i,j;
};
class derived : public base
{
private:
int k;
};
int main()
{
base b;
derived d;
b=d;
return o;
}
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 ?    46 Yes 6 No

what is object slicing? ..

Answer / soumya_830312

When an base class is assinged to its derived class the
base class takes up only the base member data leaving the
data members of derived class.this is called...

Is This Answer Correct ?    71 Yes 49 No

what is object slicing? ..

Answer / 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

what is object slicing? ..

Answer / chris pearson

"Object slicing" is an informal term that is often used (as
in all the answers above) to indicate that (in some
languages, usually C++) assignment to a superclass instance
from a subclass instance does not copy the member variables
defined only in the subclass.

This usage of the term is inapt and misleading, for several
reasons:

1. The notion that an object has been damaged ("sliced")
suggests something to be avoided. However, it is not
possible (or meaningful) in any statically typed language
for a superclass instance to contain subclass member
variables, so in fact assignment to a superclass operates
correctly and is a useful language feature.

2. After such an assignment, the source, subclass, object
remains unchanged, so it has in no sense been been "sliced".

3. After such an assignment, all member variables of the
destination, superclass, object are present and have been
assigned values, so neither has it in any sense
been "sliced".

The case to which the term "object slicing" is better
applied is more subtle and problematic. It occurs in a
statically typed language such as C++ where assignment
appears to be to a superclass instance but is actually to a
subclass instance.

For example:

void myassign(mysuperclass &dest)
{
mysuperclass source;
dest = source;
}

mysubclass sub;
myassign(sub); // assigns only superclass members of
sub!

A better term for this case might be "partial assignment".

Such partial assignment is not possible in Java because
that language only allows object references, and assignment
to a reference simply causes it to refer to a different
object.

-- Chris

Is This Answer Correct ?    9 Yes 2 No

what is object slicing? ..

Answer / susanta (rcm)

object slicing is the phenomenon in which when we assigned
derived class with base class ,the separation of base class
data members from derived class data members occur.

Is This Answer Correct ?    14 Yes 8 No

what is object slicing? ..

Answer / bibek

object slicing is the technique where we when we assign the
value of derived class to the base class,only the base class
content is sliced not the derived class own datamember.

Is This Answer Correct ?    10 Yes 7 No

what is object slicing? ..

Answer / rahul

execute this program and you will get your answer

class base
{
public int i,j;
}
class drived extends base
{
public int k;
}
class main
{
public static void main(String[] s)
{
base b = new base();
drived d = new drived();
b.i=10;
b.j=20;
System.out.println("this is class base i=10,j=20");
System.out.println("i =" +b.i);
System.out.println("j =" +b.j);
System.out.println("this is class drived i=100,j=200,k=300");
d.i=100;
d.j=200;
d.k=300;
System.out.println("i =" +d.i);
System.out.println("j =" +d.j);
System.out.println("k =" +d.k);
b=d;

System.out.println("after the assignment");
System.out.println("i =" +b.i);
System.out.println("j =" +b.j);
//System.out.println("k =" +b.k);
System.out.println("even though i am calling i and j using b object it is showing d values and you cannot call k this is called object sclicing object d got scliced now you cannot access k after the assignment of d into b");
}
}

Is This Answer Correct ?    7 Yes 7 No

what is object slicing? ..

Answer / guest

dividing ith object

Is This Answer Correct ?    1 Yes 13 No

Post New Answer

More OOPS Interview Questions

what is inline function?

3 Answers  


Write on signed and unsigned integers and give three (3) examples each

1 Answers  


which is platform independent device used in computers

2 Answers  


what is new modifier in C#

8 Answers   HCL,


how many types of notations are in java

1 Answers   National University of Modern Languages (NUML),






What is the difference between and interface and an abstract class ?

4 Answers   IBM, Infosys, Ness Technologies,


Hi All, I am new to programming and want to know how can i write a code to take input of 2 numbers from user and swap it without using a temp variable?

2 Answers   NIIT,


why function overloading is not called as pure polymorphism?

2 Answers  


What is the difference between a constructor and a destructor?

0 Answers  


what is graphics

0 Answers  


What is difference between class and object with example?

0 Answers  


what is multithreading in c++ , what is difference between multithreading and singlethreading.

4 Answers  


Categories