Programming and Graphics

(Kiana) #1

6.3 Class definition 153


Destructors


Destructors are member functions that delete an object for revenge or to
free up memory and prevent memory leaks.


Transient objects are generated when we call a function to perform certain
operations, and then abandoned when we exit the function. Destructors allow
us to abandon the objects before exiting a function.


Problems


6.2.1.A function takes a bite off an apple declared as a member of the fruit
class. It this a mutator member function?


6.2.2.Define an accessor and a mutator member function operating on the
data type (class) of all integers.


6.3 Class definition


The “fruit” class definition has the general appearance:


class fruit
{
...
};

Here and elsewhere, the dots represent additional lines of code. Note the semi-
colon at the end of the class definition.


Member attributes are declared aspublicif they are disclosed to the main
program and functions of a different class, andprivateotherwise. Similarly,
interface functions are declared as public if they can be called from the main
program and from functions of a different class, and private otherwise. This
distinction motivates the class-definition structure:


class fruit
{
public:
...
private:
...
};
Free download pdf