overloaded function with mismatching parameters

class  A
{       public:
        void g(int i)
        {       cout<<"in a";
        }

};
class B:public A
{       public:
        void f()
        {       cout<<"in b";
        }
};
int main()
{       B b;
        b.f();   //vl call b::f()
        b.g(4);  //vl call a::g()
}
but
class  A
{       public:
        void f(int i)
        {       cout<<"in a";
        }
};
class B:public A
{       public:
        void f()
        {       cout<<"in b";
        }
};
int main()
{       B b;
        b.f();   //vl call b::f()
        b.f(4);  //bt here errror occurs not a matching protoype...
}
So,can we overload function in inheritance(which vary in
parameters)?

Comments

  1. Yes ,we can use overload function but actual terminology its called overriding here instead of b.f(4); you will write b.A::f(4);
    since this is the generic syntax and you can use this over any condition.

    ReplyDelete
  2. @Mufaddal yes it can be accessed the way you have mentioned or we have to declare same function with matching parameters in class B also..:)

    ReplyDelete

Post a Comment

Popular posts from this blog