lecture3_introductiontoclasses
DESCRIPTION
booTRANSCRIPT
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
In function definition class: Rectangle::~Rectangle() { }
Destructor – same name as class and no return value; ~ in front of name
class Rectangle
{
private:
double width;
double length;
public:
Rectangle();
~Rectangle();
void setWidth(double);
void setLength(double);
double getWidth() const;
double getLength() const;
};
General form: ClassName::~ClassName()
Destructor • Constructor is member function that is automatically
called when a class object is destroyed
Performs shut-down procedures when the object goes out of existence
class Rectangle
{
private:
double width;
double length;
public:
Rectangle();
~Rectangle();
void setWidth(double);
void setLength(double);
double getWidth() const;
double getLength() const;
};
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;
}
void InventoryItem::setCost(string c)
{
cost = atof(c.c_str());
}
Called with: InventoryItem apples(12.0);
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 don’t 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:
const int ARRAY_SIZE = 40;
InventoryItem inventory[ARRAY_SIZE];
Defines an array of 40 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 class’s member variables
o Bottom section: List of class’s 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?