I was learning about inheritance in C++. So whenever a object of a class is created a constructor is called. And constructor is used to initialize the class variables.
#include<bits/stdc++.h>
using namespace std;
class Base
{
protected:
int x;
public:
Base(int a): x(a)
{
cout<<"Base"<<endl;
}
};
class Derived: public Base
{
private:
int y;
public:
Derived(int b):y(b)
{
cout<<"Derived"<<endl;
}
void print()
{
cout<<x<<" "<<y<<endl;
}
};
int main()
{
Derived d(20);
d.print();
return 0;
}
Since here I am creating object of base class and calling print function on this. So I should get output. But my code is giving compiler error, why? Can anyone help me understanding this?