Re: Newbie and seventy-two-bie, please help.
From: Jonathan Mcdougall (jonathanmcdougall_at_DELyahoo.ca)
Date: 08/15/04
- Next message: Edo: "Re: interdependency and #include"
- Previous message: Rolf Magnus: "Re: Nested class"
- In reply to: Harry Scott: "Newbie and seventy-two-bie, please help."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 15 Aug 2004 15:20:12 -0400
Since you probably understand things a little better now, I will go for some
more comments.
> #include <iostream>
This is a standard header and everything in these headers are contained in
the namespace std. Therefore, you must either fully qualify the names you
are using or bring the names into the global namespace.
1) Qualify the name :
std::cout << "Hello";
2a) Bring the name in the global namespace
using std::cout;
cout << "Hello";
2b) Bring all the names in the global namespace
using namespace std;
cout << "Hello";
The last alternative may seem to be interesting, in the sense that you don't
have to bother about qualifying anything now, but believe me, it is the
wrong way. Namespaces were created to avoid name clashes and to put names
into a logical group. Dumping all these names with a using declaration is a
bad idea since it defeats the use of namespaces. Alternative 2a) is ok and
1) is the one I personnaly use in most cases.
> class CPool {
Prefixing class names with C is considered bad style :) No, seriously, this
is hungarian notation and you should think twice (or more) before using it.
IMHO,
class Pool
{
is much better. But this is a personal opinion.
> float *num, *amount;
Prefer putting each declaration on its own line and adding a comment for
each. Since your code is not clear on what it does, I cannot do that for
yourself. What's more, double is considered the 'default' floating point
type in C++ because the language says that the litteral 13.23 for example is
a double, not a float.
double num; // what is that
double amount; // what is that
> public:
> CPool (float, float);
Take the habit of specifying the parameter names in the declarations. It is
of no use for the compiler, but very useful for other programmers.
CPool(double a, double b);
Well... when you use better variable names.
> ~CPool ();
> float dividend (void) {return (*num * *amount);}
Do you understand all the implications of defining a member function _in_
the class? That function is automatically considered inlined. Read about
inlining functions.
> };
So your class becomes
// in pool.h
// this is my class which does something
//
class Pool
{
private:
double num; // what is this
double amount; // what is this
public:
// this does that
//
CPool(float a, float b);
// this does that
//
double dividend();
};
This is a class definition. Note that its member functions are only
declared, not defined. This is normal, since the the other files including
that one do not need to see the details of these functions. They only need
to know that dividend() does that, as specified in the comment. So this
definition goes into a header file called for example pool.h and rest goes
in pool.cpp :
// pool.cpp
CPool::CPool(float a, float b)
: num(a),
amount(b)
{
}
double CPool::dividend()
{
return num * amount;
}
The weird part between the contructor's signature and its body is called an
initialization list. Read about it, this is very important and it is better
to initialize objects there than in the contructor's body.
>
> float main () {
main() always returns an int. The two possible signatures are
int main()
and
int main(int argc, char **argv)
> CPool part (3.0,20.71, partb (41.0,13.33);
> cout << "parta dividend: " << part.dividend() << endl;
> cout << "partb dividend: " << partb.dividend ():<< endl;
> }
Since you have no qualified the standard names nor brought the names into
the global one, this should not have compiled.
# include <iostream>
# include "pool.h"
int main()
{
Pool parta(3.0, 20.71);
Pool partb(41.0, 13.33);
std::cout << "parta dividend : " << parta.dividend() << std::endl;
std::cout << "partb dividend : " << partb.dividend() << std::endl;
}
Have fun!
Jonathan
- Next message: Edo: "Re: interdependency and #include"
- Previous message: Rolf Magnus: "Re: Nested class"
- In reply to: Harry Scott: "Newbie and seventy-two-bie, please help."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|