LoginSignup
1
0

More than 5 years have passed since last update.

C++ - What is object slicing?

Last updated at Posted at 2018-07-20

#include <iostream>
#include<string>
using namespace std;

class Base
{
public:
    virtual string print() const
    {
        return "This is Base class";
    }
};

class Derived : public Base
{
public:
    virtual string print() const
    {
        return "This is Derived class";
    }
};

void describe(Base p)  <--- Object Slicing Here.
{
    cout << p.print() << endl;
}

int main()
{
    Base b;
    Derived d;
    describe(b);
    describe(d);
    return 0;
}

Output

This is Base class
This is Base class

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0