C++ problems with class inheritance

  c++

I have base class with some variables and functions and multyply child classes. I want to minimaze amount of code required in child classes as much as possible.

Example of my code:

#include <iostream>

class base{
public:
   int a = 10;
   int b;
   void print()
   {
      std::cout << a <<std::endl;
   }
};

class child: public base
{
   public:
      int a;
};

int main()
{
    child ch;
    ch.a = 20;
    ch.print();

}

As result number 10 was printed, that means base class variable a was used, but i need to use child class variable if it exists. So expected output for this example is 20.

Source: Windows Questions C++

LEAVE A COMMENT