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 |
should we use linear search or binary search if elements are placed in random order or mixed?in both cases? i need a little bit detail ans?thnks
What are the valid types of data that the main () can return in C/C++ language
1.what are two type of classe members called? 2.what is data hiding and data encapsulation? 3.how do you make a class member visible aouside its class? 4.what is the default visibility of a class data member? 5.what are the advantages of oop over the structured programing?
What is cohesion in oop?
what is the difference between <stdio.h>and "stdio.h"?
Where You Can Use Interface in your Project
Define a class to represent a bank account. Include the following members: Data Members: Name of the Depositor Account Number Type of Account Balance amount in the account Member Functions: To assign the initial values. To deposit an account. To withdraw an amount after checking the balance. Write a C++ main program to display account number, name and balance.
is java purely oop Language?
49 Answers HCL, Infosys, TCS,
Which keyword is written to use a variable declared in one class in the other class?
What is constructor overloading in oop?
Why multiple inheritance is not possible?
what is data abstraction with example.