468,527 Members | 2,091 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,527 developers. It's quick & easy.

Could someone explain some code?

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
Jul 22 '05 #1
21 1363
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
Jul 22 '05 #2
Victor Bazarov <v.********@comAcast.net> wrote in
news:Gi*****************@newsread1.dllstx09.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.
Jul 22 '05 #3
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

Jul 22 '05 #4

"Gactimus" <ga******@xrs.net> schrieb im Newsbeitrag
news:1102605878.0eb1675083e85648bb700b5e41f13861@b ubbanews...
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
Jul 22 '05 #5
"Gernot Frisch" <Me@Privacy.net> wrote in
news:31*************@individual.net:
"Gactimus" <ga******@xrs.net> schrieb im Newsbeitrag
news:1102605878.0eb1675083e85648bb700b5e41f13861@b ubbanews...
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.
Jul 22 '05 #6
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".
Jul 22 '05 #7
Victor Bazarov <v.********@comAcast.net> wrote in
news:3r*****************@newsread1.dllstx09.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?
Jul 22 '05 #8
Gactimus <ga******@xrs.net> wrote in
news:1102623238.bb85af6c6bc2493985259870e212990a@b ubbanews:
Victor Bazarov <v.********@comAcast.net> wrote in
news:3r*****************@newsread1.dllstx09.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.
Jul 22 '05 #9
> > #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
Jul 22 '05 #10
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?
Jul 22 '05 #11
Gactimus wrote:
Gactimus <ga******@xrs.net> wrote in
news:1102623238.bb85af6c6bc2493985259870e212990a@b ubbanews:

Victor Bazarov <v.********@comAcast.net> wrote in
news:3r*****************@newsread1.dllstx09.us.t o.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
Jul 22 '05 #12
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
Jul 22 '05 #13
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
Jul 22 '05 #14
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
Jul 22 '05 #15
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::ostream& operator<<(std::ostream& 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

Jul 22 '05 #16
Gactimus <ga******@xrs.net> wrote in
news:1102605878.0eb1675083e85648bb700b5e41f13861@b ubbanews:
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


Jul 22 '05 #17
"Gactimus" <ga******@xrs.net> wrote in message
news:1102623238.bb85af6c6bc2493985259870e212990a@b ubbanews...
Victor Bazarov <v.********@comAcast.net> wrote in
news:3r*****************@newsread1.dllstx09.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

Jul 22 '05 #18
In article <I8*******@news.boeing.com>, Default User
<fi********@boeing.com.invalid> 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
Jul 22 '05 #19
Francis Glassborow wrote:
In article <I8*******@news.boeing.com>, Default User
<fi********@boeing.com.invalid> 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.


You have completely lost track, then. See the post that started the
thread. The two lines quoted _were_ in a header. And I don't see any
'main' in it either.

V
Jul 22 '05 #20
Francis Glassborow wrote:
In article <I8*******@news.boeing.com>, Default User
<fi********@boeing.com.invalid> 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.


Nope. Here's the original code under discussion:

-----------

#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


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.


Indeed. Feel up to posting a tutorial? I didn't. The people in question
should find a good book that explains the subject. Now, if we only knew
of some web site or something that reviewed books ;)


Brian
Jul 22 '05 #21

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Gi*****************@newsread1.dllstx09.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.


I thought the * was actually part of the code, and I could not figure out what
it was supposed to do :-)
Jul 22 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by C++fan | last post: by
3 posts views Thread by MarcJessome | last post: by
5 posts views Thread by J Allen Horner | last post: by
6 posts views Thread by Dave Young | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.