|
|
|
For example, the
programmer could model a car object this way:
|
|
class car {
|
|
engine motor;
|
|
public: //please
ignore for the moment
|
|
void on();
|
|
void off();
|
|
};
|
|
This means that a car object is something
that has an engine, represented by the motor data member, and for which two operations are permitted: on() (which presumably
turns the engine on) and off() (which turns the engine off).
|
|
After having given
these lines, the user could create more instances of the car class, with statements
like:
|
|
car
renault_megane, fiat_punto,opel_astra;
|
|
and operate on them
with the allowed member functions:
|
|
renault_megane.on();
|
|
opel_astra.off();
|
|
Please be aware
that, while every instance of the car class shown permits the same operations on it (namely, on() and off()) they have each one
their own copy of the data members, i.e. the renault_megane and the fiat_punto objects for
example have their own motor inside!
|