-1

The expected output:

Child = 2
Parent = 1

My code:

class Parent{
    int a;
    public:
        void display(){
            a = 1;
            cout << "Parent = " << a << endl;
        }
};

class Child:public Parent{
    int b;
    public:
        void display(){
            b = 2;
            cout << "Child = " << b << endl;
        }
};

int main()
{
    Parent *p = new Child();
    Child c;

    //write your code here
    /* My solution :
      c.display();
      Parent* a = &c;
      a->display();
    */


}

I made use of static binding to successfully call the display() member function of the parent class

However, I wonder if there is any better solution for this question.

What is the purpose of having this line Parent *p = new Child();? Is it a pointer pointing to a constructor?

What is the difference between that line and Parent *p = new Child;?

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
zZzZ
  • 141
  • 2
  • 3
  • 10
  • This would be the duplicate if you had correctly used virtual methods to implement overriding: https://stackoverflow.com/questions/672373/can-i-call-a-base-classs-virtual-function-if-im-overriding-it. – Max Langhof Oct 09 '19 at 15:53
  • 1
    What do you mean "what is the purpose of having this line"? If you don't know why it is there, why do you have it? Is this a homework question? – Martin Bonner supports Monica Oct 09 '19 at 15:58
  • Yup this is a homework question.The code structure given cannot be modified.I'm only allowed to add my code in that specified area.I'm able to produce the desired output but this line "Parent *p = new Child();" is not useful at all for my solution.So I wonder what is the purpose of having this line... – zZzZ Oct 09 '19 at 16:02

2 Answers2

4

There are several points to make here:

  1. Child is not overriding anything, it is only hiding the base class method. Overriding would require virtual member functions, and you can verify that a method actually overrides a base class function by adding the override keyword. Neither class is polymorphic, because there are no virtual methods anywhere.

  2. Since your classes are not polymorphic, you will only ever have static binding, no dynamic binding. (You correctly identified this happening).

  3. To call a base class function that was overridden or hidden in a derived class, you prefix the call with the base class name and ::, like so:

    class Child : public Parent{
      int b;
      public:
        void display(){
          Parent::display();
        }
    };
    
    // or
    
    int main()
    {
      Child c;
      c.Parent::display();
    }
    
  4. There is no difference between Parent *p = new Child(); and Parent *p = new Child; in your code, both call the default constructor to initialize a new child object. For types like int there is a difference, new int; creates an uninitialized int object, new int(); (or new int{};) initializes it to zero instead.

Max Langhof
  • 23,383
  • 5
  • 39
  • 72
1

How to call the overriden function of parent class in C++?

You haven't actually overridden any function in your example. Only virtual member functions can be overridden, and display is non-virtual.

I made use of static binding to successfully call the display() member function of the parent class

Your solution works because there is no overriding or virtual functions involved. Another way to call the hidden member function is to use the scope resolution operator:

c.Parent::display();

This would use static binding even if the function was virtual.


What is the purpose of having this line Parent *p = new Child();?

It doesn't seem to have any purpose in your program. In general, new is used to allocate objects dynamically from the free store.

Note that the memory will be leaked unless you explicitly deallocate it with delete. Also not that delete p would have undefined behaviour, because it points to a base whose destructor is non-virtual.

Is it a pointer pointing to a constructor?

No. p points to the dynamic object that was created in the free store.

What is the difference between that line and Parent *p = new Child;?

Without parentheses, you use default initialisation syntax. With empty parentheses you use value initialisation.

In the case of trivially constrtuctible classes (such as Parent), default initialisation will default initialise the members. In case of integer members, this leaves the members uninitialised. Value initialisation will instead value initialise the members, which for integers means initialisation to zero.

In the case of non-trivially constructible classes (such as Child), both default and value initialisation invoke the default constructor.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • If the parenthesis doesn't hold any value, means it's the same as the one without parenthesis,is it? – zZzZ Oct 09 '19 at 16:17
  • Someone told me this is an upcasting "Parent *p = new Child" ... – zZzZ Oct 09 '19 at 16:18
  • @XinYi Yes. There is an implicit conversion from derived pointer to base pointer. Such conversion is called an upcast. Similar conversion is done on the line `Parent* a = &c`. – eerorika Oct 09 '19 at 16:20
  • In that case, I can also use p->display(); to get the desired output.Thank you very much!!!! =] – zZzZ Oct 09 '19 at 16:39