Object Slicing in C++

Jinku Hu Oct 12, 2023
Object Slicing in C++

This article will introduce object slicing in C++.

Use Object Slicing to Derived Class Object to the Base Class in C++

The core concept of object-oriented programming is inheritance, which implies that classes can be related to each other and form a hierarchy. The root of the hierarchy is generally called a base class from which other classes inherit features in the form of data members or functions. The classes that inherit from other classes are called derived ones. There are different types of inheritance that specify the access control for the base class members and which of them are accessible in the derived class. For example, private members of the base class can’t be accessed directly from the derived class. Instead, a derived class should use public methods to retrieve these members. The base class can designate a separate set of members with a protected identifier accessible from the derived classes directly.

In this case, we focus on derived-to-base conversion rules and the use cases for such features. Usually, derived classes contain nonstatic members defined in the derived class itself and all members that are inherited from the other classes. When a derived object is assigned to a base class object, an assignment operator run is taken from the base class. Thus, this operator knows about only the base class members, and only those members get copied during the assignment operation.

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::string;

class Person {
 protected:
  string name;

 public:
  explicit Person(string s) : name(std::move(s)) {}

  string getName() { return name; };
};

class Athlete : public Person {
  string sport;

 public:
  explicit Athlete(string s) : Person(std::move(s)){};
  Athlete(string s, string sp) : Person(std::move(s)), sport(std::move(sp)){};

  string getSport() { return sport; }
};

class Politician : public Person {
  string party;

 public:
  explicit Politician(string s) : Person(std::move(s)) {}
  Politician(string s, string p) : Person(std::move(s)), party(std::move(p)) {}

  string getParty() { return party; }
};

int main() {
  Politician p1("Lua", "D");
  Athlete a1("Lebron", "LA Lakers");

  cout << p1.getName() << " " << p1.getParty() << endl;

  Person p0 = p1;
  Athlete a2(p0.getName());

  cout << p0.getName() << endl;
  cout << a2.getName() << endl;

  return EXIT_SUCCESS;
}

Output:

Lua D
Lua

The previous example code defines two classes, Politician and Athlete, derived from the Person class. So, we can assign the Politician object to the Person, but not another way around. When the assignment is done, the member function getParty is not accessible in the newly created p0 object. Consequently, the following version of the code results in a compile-time error since the p0 invokes a member of the Politician class.

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::string;

class Person {
 protected:
  string name;

 public:
  explicit Person(string s) : name(std::move(s)) {}

  string getName() { return name; };
};

class Athlete : public Person {
  string sport;

 public:
  explicit Athlete(string s) : Person(std::move(s)){};
  Athlete(string s, string sp) : Person(std::move(s)), sport(std::move(sp)){};

  string getSport() { return sport; }
};

class Politician : public Person {
  string party;

 public:
  explicit Politician(string s) : Person(std::move(s)) {}
  Politician(string s, string p) : Person(std::move(s)), party(std::move(p)) {}

  string getParty() { return party; }
};

int main() {
  Politician p1("Lua", "D");
  Athlete a1("Lebron", "LA Lakers");

  cout << p1.getName() << " " << p1.getParty() << endl;

  Person p0 = p1;
  Politician p2 = p0;
  Politician p2 = a1;

  cout << p0.getName() << " " << p0.getParty() << endl;

  return EXIT_SUCCESS;
}
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ Class