Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am teaching myself programming and I am in the middle of making a humongous class. To improve readability and function I want to split some of the functions of this class into smaller classes. I have managed to do this for some tools functions that do not need to be restricted per object and that pass a variable back as needed. However when I am trying to do something a bit more complicated, such as have a class that reads in data and stores it in arrays that my main class can then read from and store what it reads or use what it reads to change a private variable, I am completely stuck.

For example, if I make class Num with public arr1[20] and arr2[20] and two member functions that fill these with 20 numbers, how would I then read a specific cell or read the array with my main class so I can for example take the number of cell 4 and put it into its private variable, or compare arr1[16] < arr1[17] so do this?

Basically, I am having issues with the main class being able to read, copy or compare data from another class.

What I have tried:

I have tried moving various variables and functions in and out of classes, making them scoped to their source while in and out of them... I am just not sure how to make these classes talk to and use each others information, either public or private. Of course I can use set/gets in theory, but I am not sure how to lay those out, or how to lay out the code to allow set/gets to function the way I intend.
Posted
Updated 16-Apr-17 10:10am
Comments
Richard MacCutchan 15-Apr-17 4:31am    
A class that holds data should provide methods which allow access to the data. The whole point of OOP is that the data is only accessed through methods of the class. How that data is held internally is no one else's business.

1 solution

Have a look at friend classes ànd functions. A friend has access to private and protected members. Be careful with them though as they are easy to abuse.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900