Littlebitter
Littlebitter

Reputation: 701

using base class methods that use private field

I have a class, it has several public methods that use one private field that they all changed and use this field in their computations.

I need subclass from this class with few new methods, new methods won't use this field. I expect those methods from the base class behave the same as they did, but a little bit confused that they use private field.

Are private field accessible in the subclass? Should I also declare a field with same type and name in my subclass?

Upvotes: 0

Views: 2129

Answers (5)

mar.na
mar.na

Reputation: 110

You could use the public functions (which use a private field) in you drived class. You should not declare a field with same type!! But you could not direct access a private field from the base class in the drived class. Use for this a public function from your base class (e.g. setter and getter functions)

Sample:

//***********************************
//Base Class
//***********************************
class Base
{
public:
    Base();
    ~Base();

    void increment();
    void decrement();

    void get();

private:
    int id_;
};

Base::Base()
    : id_(0)
{
}

Base::~Base()
{
}

void Base::increment()
{
    ++id_;
}

void Base::decrement()
{
    --id_;
}

int Base::get()
{
    return id_;
}

void Base::set(id)
{
    id_ = id;
}

//***********************************
// Drived Class
//***********************************
class Drived() : public Base
{
public:
    Drived();
    ~Drived();

    void plus(int a);
    void minus(int b);
};

Drived::Drived()
    : Base()
{
}

Drived::~Drived()
{
}

void Drived::plus(int a)
{
    int i = get();
    set(i + a);             // <- OK
}

void Drived::minus(int b)
{
    id_ -= b;               // <- Not allowd; id is private
}

Upvotes: 0

mathematician1975
mathematician1975

Reputation: 21351

Private member data is not accessible to derived classes. If you want to access this data, change it to protected. Public member functions of the base class that are used to change the private member will still be available to your derived class but direct access of base class private data is not allowed. All public and protected fields of the base class will be available to your derived class (assuming you are using public inheritance)

Upvotes: 3

Alex Chamberlain
Alex Chamberlain

Reputation: 4207

In the derived class members, you cannot access the private field. However, the base class members, which you have inherited through the virtue of them being public, can of course still access it. The base member functions will be visible through the derived class if you used public inheritance, but only to the derived class if you used protected or private inheritance. protected and private inheritance is rarely used, though default if you are using the class keyword.

For further clarification, if you did want to access the field, you should define accessors in the base class with protected access. This would allow you to control the access to the private field.

However, you should ask yourself whether polymorphism is the correct solution here? Does the derived class pass the is-a test? Is the base class's destructor virtual?

Upvotes: 0

user1773602
user1773602

Reputation:

Most posters seem to misunderstood your question.

YES, you can use those public methods. That is the whole point of private members - they can be accessed by any member of the same class be they public, protected or private

Upvotes: 1

bash.d
bash.d

Reputation: 13207

Private fields are never accessible to derived classes. If you declared your methods which access the private field as public, the derived classes can use them.

Upvotes: 1

Related Questions