S
Stefan Ram
For teaching purposes, I wrote this program as a first
example of a definition of a class with non-static functions:
#include <iostream>
#include <ostream>
class Account
{ private:
double balance_;
public:
Account( double const balance );
double balance(); };
::Account::Account( double const balance )
{ this->balance_ = balance; }
double ::Account::balance()
{ return this->balance_; }
int main()
{ Account acct( 100 );
::std::cout << acct.::Account::balance() << '\n';
::std::cout << acct.balance() << '\n'; }
(End of program.)
Regarding »acct.::Account::balance()«:
I was not aware that such a full qualification of a member
function in a member function call was possible at all.
When would one need this? (To call an overloaded function
of a base class?)
Can the above definition of the class still be improved somehow?
~~
Is such an account class is a good example for teaching?
I need the first example to be very simple, yet it needs
to suggest to be useful and related to applications of
the language.
Are there any other good examples of classes, that are
- small and simple
- not already part of the standard library
- not requiring special knowledge of a field
- looking somewhat natural and useful
?
Account classes are used often.
Sometimes, a class for complex numbers is used in teaching,
but such a class already is defined in the standard library
and might look too »mathematical«.
The only other topic that comes to my mind right now
would be a class for a compound date (YYYY, MM, DD)
or time (HH, MM) object.
example of a definition of a class with non-static functions:
#include <iostream>
#include <ostream>
class Account
{ private:
double balance_;
public:
Account( double const balance );
double balance(); };
::Account::Account( double const balance )
{ this->balance_ = balance; }
double ::Account::balance()
{ return this->balance_; }
int main()
{ Account acct( 100 );
::std::cout << acct.::Account::balance() << '\n';
::std::cout << acct.balance() << '\n'; }
(End of program.)
Regarding »acct.::Account::balance()«:
I was not aware that such a full qualification of a member
function in a member function call was possible at all.
When would one need this? (To call an overloaded function
of a base class?)
Can the above definition of the class still be improved somehow?
~~
Is such an account class is a good example for teaching?
I need the first example to be very simple, yet it needs
to suggest to be useful and related to applications of
the language.
Are there any other good examples of classes, that are
- small and simple
- not already part of the standard library
- not requiring special knowledge of a field
- looking somewhat natural and useful
?
Account classes are used often.
Sometimes, a class for complex numbers is used in teaching,
but such a class already is defined in the standard library
and might look too »mathematical«.
The only other topic that comes to my mind right now
would be a class for a compound date (YYYY, MM, DD)
or time (HH, MM) object.