1
votes

I'm working on a project to learn about object oriented programming. From what I understand, when a class is derived from a base class, it can access all of the public member functions of the base class. In C++ a public inheritance means that private members cannot be accessed. So what happens when a private member function is modified in a base class?

I have tried building some test programs to see what happens, but I'm still extremely confused. I also tried looking online to see if I can find any answers, but I didn't really find any.

class Base 
{
    private:
        int length;
    public 
        void increaseLengthByOne();
};

void Base::increaseLengthByOne()
{
    this->length++;
}

class Derived : public Base 
{
    private:
        int dLength;
    public: 
        void newFunction();
};

void Derived::newFunction()
{
    printf("New function working");
    increaseLengthByOne();
}

From what I understand, dLength won't be changed and Length can't be changed. What does the increaseLengthByOne function do then? And how would I make it so the increaseLength function can increase dLength?

5
tip: include some couts to verify your assumptions. Is increaseLengthByOne() really no able to modify length when called from the derived? - 463035818_is_not_a_number
surely, this must have a dupe. - Walter
Your data attributes (length and dLength) are un-initialized - undefined behavior. - 2785528

5 Answers

2
votes

From what I understand, [...] Length can't be changed.

You've misunderstood. A member being private does not imply that it cannot be changed.

What does the increaseLengthByOne function do then?

It increases the member length.

1
votes

It's not because it's not visible that it doesn't exist or cannot access it. The method will be able to access the member variable because it was declared as being able to access it.

That's the principle of visibility for traditional method calls as well, such a method can access private or protected member of the class, even if the caller code cannot.

0
votes

How does a derived class use that function?

When the Base function is public or protected, the derived class function can simply invoke the Base function.

When the Base function is private, the derived class function can not directly invoke it.

Below I have lightly touched your code to initialize, and demonstrate derive class function invocation of Base functions.

Notes:

  • Remember to initialize your data attributes --- I prefer initialization list of ctor.

  • use cout to show what is happening, these are easily removed for submittal

  • learn to use gdb


#include <iostream>
using std::cout, std::endl;  // feature requires -std=c++17


class Base
{
private:
   int length;

public:
   Base(int lInit = 0) : length(lInit)  // init length
      { cout << "\n  length init: " << length << endl; }

   void increaseLengthByOne();
};

void Base::increaseLengthByOne()
{
   length++;   // this->length++;
   cout <<  "\n  length++ : " << length;
}

class Derived : public Base
{
private:
   int dLength;

public:
   Derived() : Base(1), dLength(0)  // init Base and dLength
      { cout << "\n  dLength init: " << dLength << endl; }

   void newFunction(int d);
};

void Derived::newFunction(int d)
{
   cout << "\n  Derived::newFunction(int)";
   cout << "\n  dLength a: " << dLength;
   dLength = d;
   cout << "\n  dLength b: " << dLength;
   increaseLengthByOne();
   cout << "\n  dLength c: " << dLength;
}


class T989_t
{
public:
   int operator()() { return exec(); } // functor entry

private: // methods
   int exec()
      {
         Derived d;
         d.newFunction(5);    
         return 0;
      }
}; // class T98_t

int main(int , char** ) { return T989_t()(); } // invoke functor

Output:

  length init: 1

  dLength init: 0

  Derived::newFunction(int)
  dLength a: 0
  dLength b: 5
  length++ : 2
  dLength c: 5
0
votes

A member that is private won't be accessible outside the class.But You can always write wrapper or helper function to give its access outside the world.This is happening in your case.So the value of length would increase.

0
votes

From what I understand, ... Length can't be changed.

length can't be changed directly outside Base class since it is a private member of Base class, however you can change it using public member function of Base class.

What does the increaseLengthByOne function do then?

Derived class can access Base class public/protected members as such when you call increaseLengthByOne in derived class it executes Base class function increaseLengthByOne which increases length.

how would I make it so the increaseLength function can increase dLength?

There are 2 ways of doing this
1) Just add dLength++ to your function newFunction() i.e

void Derived::newFunction()
{
    std::cout << "New function working";
    dLength++;
    increaseLengthByOne();
}

2) In case you want to keep function name increaseLengthByOne same then you can override it in derived class i.e

class Derived : public Base 
{
   private:
    int dLength;
   public: 
    void newFunction();
    void increaseLengthByOne();  //this overrides Base class function
};

void Derived::increaseLengthByOne()
{
    dLength++;
    Base::increaseLengthByOne();   //this increases length;
}