Base Constructor Call in Derived Class

joachim

I have got the following problem in a homework for university, the task is as follows:

Derive a class MyThickHorizontalLine from MyLine. One requirement is that the constructor of the derived class MyThickHorizontalLine does not set the values itself, instead its obligated to call the base constructor.

Which currently looks like this in my cpp file:

MyThickHorizontalLine::MyThickHorizontalLine(int a, int b, int c)
{
    MyLine(a, b, c, b);
}

This is my Base constructor:

MyLine::MyLine(int x1, int y1, int x2, int y2)
{
    set(x1, y1, x2, y2);
}

Header Definition of MyLine:

public:
    MyLine(int = 0, int = 0, int = 0, int = 0);

Current problem is that when I debug this I step into the constructor of MyThickHorizontalLine my values for a b c are for example 1 2 3 they are set there and when I then step further and it gets into the Base constructor all my values are Zero.

I am probably missing a crucial part about inheritance here, but I can't get my mind on it.

πάντα ῥεῖ
MyThickHorizontalLine::MyThickHorizontalLine(int a, int b, int c)
{
     MyLine(a, b, c, b); // <<<< That's wrong
}

You cannot initialize your base class inside the constructor's body. Simply use the member initializer list to call the base class constructor:

MyThickHorizontalLine::MyThickHorizontalLine(int a, int b, int c) : MyLine(a, b, c, b) {}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Constructor in base and derived class

From Dev

How to call constructor of a template base class in a template derived class?

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

Visibility of a base class constructor in a derived template class

From Dev

Inadvertent base class constructor "hiding" in derived class

From Dev

Use derived class property in base class constructor

From Dev

How do I call a derived class method from the base class constructor in C++?

From Dev

Calling overloaded base constructor from derived class

From Dev

Setting base members in derived class constructor

From Dev

when using "new" to allocate memory to a Derived class, how to call the Base constructor?

From Dev

Derived class does not call base class method

From Dev

Calling base class constructor from derived class constructor

From Dev

Calling base class constructor from derived class constructor

From Dev

Call base() from derived class to execute base class function?

From Dev

Calling a base class constructor in derived class after some code block in derived constructor

From Dev

How to pass a derived class property to the constructor of a base class

From Dev

Can't enforce the use of base constructor of an abstract class into derived class

From Dev

Calling a member function of a derived class from the base class constructor

From Dev

Can a derived class access constructor and destructor of base class?

From Dev

constructor of derived class cannot be constexpr if base class contains array member

From Dev

Inheritance Copying Base class Constructor to Derived class Object

From Dev

constructor of base class is called while declaring a derived class

From Dev

Calling constructor of a template base class from a template derived class

From Dev

Getting to know which derived class got instantiated in the base class constructor

From Dev

How to use derived class members in a base class constructor

From Dev

Throwing exception in derived class constructor. Why is base class destructor called but not derived class destructor?

From Dev

Throwing exception in derived class constructor. Why is base class destructor called but not derived class destructor?

From Dev

Unsure about parameters when calling base constructor from derived class

Related Related

  1. 1

    Constructor in base and derived class

  2. 2

    How to call constructor of a template base class in a template derived class?

  3. 3

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  4. 4

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  5. 5

    Visibility of a base class constructor in a derived template class

  6. 6

    Inadvertent base class constructor "hiding" in derived class

  7. 7

    Use derived class property in base class constructor

  8. 8

    How do I call a derived class method from the base class constructor in C++?

  9. 9

    Calling overloaded base constructor from derived class

  10. 10

    Setting base members in derived class constructor

  11. 11

    when using "new" to allocate memory to a Derived class, how to call the Base constructor?

  12. 12

    Derived class does not call base class method

  13. 13

    Calling base class constructor from derived class constructor

  14. 14

    Calling base class constructor from derived class constructor

  15. 15

    Call base() from derived class to execute base class function?

  16. 16

    Calling a base class constructor in derived class after some code block in derived constructor

  17. 17

    How to pass a derived class property to the constructor of a base class

  18. 18

    Can't enforce the use of base constructor of an abstract class into derived class

  19. 19

    Calling a member function of a derived class from the base class constructor

  20. 20

    Can a derived class access constructor and destructor of base class?

  21. 21

    constructor of derived class cannot be constexpr if base class contains array member

  22. 22

    Inheritance Copying Base class Constructor to Derived class Object

  23. 23

    constructor of base class is called while declaring a derived class

  24. 24

    Calling constructor of a template base class from a template derived class

  25. 25

    Getting to know which derived class got instantiated in the base class constructor

  26. 26

    How to use derived class members in a base class constructor

  27. 27

    Throwing exception in derived class constructor. Why is base class destructor called but not derived class destructor?

  28. 28

    Throwing exception in derived class constructor. Why is base class destructor called but not derived class destructor?

  29. 29

    Unsure about parameters when calling base constructor from derived class

HotTag

Archive