Using Class Objects

Discussion in 'C++' started by Mike Copeland, Jul 31, 2012.

  1. 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
    Mike Copeland, Jul 31, 2012
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jul 31, 2012
    #2
    1. Advertising

  3. Mike Copeland

    Jorgen Grahn Guest

    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 .
    Jorgen Grahn, Jul 31, 2012
    #3
  4. In article <jv9hnf$h38$>, d
    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, Jul 31, 2012
    #4
  5. In article <>,
    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?
    Mike Copeland, Jul 31, 2012
    #5
  6. On 7/31/2012 6:29 PM, Mike Copeland wrote:
    > In article <jv9hnf$h38$>, d
    > 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
    Victor Bazarov, Aug 1, 2012
    #6
  7. On Jul 31, 11:33 pm, (Mike Copeland) wrote:
    > In article <>,
    > 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).
    Nick Keighley, Aug 1, 2012
    #7
  8. Mike Copeland

    Jorgen Grahn Guest

    On Tue, 2012-07-31, Mike Copeland wrote:
    > In article <>,
    > 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 .
    Jorgen Grahn, Aug 1, 2012
    #8
  9. On Aug 1, 2:19 pm, Jorgen Grahn <> wrote:
    > On Tue, 2012-07-31, Mike Copeland wrote:
    > > In article <>,
    > > 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
    Nick Keighley, Aug 1, 2012
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,684
    Thomas Weidenfeller
    Oct 12, 2005
  2. jd
    Replies:
    3
    Views:
    333
  3. =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=

    form objects into class objects

    =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=, Jul 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    421
    =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=
    Jul 6, 2006
  4. 7stud
    Replies:
    11
    Views:
    661
    Dennis Lee Bieber
    Mar 20, 2007
  5. ghoetker
    Replies:
    1
    Views:
    358
Loading...

Share This Page