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 are classes oop?

596


can we make game by using c

3415


Why interface is used?

549


What is abstract class in oops?

596


Do you know about multiple inheritance?

637






INSTANCE FIELDS DECLARED private ARE ACCESSIBLE BY THE METHODS ONLY.CAN WE CHANGE THE private FIELD OF AN OBJECT IN A METHOD OF SOME OTHER OBJECT OF THE SAME CLASS?

1630


What is overloading and its types?

612


What are benefits of oop?

631


Give two or more real cenario of virtual function and vertual object

1849


What is encapsulation c#?

600


What do you mean by abstraction?

608


They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?

1392


to find out the minimum of two integer number of two different classes using friend function

1637


design a c++ class for the chess board,provide a c++ class definition for such class(only class definition is required)

6143


What is for loop and its syntax?

595