Derived Class Constructor from an Abstract Class

Kevin Del Castillo Ramirez

I have a problem with Derived Class from an Abstract Class Example:

#include <iostream>

using namespace std;

class A
{
protected:
  int a,b;
public:
  A(): a(0), b(0) {}
  A(int na,int nb): a(na), b(nb) {}
  virtual void print() = 0; // Reason to be an abstract class
};

class B : public A
{
public:
  B(int a, int b)  {A(a,b);}
  void print()
  {
    cout << a << endl;
    cout << b << endl;
  }
};

int main(){

  B clase(3,2);
  clase.print();
  return 0;
}

The above code give me an error like this:

lab1.cpp: In constructor ‘B::B(int, int)’:
lab1.cpp:20:10: error: invalid cast to abstract class type ‘A’
     A(a,b);
          ^
lab1.cpp:5:7: note:   because the following virtual functions are pure within ‘A’:
 class A
       ^
lab1.cpp:12:16: note:   virtual void A::print()
   virtual void print() = 0;
                ^

Note: I know how to make it works, the B constructor will look like this:

  B(int a, int b): A(a,b) {}

But my real question is, what if I want to call the constructor of my derived class within the branches (like the above code)? What is the correct way to call the abstract constructor within the branches?

Captain Giraffe

What is the correct way to call the abstract constructor within the branches?

There is none.

There is a very good reason for this. The superclass has to be constructed before your class can be constructed.

When you write the code for the constructor you have a reasonably constructed object. Your parent is constructed at the very least.

This is neatly expressed with the initialing syntax for the constructor.

B::B(int a, int b):A(a, b) {  
     // A is complete. now we can do stuff.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Abstract class constructor initialization

From Dev

Check if type is derived from abstract generic class

From Dev

Calling the constructor of an abstract class

From Dev

Call constructor in an abstract class

From Dev

Call abstract method from class constructor

From Dev

using constructor in abstract class

From Dev

Using Overloaded Operator from Abstract Class in Derived Class

From Dev

Java abstract class, abstract constructor

From Dev

Calling base class constructor from derived class constructor

From Dev

Java constructor of an abstract class

From Dev

Call constructor from "abstract" base "class"

From Dev

Casting from abstract base class pointer to derived class

From Dev

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

From Dev

Java abstract class constructor

From Dev

Constructor of class derived from QCustomPlot

From Dev

Move constructor for derived class

From Dev

C++: Use pointer from abstract class for accessing method of derived classes; derived class apears to be abstract as well

From Dev

Mocking an abstract class derived from an abstract class

From Dev

Execution of child class' static block derived from abstract class

From Dev

Calling overloaded base constructor from derived class

From Dev

Trying to make derived class, compiler expects default constructor for abstract base

From Dev

how to access derived class object from abstract class in boost python

From Dev

calling a constructor from a derived class

From Dev

Call constructor in an abstract class

From Dev

Overloaded constructor in derived class

From Dev

Calling base class constructor from derived class constructor

From Dev

Error on constructor in derived class

From Dev

Constructor in base and derived class

From Dev

New derived class from abstract classes

Related Related

  1. 1

    Abstract class constructor initialization

  2. 2

    Check if type is derived from abstract generic class

  3. 3

    Calling the constructor of an abstract class

  4. 4

    Call constructor in an abstract class

  5. 5

    Call abstract method from class constructor

  6. 6

    using constructor in abstract class

  7. 7

    Using Overloaded Operator from Abstract Class in Derived Class

  8. 8

    Java abstract class, abstract constructor

  9. 9

    Calling base class constructor from derived class constructor

  10. 10

    Java constructor of an abstract class

  11. 11

    Call constructor from "abstract" base "class"

  12. 12

    Casting from abstract base class pointer to derived class

  13. 13

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

  14. 14

    Java abstract class constructor

  15. 15

    Constructor of class derived from QCustomPlot

  16. 16

    Move constructor for derived class

  17. 17

    C++: Use pointer from abstract class for accessing method of derived classes; derived class apears to be abstract as well

  18. 18

    Mocking an abstract class derived from an abstract class

  19. 19

    Execution of child class' static block derived from abstract class

  20. 20

    Calling overloaded base constructor from derived class

  21. 21

    Trying to make derived class, compiler expects default constructor for abstract base

  22. 22

    how to access derived class object from abstract class in boost python

  23. 23

    calling a constructor from a derived class

  24. 24

    Call constructor in an abstract class

  25. 25

    Overloaded constructor in derived class

  26. 26

    Calling base class constructor from derived class constructor

  27. 27

    Error on constructor in derived class

  28. 28

    Constructor in base and derived class

  29. 29

    New derived class from abstract classes

HotTag

Archive