Posting Freak
Posts: 8,478
Threads: 128
Joined: 2008-07
Gender: Neuter
Country Flag: canada
IGN: Oooh
Server: Bera
Job: Empress
Farm: Stereo
Well there are basically 3 parts to reading a file...
1) open a file handle so it knows which file to look at
2) read the characters of the file (since it's a text file you basically can treat the file as one long string, starting reading from the top), convert it to format you want (double, char, etc.)
3) close the file when it hits the end
These are fairly common tasks so most languages make them easy, it is still a little complicated to allow flexibility but you can pretty much distinguish between the 3 steps. Just be happy you're not using Java, and don't have to wrap file i/o in try/catch statements.
In simple text files part 2 is taken care of by getline() - which takes the next line - and then splitting it up (into tokens) - and then converting tokens to the type you want, which seems to be double (so strtod).
Senior Member
Posts: 420
Threads: 2
Joined: 2008-09
My C++ is very rusty (no pun intended dr.rusty), but I somewhat remembered you can do something like this
char gender;
float gpa;
open file
file_handle >> gender >> gpa;
Again, I am not 100% sure, but somehow, I vaguely recall you can do something like that. Too early to Google check.
Test Mushroom
Posts: 10,045
Threads: 1,506
Joined: 2008-06
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: GuavaCowboy
Server: Zenith
Level: 10x
Job: Jett
Guild: L>
Gummy, I think that reads binary data. So that will read the first char fine, but it won't skip the space like fscanf.
Senior Member
Posts: 420
Threads: 2
Joined: 2008-09
Fiel Wrote:Gummy, I think that reads binary data. So that will read the first char fine, but it won't skip the space like fscanf.
Ahh, I see. I guess it'll be a read line + substr + casting.
Good to see you get back to C++
Posting Freak
Posts: 4,380
Threads: 186
Joined: 2008-07
Gender: Male
IGN: Volte
Level: XX
Job: Nuke
Guild: US Navy
Could someone help me out with "private" in object programming? I just don't get the use of it. I understand declaring things in private is supposed to make it "private", but I just can't think of a good way to use it or how it's actually "private". This is one of the examples my teacher gave me. He doesn't speak english very well which is why I had to make this thread and ask for SP's help.
Code: #include<iostream>
using namespace std;
// declaration section:
class Student
{
private:
int idNum;
public:
static double athleticFee;
void setIdNum(int);
void displayStudentData();
};
// implementation section:
double Student::athleticFee = 45.25;
void Student::displayStudentData()
{
cout<<"Student #"<<idNum<<"'s athletic fee is $"
<<athleticFee<<endl;
}
void Student::setIdNum(int num)
{
idNum = num;
}
int main()
{
Student aFreshman, aSophomore;
aFreshman.setIdNum(1234);
aSophomore.setIdNum(2345);
aFreshman.displayStudentData();
aSophomore.displayStudentData();
cout<<"The standard student athletic fee is $"<<
Student::athleticFee<<endl;
}
Now what I don't understand is how idNum (declared in the object/class) is private. When I run the program, it straight up tells me what the idnum is of aFreshman and aSophomore, so how is that private?
Administrator
Posts: 17,190
Threads: 2,153
Joined: 2008-09
Gender: Male
Sexual Orientation: Gay
IGN: Aesilyn
Server: Mardia
Level: 200
Job: I/L ArchMage
Guild: Animus
Private doesn't mean it can't be seen/touched, it means it can only be seen touched within a reference to it's owner.
http://msdn.microsoft.com/en-us/library/...s.80).aspx
It prevents the value from being touched or manipulated outside the class itself, more or less.
Posting Freak
Posts: 4,380
Threads: 186
Joined: 2008-07
Gender: Male
IGN: Volte
Level: XX
Job: Nuke
Guild: US Navy
Oh ok. So you can only use idNum if you have a variable declared with the class? (ex Student aFreshman). What about stuff put into public? If idNum was put into public. Could you use it without declaring it with the class?
Sorry that I'm not good with the terminology
Administrator
Posts: 17,190
Threads: 2,153
Joined: 2008-09
Gender: Male
Sexual Orientation: Gay
IGN: Aesilyn
Server: Mardia
Level: 200
Job: I/L ArchMage
Guild: Animus
Test Mushroom
Posts: 10,045
Threads: 1,506
Joined: 2008-06
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: GuavaCowboy
Server: Zenith
Level: 10x
Job: Jett
Guild: L>
Maybe this will help:
Public: Think of a public bathroom or drinking fountain. Anyone can come up and use it.
Protected: Think of a bouncer at a nightclub. "Sorry pal, you gotta be one of us to get in." If it's protected, only those who are part of that class or subclasses can access it ("get into the nightclub").
Private: Smeagol. "MY PRECIOUS! No one can haveses it! It's mine, mine!"
Administrator
Posts: 17,190
Threads: 2,153
Joined: 2008-09
Gender: Male
Sexual Orientation: Gay
IGN: Aesilyn
Server: Mardia
Level: 200
Job: I/L ArchMage
Guild: Animus
If I didn't already know what it meant I think that analogy would confuse me more, actually. :f6:
Posting Freak
Posts: 4,380
Threads: 186
Joined: 2008-07
Gender: Male
IGN: Volte
Level: XX
Job: Nuke
Guild: US Navy
Your comment helped me a bit Eosian... but damn there's just so much back and forth lol.
so it goes something like
-1234 is passed into function setIdNum (declared in the class)
-inside that function, 1234 is stored into idNum (doing it this way is the only way to store a value into the private idNum?)
-then displayStudentData is run in the main
-since displayStudentData is part of the class, it can display idNum in the cout.
if that's right, then yeah... there's a lot of back and forth. This is definitely the hardest thing for me to grasp out of all the programming things I've learned so far. It looks like the number is asked for (in this case just given) in the main, but to store it in the private int idNum of Student, it needs to pass through a function that's also part of Student.
this is going to take a lot of practice to get this down......
Administrator
Posts: 17,190
Threads: 2,153
Joined: 2008-09
Gender: Male
Sexual Orientation: Gay
IGN: Aesilyn
Server: Mardia
Level: 200
Job: I/L ArchMage
Guild: Animus
I think you got it now, yes.
Private & Protected methods have to go through a more circuitous route than public ones.
It's all about encapsulation.
Posting Freak
Posts: 4,380
Threads: 186
Joined: 2008-07
Gender: Male
IGN: Volte
Level: XX
Job: Nuke
Guild: US Navy
Senior Member
Posts: 354
Threads: 13
Joined: 2008-12
I don't really know much about C++, but from a design perspective; ideally, you'd want the displayCourseData() method to return a string that the program would then output, not print the result directly. That way you could use the object's logic in other ways (such as a windows applications) without having to rewrite the class for different implementations. Not that it matters much in your current context, but it's always best to learn good design from the start
Test Mushroom
Posts: 10,045
Threads: 1,506
Joined: 2008-06
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: GuavaCowboy
Server: Zenith
Level: 10x
Job: Jett
Guild: L>
Making displayCourseData() return a string breaks encapsulation.
Posting Freak
Posts: 8,478
Threads: 128
Joined: 2008-07
Gender: Neuter
Country Flag: canada
IGN: Oooh
Server: Bera
Job: Empress
Farm: Stereo
It seems standard in Java to have print functions (usually via toString() ) return a string describing the object, which you then print to whatever device. I'm not sure what the benefit is of having the function go directly to cout instead of doing cout<<course.displayCourseData()<<endl; and having string CollegeCourse::displayCourseData() as the header.
Senior Member
Posts: 420
Threads: 2
Joined: 2008-09
One of the concept of OOP is encapsulation. What it means is that variables/attributes are not being exposed outside of the class and that you would need to manually allow updates via some methods.
The 3 keywords are public, protected, private.
Public methods/attributes can be referred by external objects. It is quite common for methods to be public, so that others can call it, but it is not suggested to have attributes public. IIRC, one of the good use of public attributes is to declare constants. In Java iirc, you can do something like public final SOME_CONST = 1. This allows you to access the constant SOME_CONST directly via the class, which simplify lots of unnecessary steps just to get its value. The danger of having public attributes is that client programs can modify it directly. This leads to a whole world of pains.
Protected simply means only itself or its children can refers to. Protected methods/variables are the bread and butter of inheritance. It is the most flexible keyword of the 3. Most of the times, I made my methods protected so that in the future, I may extends the class without having to modify anything.
Private is the most strict of them all. Only the concrete object can access the private area. If you're sure an attribute or a method is a dead end, make it private.
Posting Freak
Posts: 4,302
Threads: 256
Joined: 2008-07
Gender: Male
Level: 251
GummyBear Wrote:My C++ is very rusty (no pun intended dr.rusty), but I somewhat remembered you can do something like this
char gender;
float gpa;
open file
file_handle >> gender >> gpa;
Again, I am not 100% sure, but somehow, I vaguely recall you can do something like that. Too early to Google check.
Fiel Wrote:Gummy, I think that reads binary data. So that will read the first char fine, but it won't skip the space like fscanf.
Actually I'm quite sure it does skip spaces (unless you're trying to read a char instead, in which case it will read the space as the char).
|