This is the one way to access the member function of base class with help of scope resolution operator. But member functions have same names display().
#include<iostream>
using namespace std;
class person{
public:
int age;
void display() /////////// One member function same name
{
cout<<age;
}
};
class men:public person
{
public:
int height;
void display() ///////// Other member function same name
{
cout<<height;
person::display();
}
};
main()
{
men A;
A.age=25;
A.height=6;
cout<<"results is"<<endl;
A.display();
}
See this 2nd Code, Two different function in base and derive class display() and showdata() .Both are working same as above code snippet.
#include<iostream>
using namespace std;
class person{
public:
int age;
void showdata() ////////////// One member function
{
cout<<age;
}
};
class men:public person
{
public:
int height;
void display() ////////// another member function
{
cout<<height;
person::showdata();
}
};
main()
{
men A;
A.age=25;
A.height=6;
cout<<"results is"<<endl;
A.display();
}
Which one is the good approach that gives actual benefits of inheritance? should we use the same named member functions in both classes ( base and derived class ) ? or should we use difference named member functions in both classes ( Base and derived ).
And i know it is necessary to use base member function and derived class member function, because according to inheritance, derived class hold all the characteristics of base class too so it will hold the member function of base class too.