0% found this document useful (0 votes)
7 views

11ObjectSlicing

Uploaded by

nanigir651
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

11ObjectSlicing

Uploaded by

nanigir651
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Object Slicing

Object slicing occurs when a derived class object is assigned to a base class object. This
"slicing" happens because the base class object can only hold the base class part of the data,
and any additional members or functionalities of the derived class are discarded.
#include <iostream>

#include <string>

using namespace std;

class Base {

public:

string baseData;

Base(string data = "BaseClassData") : baseData(data) {}

virtual void display() const {

cout << "Base class data: " << baseData << endl;

};

class Derived : public Base {

public:

string derivedData;

Derived(string baseData = "BaseClassData", string derivedData = "DerivedClassData")

: Base(baseData), derivedData(derivedData) {}

void display() const override {

cout << "Base class data: " << baseData << endl;

cout << "Derived class data: " << derivedData << endl;

};

int main() {
Derived derivedObj("CustomBaseData", "CustomDerivedData");

Base baseObj = derivedObj; // Object slicing occurs here

// Displaying data from the sliced base object

baseObj.display(); // Calls Base::display, derivedData is lost

// Displaying data from the original derived object

cout << endl << "Original derived object:" << endl;

derivedObj.display();

return 0;

Base class data: CustomBaseData

Original derived object:

Base class data: CustomBaseData

Derived class data: CustomDerivedData

Avoiding Object Slicing in C++


Object slicing can be avoided by using pointers or references instead of copying objects directly. This
ensures that the entire object, including derived class-specific members, is accessible and
polymorphic behavior is preserved.

Example: Using References to Avoid Object Slicing

#include <iostream>

#include <string>

using namespace std;

class Base {
public:

virtual void display() const {

cout << "This is the Base class." << endl;

virtual ~Base() {} // Virtual destructor to allow proper cleanup in polymorphism

};

class Derived : public Base {

public:

void display() const override {

cout << "This is the Derived class." << endl;

};

void printObject(const Base& obj) { // Pass by reference to avoid slicing

obj.display(); // Calls the appropriate version (Base or Derived)

int main() {

Derived derivedObj;

cout << "Calling function with reference:" << endl;

printObject(derivedObj); // Polymorphism works, no slicing

return 0;

}Output:

Calling function with reference: This is the Derived class.

Example: Using Pointers to Avoid Object Slicing


#include <iostream>

using namespace std;

class Base {

public:

virtual void display() const {

cout << "Base class display" << endl;

virtual ~Base() {} // Virtual destructor for proper cleanup

};

class Derived : public Base {

public:

void display() const override {

cout << "Derived class display" << endl;

};

void printObject(const Base* obj) { // Accept a pointer to Base

obj->display(); // Calls the appropriate version

int main() {

Derived derivedObj;

cout << "Calling function with pointer:" << endl;

printObject(&derivedObj); // No slicing, polymorphism works

return 0;

}
Output:

Calling function with pointer: Derived class display

You might also like