Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Using Class Objects (http://www.velocityreviews.com/forums/t948890-using-class-objects.html)

Mike Copeland 07-31-2012 08:38 PM

Using Class Objects
 
I'm trying to learn and adopt some new programming techniques, and
I've reached a roadblock. Specifically, although I have most of a class
defined and declared, I don't know how to use it.
In the code below I have functions that populate the elements of a
class object, but I don't know how to (1) store the object in the
declared map and (2) access/update the information in the objects that
are stored.
My intent here is to have one or more objects stored in the map, but
to instantiate each and use information from one for a cycle of
activity, followed by placing it back into the map. These activities
will repeat off and on throughout the program's execution, and I need to
reacquire some of the objects (one at a time) to use and update the
information that's current in the object.
So, I need to initially instantiate each object as needed, use and
modify its data values, save the object back into the map, use another
map object, and (possibly) reuse any of the objects I've processed. My
quandry is how to for each object initially store, access/update it as
needed, restore it and build/use another object, etc.
The following code doesn't compile, telling me I'm trying to use my
declarations incorrectly:

pfWork.setFileName(printFile);// store new print file info object
pfWork.setIsOpen(true), pfWork.clrRecCount();
print_map[cRCode] = pfWork; // cRCode is a character variable

The class declarations are:

class PrintFile // Print File class
{
bool bIsOpen; // private members
bool bIsUsed;
int nRecords;
string strFileName;
public:
PrintFile(const string sFileName) : // constructor
bIsOpen(true), bIsUsed(true), nRecords(0),
strFileName(sFileName) {};
PrintFile() : // default constructor
bIsOpen(false), bIsUsed(false), nRecords(0),
strFileName("") {};
PrintFile(const PrintFile &pf) : // copy constructor
bIsOpen(pf.bIsOpen), bIsUsed(pf.bIsUsed),
nRecords(pf.nRecords),
strFileName(pf.strFileName) {};
PrintFile &operator= (const PrintFile &rhs) // operator =
{
if(this == &rhs) return *this; // don't assign to self
bIsOpen = rhs.bIsOpen;
bIsUsed = rhs.bIsUsed;
nRecords = rhs.nRecords;
strFileName = rhs.strFileName;
return *this;
};
// access private members
bool getOpened () const { return bIsOpen; };
bool getUsed () const { return bIsUsed; };
int getRecords () const { return nRecords; };
string getFileName () const { return strFileName; };
void setFileName (const string sfn) { strFileName = sfn; };
void setIsOpen (const bool bState) { bIsOpen = bState; };
void setIsUsed (const bool bState) { bIsUsed = bState; };
void addRecCount () { nRecords++; };
void clrRecCount () { nRecords = 0; };
}; // end of class PrintFile

typedef map<char, PrintFile> print_map;
print_map printFiles; // 1 object per event
PrintFile pfWork; // working print file object

Victor Bazarov 07-31-2012 09:12 PM

Re: Using Class Objects
 
On 7/31/2012 4:38 PM, Mike Copeland wrote:
> I'm trying to learn and adopt some new programming techniques, and
> I've reached a roadblock. Specifically, although I have most of a class
> defined and declared, I don't know how to use it.


Huh? My experience tells me that you have to know how to use your class
*before* you even attempt to declare/define it. Are you defining it in
a vacuum? What are your requirements? Where do they come from if not
from the *use* of that class?

> In the code below I have functions that populate the elements of a
> class object, but I don't know how to (1) store the object in the
> declared map and (2) access/update the information in the objects that
> are stored.
> [..]
> The following code doesn't compile, telling me I'm trying to use my
> declarations incorrectly:
> [..]


Have you read the FAQ? Pay close attention to #5.8.

V
--
I do not respond to top-posted replies, please don't ask

Jorgen Grahn 07-31-2012 10:04 PM

Re: Using Class Objects
 
On Tue, 2012-07-31, Victor Bazarov wrote:
> On 7/31/2012 4:38 PM, Mike Copeland wrote:
>> I'm trying to learn and adopt some new programming techniques, and
>> I've reached a roadblock. Specifically, although I have most of a class
>> defined and declared, I don't know how to use it.

>
> Huh? My experience tells me that you have to know how to use your class
> *before* you even attempt to declare/define it. Are you defining it in
> a vacuum? What are your requirements?


That was my problem when trying to read the code. I saw a PrintFile
class, but had a hard time imagining what the OP wanted it to *mean*.
The posting didn't provide any clues (I read it very quickly though).

I warmly recommend writing a sentence or two of class documentation
*before* starting to write down the class declatation.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Mike Copeland 07-31-2012 10:29 PM

Re: Using Class Objects
 
In article <jv9hnf$h38$1@dont-email.me>, v.bazarov@comcast.invalid
says...
> > I'm trying to learn and adopt some new programming techniques, and
> > I've reached a roadblock. Specifically, although I have most of a class
> > defined and declared, I don't know how to use it.

>
> Huh? My experience tells me that you have to know how to use your class
> *before* you even attempt to declare/define it. Are you defining it in
> a vacuum? What are your requirements? Where do they come from if not
> from the *use* of that class?
>

That's what I'm trying to do - learn how to use a class structure.
In this example, I'm converting an existing implementation that uses a
more simple struct and array, hoping to make the logic more flexible and
"up-to-date".
I don't know how to blend a class and a container object, both of
which I've had some experience but not together.

> > In the code below I have functions that populate the elements of a
> > class object, but I don't know how to (1) store the object in the
> > declared map and (2) access/update the information in the objects that
> > are stored.
> > [..]
> > The following code doesn't compile, telling me I'm trying to use my
> > declarations incorrectly:
> > [..]

>
> Have you read the FAQ? Pay close attention to #5.8.


No, I haven't. I'll do that...

Mike Copeland 07-31-2012 10:33 PM

Re: Using Class Objects
 
In article <slrnk1glis.ph6.grahn+nntp@frailea.sa.invalid>,
grahn+nntp@snipabacken.se says...
> On Tue, 2012-07-31, Victor Bazarov wrote:
> >> I'm trying to learn and adopt some new programming techniques, and
> >> I've reached a roadblock. Specifically, although I have most of a class
> >> defined and declared, I don't know how to use it.

> >
> > Huh? My experience tells me that you have to know how to use your class
> > *before* you even attempt to declare/define it. Are you defining it in
> > a vacuum? What are your requirements?

>
> That was my problem when trying to read the code. I saw a PrintFile
> class, but had a hard time imagining what the OP wanted it to *mean*.
> The posting didn't provide any clues (I read it very quickly though).
>
> I warmly recommend writing a sentence or two of class documentation
> *before* starting to write down the class declatation.
>

As I stated to Victor's reply, I'm trying to blend several
disciplines (class and STL container) in an attempt to make something
I've already developed that's crude and clumsy. I have some experience
with each, but my difficulty is in trying to combine them into something
that might be more elegant and performent than what I've already done.
Is there something wrong with trying to "get better" in this field?

Victor Bazarov 08-01-2012 12:21 AM

Re: Using Class Objects
 
On 7/31/2012 6:29 PM, Mike Copeland wrote:
> In article <jv9hnf$h38$1@dont-email.me>, v.bazarov@comcast.invalid
> says...
>>> I'm trying to learn and adopt some new programming techniques, and
>>> I've reached a roadblock. Specifically, although I have most of a class
>>> defined and declared, I don't know how to use it.

>>
>> Huh? My experience tells me that you have to know how to use your class
>> *before* you even attempt to declare/define it. Are you defining it in
>> a vacuum? What are your requirements? Where do they come from if not
>> from the *use* of that class?
>>

> That's what I'm trying to do - learn how to use a class structure.
> In this example, I'm converting an existing implementation that uses a
> more simple struct and array, hoping to make the logic more flexible and
> "up-to-date".
> I don't know how to blend a class and a container object, both of
> which I've had some experience but not together.


Those concepts are orthogonal. If your class is suitable to be kept in
a container, it doesn't matter whether the container is an array, or a
map, or whatnot. Essentially, if you already have some code that stores
your object in an array, all you really should need to do is replace the
declaration of the array with your map, and you should be pretty much
set. There is no difference in *how you use your class* whether it's
stored in a map or it's a pure stand-alone object. None. Whatsoever.

>[..]


V
--
I do not respond to top-posted replies, please don't ask

Nick Keighley 08-01-2012 10:13 AM

Re: Using Class Objects
 
On Jul 31, 11:33*pm, mrc2...@cox.net (Mike Copeland) wrote:
> In article <slrnk1glis.ph6.grahn+n...@frailea.sa.invalid>,
> grahn+n...@snipabacken.se says...
> > On Tue, 2012-07-31, Victor Bazarov wrote:


> > >> I'm trying to learn and adopt some new programming techniques, and
> > >> I've reached a roadblock. *Specifically, although I have most of aclass
> > >> defined and declared, I don't know how to use it.

>
> > > Huh? *My experience tells me that you have to know how to use your class
> > > *before* you even attempt to declare/define it. *Are you defining it in
> > > a vacuum? *What are your requirements?


it does seem backwards...

> > That was my problem when trying to read the code. I saw a PrintFile
> > class, but had a hard time imagining what the OP wanted it to *mean*.
> > The posting didn't provide any clues (I read it very quickly though).


could the OP provide a short explanation of what PrintFile does?

how does a file get opened and shouldn't some sort of file handle be
included if the file is opened? Do you really need a copy CTOR and
assignment operator?


> > I warmly recommend writing a sentence or two of class documentation
> > *before* starting to write down the class declatation.

>
> * *As I stated to Victor's reply, I'm trying to blend several
> disciplines (class and STL container)


there isn't (or usually isn't) anything special about classes whne it
comes to the STL. If you can store an int or a string (which is
really
a class) in a map why is your class a problem?

> in an attempt to make something
> I've already developed that's crude and clumsy. *I have some experience
> with each, but my difficulty is in trying to combine them into something
> that might be more elegant and performent than what I've already done.
> * *Is there something wrong with trying to "get better" in this field?


none but we aren't clear what your problem is.

here's some docuemntaion for std::map
http://www.cplusplus.com/reference/stl/map/

can't you use insert() (or operator[]), find(), and erase() (though
consider not removing things from the map until you've actually
finished
with them).


Jorgen Grahn 08-01-2012 01:19 PM

Re: Using Class Objects
 
On Tue, 2012-07-31, Mike Copeland wrote:
> In article <slrnk1glis.ph6.grahn+nntp@frailea.sa.invalid>,
> grahn+nntp@snipabacken.se says...
>> On Tue, 2012-07-31, Victor Bazarov wrote:
>> >> I'm trying to learn and adopt some new programming techniques, and
>> >> I've reached a roadblock. Specifically, although I have most of a class
>> >> defined and declared, I don't know how to use it.
>> >
>> > Huh? My experience tells me that you have to know how to use your class
>> > *before* you even attempt to declare/define it. Are you defining it in
>> > a vacuum? What are your requirements?

>>
>> That was my problem when trying to read the code. I saw a PrintFile
>> class, but had a hard time imagining what the OP wanted it to *mean*.
>> The posting didn't provide any clues (I read it very quickly though).
>>
>> I warmly recommend writing a sentence or two of class documentation
>> *before* starting to write down the class declatation.
>>

> As I stated to Victor's reply, I'm trying to blend several
> disciplines (class and STL container) in an attempt to make something
> I've already developed that's crude and clumsy. I have some experience
> with each, but my difficulty is in trying to combine them into something
> that might be more elegant and performent than what I've already done.
> Is there something wrong with trying to "get better" in this field?


Of course not -- but that's not what I'm talking about. Did you read
my response, or just Victor's?

I'm just saying it would be helpful -- you and us to -- if you would
explain in plain words the meaning (or "purpose", or "responsi-
bilities") of class PrintFile.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Nick Keighley 08-01-2012 01:28 PM

Re: Using Class Objects
 
On Aug 1, 2:19*pm, Jorgen Grahn <grahn+n...@snipabacken.se> wrote:
> On Tue, 2012-07-31, Mike Copeland wrote:
> > In article <slrnk1glis.ph6.grahn+n...@frailea.sa.invalid>,
> > grahn+n...@snipabacken.se says...
> >> On Tue, 2012-07-31, Victor Bazarov wrote:


> >> >> * * I'm trying to learn and adopt some new programming techniques, and
> >> >> I've reached a roadblock. *Specifically, although I have most of a class
> >> >> defined and declared, I don't know how to use it.

>
> >> > Huh? *My experience tells me that you have to know how to use yourclass
> >> > *before* you even attempt to declare/define it. *Are you defining it in
> >> > a vacuum? *What are your requirements?

>
> >> That was my problem when trying to read the code. I saw a PrintFile
> >> class, but had a hard time imagining what the OP wanted it to *mean*.
> >> The posting didn't provide any clues (I read it very quickly though).

>
> >> I warmly recommend writing a sentence or two of class documentation
> >> *before* starting to write down the class declatation.

>
> > * *As I stated to Victor's reply, I'm trying to blend several
> > disciplines (class and STL container) in an attempt to make something
> > I've already developed that's crude and clumsy. *I have some experience
> > with each, but my difficulty is in trying to combine them into something
> > that might be more elegant and performent than what I've already done.
> > * *Is there something wrong with trying to "get better" in this field?

>
> Of course not -- but that's not what I'm talking about. Did you read
> my response, or just Victor's?
>
> I'm just saying it would be helpful -- you and us to -- if you would
> explain in plain words the meaning (or "purpose", or "responsi-
> bilities") of class PrintFile.


I consider this step zero. Before we do anything else get a clear
idea
what we are trying to do. Sometimes we can't state the final goal but
we might be able "clarify requirement", "prototype subsystem" or "try
out this feature"

By the time you start hammering code in step zero should have been
completed in some form.


--
If you don't know what your program is supposed to do,
you'd better not start writing it.
-- Esdger Dijkstra's Law of Programming Inertia



All times are GMT. The time now is 01:39 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.