lecture3_introductiontoclasses

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

Upload: zelacanbery

Post on 24-Jan-2016

212 views

Category:

Documents


0 download

DESCRIPTION

boo

TRANSCRIPT

Page 1: Lecture3_IntroductionToClasses

Introduction to Classes (cont.)

COS110: Chapter 13

Dr. Mardé Helbig

Page 2: Lecture3_IntroductionToClasses

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()

Page 3: Lecture3_IntroductionToClasses

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

Page 4: Lecture3_IntroductionToClasses

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

Page 5: Lecture3_IntroductionToClasses

Overloading Constructors • If 2 functions have the same name => overloaded

• Constructors can also be overloaded

Example InventoryItem.h InventoryItem.cpp main.cpp

Page 6: Lecture3_IntroductionToClasses

#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!

Page 7: Lecture3_IntroductionToClasses

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”);

Page 8: Lecture3_IntroductionToClasses

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

Page 9: Lecture3_IntroductionToClasses

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”};

Page 10: Lecture3_IntroductionToClasses

Accessing Members of Objects in Array

Example main.cpp of InventoryItem

Page 11: Lecture3_IntroductionToClasses

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

Page 12: Lecture3_IntroductionToClasses

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

Page 13: Lecture3_IntroductionToClasses

Default Argument Values (cont)

• Order of default argument values in constructor

matter

• Why?

• If I want to change this, how can I do it?