Tuesday, June 5, 2012

What do you mean by Object Slicing?


When a derived class object is assigned to a base class, only the base class's part of content in the derived object are copied to the base class, leaving behind the derived class specific contents. This is referred as Object Slicing.

Class BaseClass
{
public int i;
};

class DerivedClass : public BaseClass
{
public int j;
};

int main()
{
BaseClass ObjectOfB;
DerivedClass ObjectOfD;
ObjectOfB = ObjectOfD;
//Here ObjectOfD contains both i and j.
//But only i is copied to ObjectOfB.
}

No comments:

Post a Comment

Live

Your Ad Here