Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ inherited class has member of same name

Tags:

In C++ you can put a member in a base class and a member with the same name in the inherited class.

How can I access a specific one in the inherited class?

like image 609
clamp Avatar asked Apr 13 '10 16:04

clamp


People also ask

Can derived class have same name as base class?

In C++, function overloading is possible i.e., two or more functions from the same class can have the same name but different parameters. However, if a derived class redefines the base class member method then all the base class methods with the same name become hidden in the derived class.

Do inherited classes get private members?

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.

What members of the class are inherited?

The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive.

What happens when a class is inherited as private?

With private inheritance, public and protected member of the base class become private members of the derived class. That means the methods of the base class do not become the public interface of the derived object. However, they can be used inside the member functions of the derived class.


1 Answers

In that case you should fully qualify a member name.

class A
{
public:
  int x;
};


class B : public A
{
public:
  int x;
  B() 
  { 
    x = 0;
    A::x = 1;
  }
};
like image 136
Kirill V. Lyadvinsky Avatar answered Oct 07 '22 00:10

Kirill V. Lyadvinsky