Using Class Objects

M

Mike Copeland

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
 
V

Victor Bazarov

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
 
J

Jorgen Grahn

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
 
M

Mike Copeland

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...
 
M

Mike Copeland

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?
 
V

Victor Bazarov

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
 
N

Nick Keighley

it does seem backwards...

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?

   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).
 
J

Jorgen Grahn

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
 
N

Nick Keighley

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top