samedi 13 juillet 2019

Calling parent class member function from derived class

I am new to C++ and have a code with several classes. I want to access member function of parent class from derived class. So I defined public inheritance and calling a member function, but getting error expected unqualified-id before ‘.’ token A.f1().

#include <iostream>

class A
{
    public:
    void f1 ()
    {
        std::cout<<"In class A::f1()";
    }
};

class B:public A
{
    public:
    void f2()
    {
        std::cout<<"In class B::f2() \n";
        std::cout<<"Calling A::f1() \n";
        A.f1(); //Error
    }
};


int main()
{
    B obj1;
    obj1.f2();

    return 0;
}

In the above scenario, to access f1() from B::f2() is public inheritance the only and recommended way?

Aucun commentaire:

Enregistrer un commentaire