[Solved] Classes and fstream

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

[Solved] Classes and fstream

Post by zodiac976 »

Should I be placing every variable inside classes as private
or protected? Sometimes it's annoying to make an accessor
for each private/protected variable but I am used to it. I am
also trying to figure out if it is possible setting a private or
protected variable through file streaming for example:

Code: Select all

#include <iostream>
#include <fstream>
using namespace std;

class Test
{
     public:
         inline void setVal(int temp) {val = temp;}
         inline int getVal() const {return val;}
     private:
          int val;
     protected:
};

int main()
{
     Test testMain;

     ifstream file("test.txt");   //Say I have the number(15) in the file and I want to assign it to the private variable
          file >> ?;              //using the testMain object. I tried a lot of ways but they do not work.
     file.close();

     return 0;
}
I already searched google for this and found nothing.
Last edited by zodiac976 on Mon Jun 29, 2009 8:13 pm, edited 1 time in total.
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: Classes and fstream

Post by Scoody »

You can overload the >> operator to do it the way you want.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Classes and fstream

Post by zodiac976 »

Scoody wrote:You can overload the >> operator to do it the way you want.
Good idea except I have no experience in overloading
operators but I will look into that. Thanks.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Classes and fstream

Post by MarauderIIC »

zodiac976 wrote:Should I be placing every variable inside classes as private
or protected?
Technically, yes. You don't make accessors for every variable, just the ones that other things need to access.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Classes and fstream

Post by zodiac976 »

So I would have to make accessors for it because
the only way to access them is through a classes
methods anyways? Why would I put variables as
private with no accessor?
User avatar
Innerscope
Chaos Rift Junior
Chaos Rift Junior
Posts: 200
Joined: Mon May 04, 2009 5:15 pm
Current Project: Gridbug
Favorite Gaming Platforms: NES, SNES
Programming Language of Choice: Obj-C, C++
Location: Emeryville, CA
Contact:

Re: Classes and fstream

Post by Innerscope »

So I would have to make accessors for it because
the only way to access them is through a classes
methods anyways?
Is this a question? If you have private variables that need to be accessed by another class, then the most proper implementation would be to have accessor functions. However, this is not required. You need to make a decision whether or not something should be private/public. Base that decision on what it needs to accomplish, rather than forcing strict OOP design methodology.
Why would I put variables as
private with no accessor?
If they are only going to be used by the class they are members of. I'd say that gives you the most reason to make them private.
Current Project: Gridbug
Website (under construction) : http://www.timcool.me
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Classes and fstream

Post by zodiac976 »

Well normally I make a class and variables I want to use for
it and make accessors for each one but if I happen to need
them for another class I just use inheritance.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Classes and fstream

Post by dandymcgee »

zodiac976 wrote:Well normally I make a class and variables I want to use for
it and make accessors for each one but if I happen to need
them for another class I just use inheritance.
Wait.. what? You use accessors to access a class's variables from within itself, and use inheritance to access them from another class?

..Are you sure you know what an accessor is?

EDIT: See http://elysianshadows.com/phpBB3/viewto ... 631#p42631.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Classes and fstream

Post by zodiac976 »

Yea my bad I understand that now...
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Classes and fstream

Post by dandymcgee »

zodiac976 wrote:Yea my bad I understand that now...
Cool. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply