object slicing

There is a distinct difference between passing the addresses of objects and passing objects by value in polymorphism.
If casting is done to an object instead to reference or pointer to object then object will be sliced.

class P{
string y;
P(const string& s):y(s){}
virtual string name() const {return y;}
virtual string show()const{return “this is ” y;


class D:public P{
string f;
d(const string& s,const string& d):P(s),f(d){}
string show() const{
return P::name() + “likes to “+f;
void showoff(P p){ //slices the object
cout <<p.show()<<endl;
int main(){
P p(“rajnish”);
D d(“shobjit”,”vishal”);

Leave a Reply