this
Pointer
class className { private: // data member declarations public: // member function prototypes };
private
in class
declarations, for that is the default access control for class
objects:
class Planet { float mass; // private by default char name[20]; // private by default public: void rotation(); };
::
)
to indicate to which class a member function belongs.
::
).
Planet
class has a member function called
getRulerName()
that returns a pointer to a char
.
Then the function heading (declared outside the class declaration)
would look like this:
char * Planet::getRulerName()In other words,
getRulerName()
is not just a type
char *
function; it is a type char *
function that belongs to the Planet
class.
Planet::getRulerName()
.
getRulerName()
, on the other hand, is an abbreviation
of the qualified name, and it can be used only in certain circumstances, such as in class
method code.
getRulerName()
has class scope, so the scope resolution operator is needed to qualify
the name when it is used outside the class declaration and a class method.
Planet naboo;
cout << naboo.getRulerName();
public
, whereas the default type for the class is
private
.
void
.
Planet
class has the following prototype for
a class constructor:
Planet(char * pName, char * rName); // constructor prototypeThen you would use it to initialize new objects as follows:
Planet naboo = Planet("Naboo", "Amidala"); // primary form Planet mars("Mars", "Martian"); // short form Planet *earth = new Planet("Earth", "Bush"); // dynamic object
Planet(double age);Then, you can use any of the following forms to initialize an object:
Planet naboo = bozo(10000); // primary form Planet mars(50000); // short form Planet earth = 99999; // special form for one-argument constructors
Planet naboo; // uses the default constructor
Planet(char * pName = "Naboo", char * rulerName = "Amidala");
Planet()
new
to allocate memory, the destructor should use delete
to
free that memory.
~
).
For example, the destructor of a class Planet would be:
~Planet();
this
Pointer
this
pointer.
this
pointer is that it points to the invoking
object.
*this
.
const
qualifier after the function argument parentheses,
you can't use this
to change the object's value.