Categories
Development

How to modify parent public member variable from child member function?

programming beginner here. I’ve been trying to figure this out myself for a while,
Can anyone tell me why this prints 5 and not 8? As far as I know this should work I’ve tried pass by reference and using pointers but as I’m still a bit of a noob perhaps I haven’t quite got it yet so if anyone could help me out and point me in the right direction id appreciate it.
Cheers.

#include <iostream>
using namespace std;


class classA {
  public:
    int a = 5;

    void print(){
      cout << a << endl;
    }
};

 class classB : public classA {
   public:
     int one;
     void modify (){
       cin >> one;
       if (one == 1){
         a =+ 3;
       }
     }
 };

int main()
{

    cout << "Hi, push '1'" << endl;
    classA printobj;
    classB modifyobj;
    modifyobj.modify();
    printobj.print();

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *