-1

I saw this example: http://www.cplusplus.com/doc/tutorial/typecasting/#dynamic_cast

(...)
class Base { virtual void dummy() {} };
class Derived: public Base { int a; };
(...)
Base * pba = new Derived;
Base * pbb = new Base;
(...)

Why 'pba' is a Base object if it's being initialized with Derived? Why not make it a Derived object?

Derived * pba = new Derived; // use this instead

And is it just a C++ thing?

Lucas Steffen
  • 1,244
  • 2
  • 10
  • 22
  • 2
    That's an instance of *polymorphism*. You cannot expect us to provide a fully elaborate answer as this is a pretty broad subject (and I'm sure there are some duplicates here). – cadaniluk Jan 09 '17 at 12:51
  • And it's an example for dynamic_cast. If you write _Derived * pba = new Derived;_ then this line has no sense _pd = dynamic_cast(pba);_ – rud0x1 Jan 09 '17 at 12:53
  • 1
    If you have both [cats and dogs in a set](https://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29%23Subtyping), and want to make all animals in this set make a sound then you call `Animal::talk()` for each animal, but this call will yield different results, based on the actual class of the animal. – Dialecticus Jan 09 '17 at 12:57
  • 2
    `pba` is not a `Base` object, it's a *pointer* to a `Base` object. It is what it is in order to illustrate the subject of `dynamic_cast`. It seems like you have looked at the code without reading the surrounding text. – molbdnilo Jan 09 '17 at 13:00
  • The other thing is that this is a simplified example. A (slightly) more realistic version would be: `Base*pba = flag ? new Derived: new Derived2;` (where `Derived2` is a different derived class). – Martin Bonner supports Monica Jan 09 '17 at 13:18
  • @molbdnilo, yes I did, but i'm not interested on the dynamic_cast, but in what situation declaring a object with a class and initializing with another would be used, and Dialecticus answered one. The link was just and example – Lucas Steffen Jan 09 '17 at 13:24
  • @Downvoter the question is Why not make pbb a Derived object? – Lucas Steffen Jan 09 '17 at 13:28
  • Because you're referencing an *example*, which demonstrates a feature of the language. Making `pbb` a `Derived` object would defeat the purpose of the example. If you want a real-life example for polymorphism, google it. It's an inherent feature of OOP and is explaines in virtually every OOP tutorial. [Here](http://stackoverflow.com/questions/1031273/what-is-polymorphism-what-is-it-for-and-how-is-it-used) is a question on polymorphism with extensive answers. – cadaniluk Jan 09 '17 at 13:32

1 Answers1

1

neither pba nor pbb is an object but they are pointers of type base class Base so in your code you used he pointers polymorphically which means a base pointer can point to the same class or to its derived class object.

  • the object is created with new not pbb or pba themselves, consider this example:

    #include <string>
    #include <iostream>
    using namespace std;
    
    
    class Base
    {
        public:
            virtual void Print() const { cout << "Base print()" << endl;} // virtual function
            void Greet()const {cout << "in Base Say: hello!" << endl;}
    };
    
    class Derived : public Base
    {
        public:
            void Print() const { cout << "Derived print()" << endl;} // ovrode the base member pritn()
            void Greet()const {cout << "in Derived Say: hello!" << endl;}
    };
    
    int main()
    {
    
        Base* pba = new Derived;
    
        pba->Print(); // Derived print()
        pba->Greet(); // in Base Say: hello! ??? because Greet() is not virtual
    
        Base* pbb = new Base;
    
        pbb->Print(); // Base print()
        pbb->Greet(); // in Base Say: hello!
    
        return 0;
    }
    

    so at runtime the pointer pba and pbb can be assigned an object of Base or Derived classes thus the virtual member function are called accordingly.

Raindrop7
  • 3,889
  • 3
  • 16
  • 27