I am new to c++ I have 2 queries regarding how a base constructor is called.
1.) Lets say my code looks somewhat like this.
#include<iostream>
using namespace std;
class Base {
public:
Base() { cout<<"Constructor: Base"<<endl; }
virtual ~Base() { cout<<"Destructor : Base"<<endl; }
};
class Derived: public Base {
public:
Derived() { cout<<"Constructor: Derived"<<endl; }
~Derived() { cout<<"Destructor : Derived"<<endl; }
};
int main() {
Base *Var = new Derived();
delete Var;
return 0;
}
I was told that a base constructor has to be called explicitly before defining the derived constructor by initialization list. But here without any call to the base constructor, the code is working as expected.
**The output for the above problem is**
Constructor: Base
Constructor: Derived
Destructor : Derived
Destructor : Base
2.) Now suppose the base class constructor takes parametrized arguments, but the derived constructor is empty. In the main function, I declare a derived object without any argument to the constructor. what will happen? Any way to separately pass the base constructor of that object an argument?
Thanks.