You are on page 1of 13

Introduction to

Classes (cont.)
COS110: Chapter 13
Dr. Mard Helbig

Destructor
Destructor is member function that is automatically
called when a class object is destroyed
class Rectangle
{
private:
double width;
double length;
public:
Rectangle();
~Rectangle();
void setWidth(double);
void setLength(double);
double getWidth() const;
double getLength() const;
};

Destructor same name


as class and no return
value; ~ in front of name
In function definition class:
Rectangle::~Rectangle()
{

General form:
ClassName::~ClassName()

Destructor
Constructor is member function that is automatically
called when a class object is destroyed
class Rectangle
{
private:
double width;
double length;
public:
Rectangle();
~Rectangle();
void setWidth(double);
void setLength(double);
double getWidth() const;
double getLength() const;
};

Performs shut-down
procedures when the
object goes out of
existence
Commonly used to free
dynamically allocated
memory

Destructor
If a class object has been dynamically allocated with
the new operator, its memory should be released
when it is no longer needed
//define a ContactInfo pointer
ContactInfo *objectPtr = nullptr;
//dynamically create ContactInfo object
objectPtr = new ContactInfo(James Bond, 555-5007)

//destroy object
delete objectPtr;

Destructor of
class ContactInfo
is called

Overloading Constructors
If 2 functions have the same name => overloaded
Constructors can also be overloaded
Example
InventoryItem.h
InventoryItem.cpp
main.cpp

#Constructors/Destructors
A class can have only one default constructor. If
there are more than one, the compiler will not know
which one to call
Important: if you have a constructor where all
arguments have default values, you cannot define a
default constructor for your class
Destructor has no arguments and a class can have
only one destructor
There can be only 1!

Overloading Other
Member Functions
If 2 functions have the same name => overloaded
Can overload member functions to deal with
different types of arguments
void InventoryItem::setCost(double c)
{
cost = c;
}

Called with:
InventoryItem apples(12.0);

void InventoryItem::setCost(string c)
{
cost = atof(c.c_str());
}

Called with:
InventoryItem apples(12.0);

Private Member Functions


If a class has a function that should only be called for
certain circumstances from within the class
You dont want code from outside to access it
=> private member function

Example
ContactInfo.h
ContactInfo.cpp

Arrays of Objects
Can define an array of class objects
Example:
Defines an array of 40
const int ARRAY_SIZE = 40;
InventoryItem inventory[ARRAY_SIZE];

InventoryItem objects
Default constructor is
called for each object

What if default constructor is not


defined?

Need to specify the required arguments for each


object individually for each object in an initializer list
InventoryItem inventory[] = {Hammer, Wrench, Pliers};

Accessing Members of
Objects in Array
Example
main.cpp of InventoryItem

Unified Modeling
Language (UML)
Provides set of standard diagrams for graphically
depicting OO systems
Box divided into 3 sections:
o Top section: Name of class
o Middle section: List of classs member variables
o Bottom section: List of classs member functions

Unified Modeling
Language (UML)
Example:
Rectangle

-width: double
-length: double
+InventoryItem():
+InventoryItem(descr: string):
+setWidth(w: double): void
+setLength(len: double): void
+getWidth(): double
+getLength(): double

Name listed first, then


datatype
- private
+ public

Default Argument Values


(cont)
Order of default argument values in constructor
matter
Why?
If I want to change this, how can I do it?