Friday, October 7, 2016

Object Slicing :)

Tags

Object Slicing:

When a derived class object is assigned to base class then the base class contents in derived object  are copied to base class leaving behind the derived class specific contents.

Following Program Illustrates This Concept:
#include<iostream>
using namespace std;
class base
{
public:
int i=10,j=70;
void show()
{   cout<<"\nIn Base Class";
cout<<"\nI ="<<i;
cout<<"\nJ ="<<j;
}
};
class derived:public base
{
public:
int k=150;
void show()
{  i=50;
  j=75;
cout<<"\nIn Derived Class";
cout<<"\nI ="<<i;
cout<<"\nJ ="<<j;
cout<<"\nK ="<<k;
}
};
main()
{
base b;
derived d;
b.show();
d.show();
b=d;
b.show();
d.show();
}

Output: