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.
}

What is difference between overloading and overriding?



Having same name methods with different parameters is called overloading, while having same name and parameter functions in base and drive class called overriding.

Live

Your Ad Here