![]() |
|
#1
|
|||
|
|||
need help to define a class in C++hi
I am learning to program in C++. I have got some difficulties in defining a class. public ans private type is confusing. anyway the class I want to define should collect information about integers. she accepts integers through a method add(). at any time she returns the average of the integers, the median (value on the middle of the list, the sum, and the number of integer. I have thought about define like this: CPP / C++ / C Code:
and for yhe method I have thought about: CPP / C++ / C Code:
if someone can give me hints about that, that would be nice thank you for any help see ya |
|||
|
#2
|
|||
|
|||
|
Hey and welcome to the boards!
OK, your class definition looks okay except for a syntax thing. You're missing the semicolons at the end of each function definition. So it should be like this: CPP / C++ / C Code:
Next thing I will go into is the public & private properties of objects. The greatest advantage of Object Orientated programming, is the way we create different objects to handle small jobs, and get them to work together to achieve a larger goal. Before, we had pages upon pages of functions and structures, which all interacted with eachother. But it was very easy to get mixed up, 2 functions might accidentally use the same variable name - causing unpredictable errors & a very hard bug to find. There was no way to reserve variables for various "sections" - or objects - which should be exclusive for that function, and not accessible anywhere else. So objects solve this problem, but how? Public and Private (and Protected - but you won't see this for a bit) properties do this. An object is a collection of variables and functions which interact with eachother only, with some functions defined as public so that the object can be useful. I'll take your project for example. Your Statistics object will have 5 functions that you will want to be able to call, send data to & retrieve the answers - all from outside the object. Namely: mean(), mode(), median(), count() and add(). These functions must be made public, otherwise C++ will not allow you to call these functions outside the object. But your Statistics object will also contain a vector, an array of integers which will store the numbers that are added through add(). You don't want other objects to be able to access this - what if something goes crazy and tries to put a character here - not using the add function? If the vector is private, then nothing but the functions in the Statistics object will be able to access it. So you've to modify your code to do this. I'll leave this bit to you! Hope this helps Garth Farley |
|
#3
|
|||
|
|||
|
I understand better the difference public and private, and the fact that the array created by the function add will be only accessible by the other functions: mean, sum...
I try to understand the ifstream and its use: what I understand is with ifstream I can access a file, which contains the list of integers. the problem is that in my main file I will call the function by somethign like: list.count so I think writting ifstream list("list.txt") in the definition of the function is not really accurate. I know that I have to read the integers from a file ".txt", then I have to put them one by one in a container. I think in the main program I should have CPP / C++ / C Code:
CPP / C++ / C Code:
if I do something like that do I have to call "c.sum" c:container? is that better to read the file ".txt" in each function? thanks for your help. yann |
|
#4
|
|||
|
|||
|
hi
I thought about the class definition: the function class is actually in private, but this function should take the integer list from outside, isn't it? so I think it is better to create a container which will be private type and the function add will be in public to be able to access to the list. thanks for help yann |
|
#5
|
|||
|
|||
|
I thought of the definition:
in private: a vector of integer, which will contain the list and the function in public. in each function, I would like to call the vector list, but I don't know if the structure is correct. besides I would like to call the function sum and count in other function. is that possible? CPP / C++ / C Code:
thanks for all yann |
|
#6
|
|||
|
|||
|
First, you should use ifstream inside the object. Send the object the name of the file in a method - let the object itself have sole access to the file.
Inside the class definition, you can have a private ifstream variable. Then all the objects methods can read from the file. So, for example CPP / C++ / C Code:
You see, you can manipulate the file withiin the object, and no other object can access the file! For your seconf question, yes you can call all the functions that are in the object, if you are in the object. A member function has access to everything in the object. Hope this helps, GF |
|
#7
|
|||
|
|||
|
Hey Yannoush,
Are you enrolled in COMP5018? |
|
#8
|
|||
|
|||
|
yes.
you too? C programming is not easy, I am doing better and better (I hope!!) |
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Keeping track of a private variable outside the class | viperv80 | C++ Forum | 1 | 03-Dec-2003 18:25 |
| using class CFont | aidanh | C++ Forum | 0 | 11-Nov-2003 09:01 |
| Upgrade of BBCode class sometime today | JdS | GIDForums™ | 4 | 03-Nov-2003 08:18 |
| problem with creating class | mohammed | C++ Forum | 1 | 11-Oct-2003 10:04 |
| [class] Timer | JdS | PHP Code Library | 2 | 29-Apr-2003 13:20 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The