what is object slicing?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 abstraction oop?
Given two strings like x=?hello? and y=?open?, remove any character from string x which is also used in string y, thus making the result x=?hll?.
what is the difference between inter class and abstract class...?
When is an object created and what is its lifetime?
How is the using() pattern useful? What is IDisposable? How does it support deterministic finalization?
What is for loop and its syntax?
Why static functions always uses static variables?
What are the important components of cohesion?
Write a c++ program to display pass and fail for three student using static member function
What is Difference Between Inheritance and creating object and getting data? means Class A extends B{ B.getMethod();} (OR) Class A{ b obj=new B(); obj.getMethod(); }
What is operator overloading? Give Example
11 Answers CTS, IBM, TCS,
What is abstraction example?