-3

I'm very new at this(object programming) and I am trying to access and then change an protected array from inherited class. But when I change it in derived class it doesn't change on in base class and that is what I want to do.

Is there a way to connect these two, like when you pass function argument by reference?

#include <iostream>
using namespace std;
class a {
protected:
    int i=0;

public:
    void print(){
        cout<<i<<endl;
    }

}a;

class b : protected a {
public:
    void print(){
        i++;
        cout<<i<<endl;
    }

}b;

int main(){
    a.print(); //0
    b.print(); //1
    a.print(); //0  - I want this to be 1
return 0;
}

Thanks in advance!

2 Answers2

3

Your code is effectively doing

Ford myFord;                     // a a;
BlueFord yourFord;               // b b;

myFord.printMileage();           // a.print();
yourFord.driveAndPrintMileage(); // b.print(); <- does more than printing!
myFord.printMileage();           // a.print();

There is no reason why myFord's mileage should change when yourFord is being driven. They are the same kind of car (a Ford, with the latter being the special blue edition), but they are not the same car (one is yours, one is mine). Driving one doesn't change the mileage of the other.

You use the same name for the type (Ford) and the instance (myFord) of each class/variable. Those are fundamentally different things and giving them the same name doesn't make them one and the same thing (it just makes things more confusing for you).

Making i a static variable would "sync" it between all the instances - it's as if each Ford car was always connected to the internet and broadcasted changes to i to all other Fords. Imagine static int i as a "total miles driven by all (blue) fords ever" counter. But note that a static variable no longer lives in one of the instances (it's not stored in each Ford) but somewhere centrally (some Ford company server).

But the entire idea of object oriented programming is getting rid of global state (which includes static variables) and have each instance manage its own state. I don't want my Ford to talk to all other Fords constantly, relying on some external connection.

Max Langhof
  • 23,383
  • 5
  • 39
  • 72
0

a and b are unrelated objects, each with their own copy of the i member variable. a.i and b.i are different variables, and so changing one does not affect the other.

Also, your class names are poorly chosen. You have an a class and an a object. That's just confusing. Capitalize the first letter of class names instead. Name them A and B.

It seems that what you want to do here is call the two different print() functions on the same object. You can do:

B b_obj;           // Create an object of type B
b_obj.A::print();  // Calls A::print() on b_obj
b_obj.print();     // Calls B::print() on b_obj
b_obj.A::print();  // Calls A::print() on b_obj

Note though that there's an issue with the print() function. Both A and B have one, and this will result in some surprising behavior when dealing with polymorphism. However, I assume you've not reached that part yet. For now, just keep in mind that having a member function in a derived class that has the same name as a function in a base class can be very problematic, unless the base function is marked as virtual. But I guess you'll come to that part of the book (or whatever you're using to learn C++) soon enough.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96