473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1584
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: C++fan | last post by:
The following code is for list operation. But I can not understand. Could anyone explain the code for me? /* * List definitions. */ #define LIST_HEAD(name, type) struct name { type...
3
by: MarcJessome | last post by:
Hi, I was wondering if someone could help me through learning C++. I've tried learning before, but I find I would work better if I had someone that could help explain a few things to me. Im using...
5
by: J Allen Horner | last post by:
I'm just starting to learn C, and the tutorial I'm reading uses this code as an example: ----------- #include <stdio.h> #define MAX 10 int a; int rand_seed=10;
4
by: konsu | last post by:
hello, my client code communicates with a web service through a https connection. the code has a stub derived from SoapHttpClientProtocol. It also sets a certificate policy handler that allows...
12
by: you | last post by:
I may be just stupid, but I don't understand the point of using an Interface. I am going through a couple of books at the moment trying to teach myself a little vb.net. Just for the heck of...
8
by: Bart | last post by:
Could someone explain me what is wrong with this code ? I gives me a compile error: Error 1 Use of unassigned local variable 'fileStreamObject' C:\Documents and Settings\Bart\Local...
2
by: patrice.pare | last post by:
Hello, Here is a summary of my Dev Environment: I use Visual Studio 2005 Team Suite SP1 with Crystal Report XI SP1 on a Windows XP SP2 development workstation. I also use SQL Server 2000 SP4. ...
12
by: Tom | last post by:
First, my thanks in advance to those who can help explain away this mystery line of code. Petzold is a great teacher and I appreciate his work; however, his explanation about an extra line of...
6
by: Dave Young | last post by:
I'm looking at some code that i've inherited and I'm not really familar with what's going on here and was hoping somone could explain it to me. For reference: f1 is a long f2 is a long ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.