Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing derived class to a function of base class argument

I have this code:

#include <iostream>

class Base {
  public:
  virtual void sayHello() {
    std::cout << "Hello world, I am Base" << std::endl;
  }
};

class Derived: public Base {
  public:
  void sayHello() {
    std::cout << "Hello world, I am Derived" << std::endl;
  }
};

void testPointer(Base *obj) {
  obj->sayHello();
}

void testReference(Base &obj) {
  obj.sayHello();
}

void testObject(Base obj) {
  obj.sayHello();
}

int main() {
  {
    std::cout << "Testing with pointer argument: ";
    Derived *derived = new Derived;
    testPointer(derived);
  }
  {
    std::cout << "Testing with reference argument: ";
    Derived derived;
    testReference(derived);
  }
  {
    std::cout << "Testing with object argument: ";
    Derived derived;
    testObject(derived);
  }
}

The output is:

Testing with pointer argument: Hello world, I am Derived
Testing with reference argument: Hello world, I am Derived
Testing with object argument: Hello world, I am Base

My question is why both the pointer case void testPointer(Base *obj) and the reference case void testReference(Base &obj) return the result of derived instance of void sayHello() but not and the pass by copy case? What should I do to make the copy case to return the result of the derived class function void sayHello()?

like image 769
Pekov Avatar asked Dec 19 '17 05:12

Pekov


People also ask

Can be inherited by a derived class from a base class?

The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive.

How do you access the base class of a derived class function?

Access Overridden Function in C++ To access the overridden function of the base class, we use the scope resolution operator :: . We can also access the overridden function by using a pointer of the base class to point to an object of the derived class and then calling the function from that pointer.

Can a derived class object be assigned to a base class object?

No, it is not possible. Consider a scenario where an ACBus is a derived class of base class Bus.

Can we pass parameters to base class constructor through derived?

To pass arguments to a constructor in a base class, use an expanded form of the derived class' constructor declaration, which passes arguments along to one or more base class constructors. Here, base1 through baseN are the names of the base classes inherited by the derived class.


1 Answers

A function taking a reference or pointer refers to the original object passed in, while by-value arguments will create a copy of your object. Since you are only copying the base part (since it takes a base object), you end up working with a copy of just the base part, and it acts like a base because it is a base.

This "base-only" copying is called "slicing" because it only copies part of your object, "slicing off" the derived part.

like image 133
Chris Uzdavinis Avatar answered Oct 04 '22 07:10

Chris Uzdavinis