1

When a class is derived from a base class, will the member functions of the base class become member function of the derived class. That is, for example if I write:

class A 
{ 
public : void f();
};
class B : public A 
{ 
public : void f1();
};

Now if the question is to name the member functions in class B, then will f() also become a member function of class B ?

DawidPi
  • 2,285
  • 2
  • 19
  • 41
TESLA____
  • 1,019
  • 7
  • 9
  • If it has same signature then it will hide base member function. You'll access though a reference to `B` then you'll get `B::f()` otherwise `A::f()`. See also: http://stackoverflow.com/q/411103/1207195 – Adriano Repetti Sep 18 '15 at 08:13
  • In this case there are no the same signatures. You can call `f()` on object of `class B`. That's why people created inheritance – DawidPi Sep 18 '15 at 08:15
  • 2
    There are no keywords `Public` or `Class` in c++. – eerorika Sep 18 '15 at 08:19

3 Answers3

1

Yes, class B has two functions: f1() and f().

If f() was protected in A, too, but f() could only be used from within the member functions of A and B (and from friends).

And if f() was private, B would have only one function f1().

alain
  • 11,939
  • 2
  • 31
  • 51
1
class A { 
public : void f();
};
class B : public A 
{ 
public : void f1();
};

Now code is valid. It is visible. Even when you change f1 name to f it is still visible in B class. If you wanna use it use scope operator ::.

Like this:

A::f()

More about basic inheritence http://www.learncpp.com/cpp-tutorial/112-basic-inheritance-in-c/

Morlacke
  • 61
  • 6
1

then will f() also become a member function of class B ?

No. f() is still a member function of class A, class B just inherits it. In your case it's a public member function and public inherit, means you can call f() on a B object.

B b;
b.f();

On the other hand, class B can define own member function f():

class A 
{ 
public : void f();
};
class B : public A 
{ 
public : void f(); // will hide A::f()
};
songyuanyao
  • 169,198
  • 16
  • 310
  • 405