Could someone explain some code?

Discussion in 'C++' started by Gactimus, Dec 9, 2004.

  1. Gactimus

    Gactimus Guest

    Can anyone explain what the lines with the '*' by them do?

    -----------

    #ifndef _COUNTER_H
    #define _COUNTER_H
    #include <iostream>
    using namespace std;

    class Counter
    {
    public:
    int count;
    public:
    * Counter(){count = 0;}
    void operator ++() { ++count; }
    int GetCount() { return count; }
    void SetCount( int c ) { count = c; }
    void PrintCount() { cout << "\n The count is " << count; }

    };

    class NewCounter:public Counter
    {
    public:
    * void operator --() { --count; }
    };

    #endif
    Gactimus, Dec 9, 2004
    #1
    1. Advertising

  2. Gactimus wrote:
    > Can anyone explain what the lines with the '*' by them do?
    >
    > -----------
    >
    > #ifndef _COUNTER_H
    > #define _COUNTER_H


    Lose the leading underscore on that macro name.

    > #include <iostream>
    > using namespace std;


    This is a very bad idea. Try to avoid ever putting 'using' directives
    in a header.

    >
    > class Counter
    > {
    > public:
    > int count;
    > public:
    > * Counter(){count = 0;}


    This one declares and defines the default constructor for this class.

    > void operator ++() { ++count; }
    > int GetCount() { return count; }
    > void SetCount( int c ) { count = c; }
    > void PrintCount() { cout << "\n The count is " << count; }
    >
    > };
    >
    > class NewCounter:public Counter
    > {
    > public:
    > * void operator --() { --count; }


    This one declares and defines a pre-decrement operator for 'NewCounter'

    > };
    >
    > #endif


    V
    Victor Bazarov, Dec 9, 2004
    #2
    1. Advertising

  3. Gactimus

    Gactimus Guest

    Victor Bazarov <> wrote in
    news:Gi_td.12050$09.us.to.verio.net:

    > Gactimus wrote:
    >
    >> Can anyone explain what the lines with the '*' by them do?
    >>
    >> -----------
    >>
    >> #ifndef _COUNTER_H
    >> #define _COUNTER_H

    >
    > Lose the leading underscore on that macro name.
    >
    >> #include <iostream>
    >> using namespace std;

    >
    > This is a very bad idea. Try to avoid ever putting 'using' directives
    > in a header.
    >
    >>
    >> class Counter
    >> {
    >> public:
    >> int count;
    >> public:
    >> * Counter(){count = 0;}

    >
    > This one declares and defines the default constructor for this class.
    >
    >> void operator ++() { ++count; }
    >> int GetCount() { return count; }
    >> void SetCount( int c ) { count = c; }
    >> void PrintCount() { cout << "\n The count is " << count;
    >> }
    >>
    >> };
    >>
    >> class NewCounter:public Counter
    >> {
    >> public:
    >> * void operator --() { --count; }

    >
    > This one declares and defines a pre-decrement operator for 'NewCounter'


    Thank you.
    Gactimus, Dec 9, 2004
    #3
  4. Gactimus

    Puppet_Sock Guest

    Gactimus wrote:
    [code help snipped]

    You should get yourself a copy of "Accelrated C++"
    by Koenig and Moo. Get yourself on the right path
    early in your C++ studies. After you've got through
    that, you should sample the books with good reviews
    at www.accu.org.
    Socks
    Puppet_Sock, Dec 9, 2004
    #4
  5. "Gactimus" <> schrieb im Newsbeitrag
    news:1102605878.0eb1675083e85648bb700b5e41f13861@bubbanews...
    > Can anyone explain what the lines with the '*' by them do?
    >
    > -----------
    >
    > #ifndef _COUNTER_H
    > #define _COUNTER_H
    > #include <iostream>
    > using namespace std;
    >
    > class Counter
    > {
    > public:
    > int count;
    > public:
    > * Counter(){count = 0;}



    That is the Constructor - look in a C++ manual - any C++ manual

    > void operator ++() { ++count; }
    > int GetCount() { return count; }
    > void SetCount( int c ) { count = c; }
    > void PrintCount() { cout << "\n The count is " << count; }
    >
    > };
    >
    > class NewCounter:public Counter
    > {
    > public:
    > * void operator --() { --count; }


    that is the -- operator for the class NewCounter:


    NewCounter cnt;
    --cnt; // This will call it

    Again, RTFM
    -Gernot
    Gernot Frisch, Dec 9, 2004
    #5
  6. Gactimus

    Gactimus Guest

    "Gernot Frisch" <> wrote in
    news::

    > "Gactimus" <> schrieb im Newsbeitrag
    > news:1102605878.0eb1675083e85648bb700b5e41f13861@bubbanews...
    >
    >> Can anyone explain what the lines with the '*' by them do?
    >>
    >> -----------
    >>
    >> #ifndef _COUNTER_H
    >> #define _COUNTER_H
    >> #include <iostream>
    >> using namespace std;
    >>
    >> class Counter
    >> {
    >> public:
    >> int count;
    >> public:
    >> * Counter(){count = 0;}

    >
    >
    > That is the Constructor - look in a C++ manual - any C++ manual
    >
    >> void operator ++() { ++count; }
    >> int GetCount() { return count; }
    >> void SetCount( int c ) { count = c; }
    >> void PrintCount() { cout << "\n The count is " << count; }
    >>
    >> };
    >>
    >> class NewCounter:public Counter
    >> {
    >> public:
    >> * void operator --() { --count; }

    >
    > that is the -- operator for the class NewCounter:
    >
    >
    > NewCounter cnt;
    > --cnt; // This will call it


    Ok in these lines:

    void operator --() { --count; }
    Counter(){count = 0;}
    void operator ++() { ++count; }
    int GetCount() { return count; }
    void SetCount( int c ) { count = c; }
    void PrintCount() { cout << "\n The count is " << count; }

    What is the stuff in the {} brackets? What is it called? The book I have is
    kind of crappy and I can't find where it discusses this.
    Gactimus, Dec 9, 2004
    #6
  7. Gactimus

    Gactimus Guest

    Victor Bazarov <> wrote in
    news:3r2ud.12062$09.us.to.verio.net:

    > Gactimus wrote:
    >
    >> Ok in these lines:
    >>
    >> void operator --() { --count; }
    >> Counter(){count = 0;}
    >> void operator ++() { ++count; }
    >> int GetCount() { return count; }
    >> void SetCount( int c ) { count = c; }
    >> void PrintCount() { cout << "\n The count is " << count; }
    >>
    >> What is the stuff in the {} brackets? What is it called? The book I
    >> have is kind of crappy and I can't find where it discusses this.

    >
    > It's called "the body of the function".


    So is it just a more efficient way of writing a function instead of
    writing a prototype and a seperate body?
    Gactimus, Dec 9, 2004
    #7
  8. Gactimus

    Gactimus Guest

    Gactimus <> wrote in
    news:1102623238.bb85af6c6bc2493985259870e212990a@bubbanews:

    > Victor Bazarov <> wrote in
    > news:3r2ud.12062$09.us.to.verio.net:
    >
    >> Gactimus wrote:
    >>
    >>> Ok in these lines:
    >>>
    >>> void operator --() { --count; }
    >>> Counter(){count = 0;}
    >>> void operator ++() { ++count; }
    >>> int GetCount() { return count; }
    >>> void SetCount( int c ) { count = c; }
    >>> void PrintCount() { cout << "\n The count is " << count; }
    >>>
    >>> What is the stuff in the {} brackets? What is it called? The book I
    >>> have is kind of crappy and I can't find where it discusses this.

    >>
    >> It's called "the body of the function".

    >
    > So is it just a more efficient way of writing a function instead of
    > writing a prototype and a seperate body?


    Actually, I think I see now.
    Gactimus, Dec 9, 2004
    #8
  9. Gactimus wrote:
    > Ok in these lines:
    >
    > void operator --() { --count; }
    > Counter(){count = 0;}
    > void operator ++() { ++count; }
    > int GetCount() { return count; }
    > void SetCount( int c ) { count = c; }
    > void PrintCount() { cout << "\n The count is " << count; }
    >
    > What is the stuff in the {} brackets? What is it called? The book I have is
    > kind of crappy and I can't find where it discusses this.


    It's called "the body of the function".
    Victor Bazarov, Dec 9, 2004
    #9
  10. Gactimus

    Frank Looper Guest

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

    >
    > This is a very bad idea. Try to avoid ever putting 'using' directives
    > in a header.
    >

    Isn't that the standard? It's at least being taught as such...

    Frank
    Frank Looper, Dec 9, 2004
    #10
  11. Frank Looper wrote:
    >>>#include <iostream>
    >>>using namespace std;

    >>
    >>This is a very bad idea. Try to avoid ever putting 'using' directives
    >>in a header.
    >>

    >
    > Isn't that the standard? It's at least being taught as such...


    Taught by whom and where?
    Victor Bazarov, Dec 9, 2004
    #11
  12. Gactimus wrote:
    > Gactimus <> wrote in
    > news:1102623238.bb85af6c6bc2493985259870e212990a@bubbanews:
    >
    >
    >>Victor Bazarov <> wrote in
    >>news:3r2ud.12062$09.us.to.verio.net:
    >>
    >>
    >>>Gactimus wrote:
    >>>
    >>>
    >>>>Ok in these lines:
    >>>>
    >>>>void operator --() { --count; }
    >>>>Counter(){count = 0;}
    >>>>void operator ++() { ++count; }
    >>>>int GetCount() { return count; }
    >>>>void SetCount( int c ) { count = c; }
    >>>>void PrintCount() { cout << "\n The count is " << count; }
    >>>>
    >>>>What is the stuff in the {} brackets? What is it called? The book I
    >>>>have is kind of crappy and I can't find where it discusses this.
    >>>
    >>>It's called "the body of the function".

    >>
    >>So is it just a more efficient way of writing a function instead of
    >>writing a prototype and a seperate body?

    >
    >
    > Actually, I think I see now.


    I am not sure I'd label it "more efficient". And, of course, I am not
    sure I understand what it is you see, but that doesn't matter.

    V
    Victor Bazarov, Dec 9, 2004
    #12
  13. Frank Looper wrote:
    >>>#include <iostream>
    >>>using namespace std;

    >>
    >>This is a very bad idea. Try to avoid ever putting 'using' directives
    >>in a header.
    >>

    >
    > Isn't that the standard?


    The standard what?

    > It's at least being taught as such...


    Change your teacher/book.

    Unfortunately, I don't see the post you are a answering to, so I am
    assuming there was a using declaration in a header.

    Putting a using declaration in a header allows for side-effects. For
    example, if I include <a_math_library.h>, I assume I'll get some
    math-related functions, not the whole namespace std (or whatever
    namespace for that matter). Just look :

    # include <list>

    // the name 'list' is not recommended, but shit happens
    //
    class list
    {
    // ...
    };

    int main()
    {
    list my_list;
    std::list a_std_list;
    }

    Now that works. But now we got that new and cool math library and we
    include it

    # include <list>
    # include <a_math_library.h>

    // same thing..

    but that header begins with

    # include <cmath>
    using namespace std;

    That means our code will break beacuse we included an unrelated header.
    That's an unpleasant side effect.

    What's more, the usual warning about using declarations also applies:
    they defeat the very purpose of namespaces, which is to avoid name
    clashes. Using directives are better, using directives at function
    scope in implementation file are the best.

    # include <iostream>

    int main()
    {
    using std::cout;

    cout << "Here we are.";
    }


    Jonathan
    Jonathan Mcdougall, Dec 9, 2004
    #13
  14. Gactimus wrote:

    > Can anyone explain what the lines with the '*' by them do?
    >
    > -----------
    >
    > #ifndef GUARD_COUNTER_H
    > #define GUARD_COUNTER_H 1
    > #include <iostream>
    > // using namespace std;
    >
    > class Counter {
    > private:
    > int count;
    > public:
    > Counter(void): count(0) { } // default constructor
    > Counter& operator++(void) { ++count; return *this; }
    > int GetCount(void) { return count; }
    > Counter& SetCount(int c) { count = c;

    return *this; }
    friend
    std::eek:stream& operator<<(std::eek:stream& os, const Counter& c) {
    return os << c.count;
    }
    > // void PrintCount() { cout << "\n The count is " << count; }
    >
    > };
    >
    > class NewCounter: public Counter {
    > public:
    > NewCounter& operator--(void) { --count; return *this; }
    > };
    >
    > #endif//GUARD_COUNTER_H
    E. Robert Tisdale, Dec 9, 2004
    #14
  15. Jonathan Mcdougall wrote:
    > [...]
    > What's more, the usual warning about using declarations also applies:
    > they defeat the very purpose of namespaces, which is to avoid name
    > clashes. Using directives are better, using directives at function
    > scope in implementation file are the best.
    >
    > # include <iostream>
    >
    > int main()
    > {
    > using std::cout;
    >
    > cout << "Here we are.";
    > }


    It's vice versa.

    using namespace std;

    is a using _directive_

    using std::cout;

    is a using _declaration_. So, using _declarations_ are better.

    Just so we're clear on that...

    V
    Victor Bazarov, Dec 9, 2004
    #15
  16. Gactimus

    Default User Guest

    Frank Looper wrote:

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

    > >
    > > This is a very bad idea. Try to avoid ever putting 'using'
    > > directives in a header.
    > >

    > Isn't that the standard? It's at least being taught as such...



    No, because then everyone who includes your header gets all of
    namespace std thrust into the global namespace, which might not be what
    they want.

    Such using declarations should only be in implementation files. Header
    should just explicitly scope items from std.




    Brian
    Default User, Dec 9, 2004
    #16
  17. Gactimus

    Gactimus Guest

    Gactimus <> wrote in
    news:1102605878.0eb1675083e85648bb700b5e41f13861@bubbanews:

    > Can anyone explain what the lines with the '*' by them do?
    >
    > -----------
    >
    > #ifndef _COUNTER_H
    > #define _COUNTER_H
    > #include <iostream>
    > using namespace std;
    >
    > class Counter
    > {
    > public:
    > int count;






    > public:
    > * Counter(){count = 0;}
    > void operator ++() { ++count; }
    > int GetCount() { return count; }
    > void SetCount( int c ) { count = c; }
    > void PrintCount() { cout << "\n The count is " << count; }


    If this section was protected, how would one access it using a friend?


    > };
    >
    > class NewCounter:public Counter
    > {
    > public:
    > * void operator --() { --count; }
    > };
    >
    > #endif
    Gactimus, Dec 9, 2004
    #17
  18. Gactimus

    David White Guest

    "Gactimus" <> wrote in message
    news:1102623238.bb85af6c6bc2493985259870e212990a@bubbanews...
    > Victor Bazarov <> wrote in
    > news:3r2ud.12062$09.us.to.verio.net:
    > > It's called "the body of the function".

    >
    > So is it just a more efficient way of writing a function instead of
    > writing a prototype and a seperate body?


    More efficient to write anyway, and perhaps to read, though maybe not if
    there's too much of it. Also, a function whose body is in the class
    definition is also inline, i.e., the same as putting the body outside, but
    with the 'inline' specification.

    DW
    David White, Dec 9, 2004
    #18
  19. In article <>, Default User
    <> writes
    >Frank Looper wrote:
    >
    >> > > #include <iostream>
    >> > > using namespace std;
    >> >
    >> > This is a very bad idea. Try to avoid ever putting 'using'
    >> > directives in a header.
    >> >

    >> Isn't that the standard? It's at least being taught as such...

    >
    >
    >No, because then everyone who includes your header gets all of
    >namespace std thrust into the global namespace, which might not be what
    >they want.


    But, unless I have completely lost track, we were not discussing a
    header (I have never come across a header with main() in it.

    >
    >Such using declarations should only be in implementation files. Header
    >should just explicitly scope items from std.


    Much better is to actually understand what using declarations and using
    directives do.

    --
    Francis Glassborow ACCU
    Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
    For project ideas and contributions: http://www.spellen.org/youcandoit/projects
    Francis Glassborow, Dec 10, 2004
    #19
  20. Victor Bazarov wrote:
    > using namespace std;
    >
    > is a using _directive_
    >
    > using std::cout;
    >
    > is a using _declaration_. So, using _declarations_ are better.
    >
    > Just so we're clear on that...


    Whoaa, I've been saying that for years and I never noticed. Sorry for
    the troubles.


    Jonathan
    Jonathan Mcdougall, Dec 10, 2004
    #20
    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. Sean Berry
    Replies:
    3
    Views:
    292
    Tor Iver Wilhelmsen
    Apr 28, 2004
  2. Replies:
    5
    Views:
    287
    Bo Persson
    Jan 10, 2006
  3. Alexander Mahone

    Could someone please explain me this error?

    Alexander Mahone, May 2, 2008, in forum: C Programming
    Replies:
    5
    Views:
    269
    Igmar Palsenberg
    May 5, 2008
  4. wkevin
    Replies:
    4
    Views:
    576
    Edward A. Falk
    Nov 7, 2011
  5. John Small
    Replies:
    4
    Views:
    104
    John Small
    Feb 2, 2009
Loading...

Share This Page