what is object slicing

Answer Posted / kar4you

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. That is, the
base class object can access only the base class members.
This also implies the separation of base class members from
derived class members has happened.

class base
{
public:
int i, j;
};
class derived : public base
{
public:
int k;
};
int main()
{
base b;
derived d;
b=d;
return 0;
}
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 does not get copied. On the effect object d got sliced.

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is inheritance and how many types of inheritance?

622


What is abstraction in oop with example?

646


Write a program to implement OOPS concepts such as inheritance, polymorphism, friend function, operator overloading?

4244


Why is polymorphism needed?

605


What is abstraction encapsulation?

659






What is difference between inheritance and polymorphism?

573


Write a java applet that computes and displays the squares of values between 25 and 1 inclusive and displays them in a TextArea box

2038


Why do we use oops?

593


What is multilevel inheritance explain with example?

628


What is polymorphism in oop example?

517


What is encapsulation process?

584


If a=5, b=6, c=7, b+=a%c*2. What is the final value of b?

944


What is difference between multiple inheritance and multilevel inheritance?

603


What is destructor in oop?

625


What is abstraction oop?

625