-1

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.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • If you are going to use a fully qualified name like `person::showdata();` then it doesn't matter *at all*. Which boils this question down to being primarily opinionated. I suspect it will be closed as such (opinion based questions are off-topic). – StoryTeller - Unslander Monica Nov 21 '18 at 06:34
  • 2
    what about virtual methods? – ΦXocę 웃 Пepeúpa ツ Nov 21 '18 at 06:43
  • how? why do i need virtual? because, both method are working correctly, and showing both values from both classes. – Jawwad Rafiq Nov 21 '18 at 07:08
  • `Person* p = new Man(); p->display();` if display is virtual, `Man::display` will be called, even though the pointer is of type `Person`; if it is not, `Person::display` will be called even though the object actually is a `Man`. – Aconcagua Nov 21 '18 at 07:43

4 Answers4

2

I would rather do so:

class person
{
public:
    int age = 0;
    virtual ~person(){}
    virtual void display()
    {
        cout<<age;
    }
};

class men:public person
{
public:
    int height = 0;
    void display() override
    {
        cout<<height;
        person::display();
    }
};

Further, it will allow to use the advantages of polymorphism.

snake_style
  • 1,139
  • 7
  • 16
0

I prefer using different names for base and derived classed, as in some programming languages, it may not accept the base class function's name without the override keyword.

Better to use different names, and keep us away from conflicts.

mouseymaniac
  • 259
  • 2
  • 9
0

In this example is not much to do, the method showdata in the base class person is public, and men class inherits person publicly too, since men IS a person, men can call showdata directly, no need to do something like person::showdata

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • if men call showdata() directly. then it will show only values of data member of base class that is age. but in inheritance, derived class should show the both values as well as parent class, as it holds all the characteristics of base class. – Jawwad Rafiq Nov 21 '18 at 07:01
  • how? why do i need virtual? because, both method are working correctly, and showing both values from both classes. – Jawwad Rafiq Nov 21 '18 at 07:08
0

I'm in favor of a simple rule that Titus Winters presented at CppCon 2018: Name functions the same when they do the same thing, so the caller doesn't need to know how the difference.

Your first code violates this principle:

person &p = getMen();
p.showData();

This is because showData() isn't virtual. This can be fixed in person by writing: virtual void showdata().

As it looks like you indeed intend to do the same thing for both classes, I would prefer this over your 2 suggestions.

JVApen
  • 11,008
  • 5
  • 31
  • 67