ALLInterview.com :: Home Page            
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Google
   
 
Categories >> Software >> Programming Languages >> C++ >> OOPS
 
 


 

 
 STL interview questions  STL Interview Questions (93)
 OOPS interview questions  OOPS Interview Questions (562)
 C++ General interview questions  C++ General Interview Questions (459)
Question
what is object slicing?
 Question Submitted By :: OOPS
I also faced this Question!!     Answer Posted By  
 
Answer
# 1
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 ?    119 Yes 9 No
Rsn
 
Answer
# 2
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 ?    42 Yes 5 No
Debashis Mishra,india,orissa
 
 
 
Answer
# 3
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 ?    68 Yes 48 No
Soumya_830312
 
Answer
# 4
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 ?    10 Yes 1 No
P Govind Rao
 
Answer
# 5
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 7 No
Susanta (rcm)
 
Answer
# 6
"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
Chris Pearson
 
Answer
# 7
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 6 No
Bibek
 
Answer
# 8
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 6 No
Rahul
 
Answer
# 9
dividing ith object
 
Is This Answer Correct ?    1 Yes 12 No
Guest
 

 
 
 
Other OOPS Interview Questions
 
  Question Asked @ Answers
 
Should you protect the global data in threads? Why or why not? IBM 2
what is static?   3
Example for 4 pillar of oops like, Inheritance,Poly,Abstraction,Encabsulation ?   3
What do you mean by stack program? Get me an example stack program?   4
What do you mean by binding of data and functions?   3
what is difference between objects and function   1
different types of castings Siemens 3
difference between static and non-static variables?   2
what is opps?why it is use in programming language? Wipro 2
What normal C constructs work differently in C++?   2
What is the difference between Home and $Home? TCS 2
What is the main difference between C++ and Java TCS 11
 
For more OOPS Interview Questions Click Here 
 
 
 
 
 


   
Copyright Policy  |  Terms of Service  |  Articles  |  Site Map  |  RSS Site Map  |  Contact Us
   
Copyright 2013  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  KalAajKal.com