How to access a variable inside a class that is edited through functions within the class?

Mar 24, 2017 at 7:40pm
First of all let me say that my issue is NOT accessing a private variable. I know how to do that.

I'm currently working on a project for my college computer science class. Essentially we are creating a library program that manages patrons and books and allows them to check out books. This is the first assignment in which we have begun to use classes and I have run into a problem. I have 6 classes. I have a class for patron, book and checkout, and then patrons, books, and checkouts classes to manage it's appropriate class. Inside my patrons class, I'm using a dynamically allocated array to add patrons (of type patron) to the system. To allocate array space, I have a "patroncount" variable inside my patrons class. Everytime I add a new patron using a function inside the patrons class, I increase this patroncount variable. This all seems to be working well, however, I've run into a major roadblock. I want to access this patroncount variable outside of my patrons class, but I am not sure how to go about it. I have created a public function to access this private variable, so the problem IS NOT that I don't know how to access the private data of the class. The problem is that I do not know how to access this patroncount variable that has been edited by functions within the class in another class. I cannot simply use the getPatroncount function I have created because I would need to create an object for that and this object will not have my updated patroncount variable. How can I go about this?

My question basically boils down to:

How do I access a variable in a class that is edited through functions within the same class outside of the class?


Patrons function file (this is just the header file, assume that my functions all work as intended).

#include"patron.h"

class patrons{
private:
patron *patronlist;
int patroncap;
int patroncount;
public :
void initialize();
void incCounter();
void freespace();
void add();
void edit();
void deletepatron();
void find();
void print();
void printOwed();
void printMailing();
void printPatron();
void printFines();
void editFines();
int getPatroncount();
};

You can see the patroncount variable I want is defined here. The getPatroncount variable is in this class too. Some of my functions in this class, such as add, manipulate my patroncount variable. Now if I wanted to access this patroncount variable OUTSIDE this class (IE another class), I do not know what to do. I know I need to use my getPatroncount function since the data is private, but what object do I use to access the data since I did not assign my patroncount number to a certain object to begin with. I'm assuming that manipulating this variable within the class itself just changes the default value of the variable. But how can I access it if I'm actively changing the variable throughout the program?
Mar 24, 2017 at 8:53pm
You made it private, so it cannot be directly accessed from outside the class.
Will patronCount be the same for every instance of your patrons class?
If so, make it static static int patroncount;, then it will be accessible without an instance. The same goes for the getters and setters
1
2
static int getPatronCount();
static void setPatronCount(int count);


You'll have to define the static variable in the cpp file as static int patrons::patroncount = someInitialValue.

You would then access it like someInt = patrons::getPatronCount();.
Mar 24, 2017 at 8:59pm
> I'm assuming that manipulating this variable within the class itself
> just changes the default value of the variable.
you are wrong.
inside a member function, you are manipulating this instance.


> I did not assign my patroncount number to a certain object to begin with.
you did.



> I would need to create an object for that and this object
> will not have my updated patroncount variable.
pass the object by reference.
Mar 24, 2017 at 10:35pm
Thank you @JayhawkZombie!

Your answer guided me in the right direction and I've gotten it to work now. Thanks a lot!
Topic archived. No new replies allowed.