Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following C++ program? class Base { public: Base() {} Base(int base

ID: 3630816 • Letter: C

Question

Consider the following C++ program?
class Base
{
public:
Base() {}
Base(int base) {baseData = base;}
private:
int baseData;
};

class Derived: public Base
{
public:
Derived(int base, int derived): Base(base) {derivedData = derived;}
private:
int derivedData;
};

int main()
{
Base b1, *b2 = new Base();
Derived d1(1, 1), *d2 = new Derived(1, 1);
b1 = d1;
b2 = d2;
return 0;
}
This program contains an example of object slicing. On what line does it occur? Why must it happen? Explain why this never happens in Java. Do some investigating and determine how C# avoids this problem.

Explanation / Answer

What is object slicing?

The line b1=d1 it contains object slicing.

Object slicing is a natural consequence of inheritance and substitutability, it is not limited to C++, and it was not introduced deliberately. Methods accepting a Base only see the variables present in Base. So do copy constructors and assignment operators. However they propagate the problem by making copies of this sliced object that may or may not be valid.

This most often arises when you treat polymorphic objects as value types, involving the copy constructor or the assignment operator in the process, which are often compiler generated. Always use references or pointers (or pointer wrappers) when you work with polymorphic objects, never mix value semantics in the game. If you want copies of polymorphic objects, use a dynamic clone method instead.

Java doesn't do object slicing. If the code you provide were turned into Java, then no kind of object slicing would happen. Java variables are references, not objects, so the postcondition of a = b is just that the variable "a" refers to the same object as the variable "b" - changes via one reference can be seen via the other reference, and so on. They just refer to it by a different type, which is part of polymorphism. A typical analogy for this is that I might think of a person as "my brother"[**], and someone else might think of the same person as "my vicar". Same object, different interface.

You can get the Java-like effect in C++ using pointers or references.

[**] In point of fact, my brother is not a vicar.

Slicing was a major pain point in C++ code, surfacing in parameter passing, exception catch blocks (catch a base type instead of the most-derived type) and other places. So most C++ programmers in the back of their head have a slicing filter running. However, things have suddenly become easy with C#. There is no call by-value for reference types and hence the whole story of slicing is suddenly not there.

So there is no point of slicing in c#.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote