I am c beginer to c++ and i found something strange and was not able to understand couldn't find anything on internet so wanted to ask here. The code i saw was this:
#include<iostream>
using namespace std;
class A
{
protected : int a;
public:
A(int a)
{
this -> a = a;
}
virtual void foo()
{
cout<<"A =" <<a <<endl;
}
};
class B: public A
{
public: B(int b): A(b + 1) {}
virtual void foo()
{
cout<<"B =" <<a <<endl;
}
};
int main(int argc, char * * argv)
{
A * a = new B(1);
a -> foo();
return 0;
}
the line that creates problem to me is this public: B(int b): A(b + 1) {}
in derived class. Where i can obserrve that Derived class inheriting the base class constructor and its output is B=2
. Could someone please explain me in detail how it became possible ? that in line A * a = new B(1); here "1" is passed as object and its value gets increased in A(b + 1) {}
constructor by inheriting from A class
in line public: B(int b): A(b + 1) {}
.