154 Introduction to C++ Programming and Graphics
Default constructor
Our first public definition is the default constructor. The fruit class
definition reads:
class fruit
{
public:
fruit ();
...
private:
...
};
Note that the default constructor does not have a return type, not evenvoid.
The name of the default constructor is identical to the class name.
To define a fruit named “kiwi” using the default constructor, we state in
the main program:
fruit kiwi;
kiwi = fruit();
or
fruit kiwi = fruit();
or
fruit kiwi;
It is erroneous to declare:
fruit kiwi();
as the compiler interprets this statement as the prototype of a function named
kiwithat receives no arguments and returns a fruit.
Parametered constructor
Including also the parametered constructor, we obtain the class declara-
tion:
class fruit
{
public:
fruit();