answersLogoWhite

0


Best Answer

A base class constructor is simply a constructor that is declared within a base class. There is nothing particularly special about them since all constructors are only of relevence and applicable to the classes in which they are declared.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

Classes are not base class of itself. Every class in Java is a derived class. Every class in Java has 'Object' as the Superclass. The Object class is the only class in JAVA that has no Superclass!

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

Unsurprisingly, a virtual base class is a base class that is declared virtual:

struct a {};

struct b : virtual a {};

Virtual bases are useful when several classes derive from the same base such that two or more of those derived classes could also be used as the direct base classes of a more derived class. Consider the following hierarchy without virtual bases:

struct A {};

struct B : A {}

struct C : A {}

struct D : B, C {}

D obj; // construct a D

In order to construct a D object we must first construct its most direct base classes, B and C. However, class B directly inherits from class A so, before we can construct a B object we must first construct an A object. Once the A object is constructed we can construct the B object. Once the B object is constructed we can begin constructing the C object. However, class C directly inherits from class A so we must construct another A object. Once the second A object has been constructed, we can begin constructing the C object. Finally, we construct the D object.

In other words, the order of construction is: A, B, A, C, D.

The problem with this is that D inherits two instances of the A class (and all its members). This leads to ambiguity because we cannot refer to A implicitly. Ideally, what we want is just one instance of A shared by both B and C, and we achieve this by declaring A to be a virtual base of both B and C:

struct A {}; struct B : virtual A {}

struct C : virtual A {}

struct D : B, C {}

When a hierarchy contains a virtual base, the most-derived class in the hierarchy becomes responsible for its construction. Thus when we construct the D object, A is constructed before we begin constructing either B or C. Once A is constructed, B and C are constructed and both will share that one instance of A. Then D is constructed.

Thus the order of construction is: A, B, C, D.

Note that if we were to construct an object of type B in its own right, B would be the most-derived class in the hierarchy thus it would construct A just as if it were a non-virtual base.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the definition of a base-class constructor?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you declare and define the constructor within class?

Yes, you can declare and define the constructor within a class. A constructor is a special member function of a class that is automatically called when an object of the class is created. It is used to initialize the object's data members. The constructor can be declared and defined within the class definition or can be defined outside the class definition using the scope resolution operator (::).


Why you use constructor instead of function?

For every class an empty constructor will be defined automatically by default unless you provide a constructor definition manually. Constructor in a class can be used to initialize variables or perfrom some basic functionallity whenever an object is created.


Default constructor in java?

If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. The default constructor is ALWAYS a no-arg constructor. (Obviously the compiler has no clue what all arguments you might want for your class. So it takes the safe way out with a no argument constructor) A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own no-arg constructor.


What is an empty constructor?

An empty constructor takes no arguments and calls the default constructor


What is the difference between implicit and explicit call of constructor in c plus plus?

An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.

Related questions

What are the definition of building constructor?

Somebody who builds structures.


Can you declare and define the constructor within class?

Yes, you can declare and define the constructor within a class. A constructor is a special member function of a class that is automatically called when an object of the class is created. It is used to initialize the object's data members. The constructor can be declared and defined within the class definition or can be defined outside the class definition using the scope resolution operator (::).


Why you use constructor instead of function?

For every class an empty constructor will be defined automatically by default unless you provide a constructor definition manually. Constructor in a class can be used to initialize variables or perfrom some basic functionallity whenever an object is created.


Can constructor be declared as constant in c plus plus?

No. Constructors initialise objects and, by definition, must be able to modify the member variables. Uninitialised members are a disaster waiting to happen even without a constructor declared const! Thankfully, the compiler won't permit a const constructor.


Default constructor in java?

If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. The default constructor is ALWAYS a no-arg constructor. (Obviously the compiler has no clue what all arguments you might want for your class. So it takes the safe way out with a no argument constructor) A no-arg constructor is not necessarily the default (i.e., compiler-supplied) constructor, although the default constructor is always a no-arg constructor. The default constructor is the one the compiler provides! While the default constructor is always a no-arg constructor, you're free to put in your own no-arg constructor.


What is an empty constructor?

An empty constructor takes no arguments and calls the default constructor


What is the difference between implicit and explicit call of constructor in c plus plus?

An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.


When a object is passed to a function as argument why it is not generating an error if the parameterized constructor is defined and not the default constructor?

default constructor is used only when the programmer does not use a constructor to initialize objects. Once the programmer defines a constructor then the default constructor is no longer used


Can you call constructor?

yes we can call constructor


What is use of constructor in java?

Constructor is used to do something (written in constructor) immediately after object creation.


What is a default parameterized constructor?

There is no such thing as a default parameterized constructor. The default constructor is always the 'no-arg' constructor and does not take any parameters or arguments as input


What happens when no constructor function is declared in a class - in Java?

When any constructor is deffined in your class, the java compiler create a default no argument constructor for you. This constructor only have an invocation to the super class constructor (" super( ) ").