I am trying to call a function in a derived class with the same name existing in the base class. The function simply prints class elements. For example:
class A // Base Class
{
public:
string a;
int b;
A(){}; //Default Constructor
A(string x,int y):a(x),b(y){} //Parametrized Constructor
~A(){}; //Destructor
void printInfo()
{
cout << a << endl;
cout << b << endl;
};
};
class B:public A // Derived Class
{
public:
string c;
B(){}; //Default Constructor
B(string z):c(z){} //Parametrized Constructor
~B(){}; //Destructor
void printInfo()
{
cout << a << endl;
//I am trying to call printInfo of A inside this function
};
};
So that in the output, when I create an object of B
(let's say B example;
) and try to use printInfo()
member function of class B (Ex: example.printInfo();
), I would print first c and then a and b. Also it will not be limited with 2 classes, but more than 5 classes having a common printInfo()
member function. How do I achieve this ?