C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf ·...

35
C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Transcript of C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf ·...

Page 1: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

C++: Const

Function OverloadingConstructors and Destructors

EnumerationsAssertions

Page 2: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Const

� const float pi=3.14159;� const int* pHeight; // defines pointer to

// constant int – value cannot be changed� // pointer can be changed!!� const int* pHeight const = &ht; // both

// value and pointer cannot be changed� const float& PI = pi; // ref to a constant

Page 3: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Const in functions

� class house {� public:

� float Area () const; // method does not change object� void ConstructionInfo (const string builder, const Date& d);

// arguments cannot be modified

� … }� Compiler will enforce restrictions� Propagation of const: const method cannot invoke

non-const methods� Cannot pass const objects received unless receiver

promises not to modify them

Page 4: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Function prototypes

� Every function call must have a prototype that the compiler has seen earlier

� bool strcmp (const char*, const char*);

� This includes system library functions� Man page for system calls includes

header file to be included to get prototype� Including class header file provides

prototype for all its methods

Page 5: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Function overloading

� Class may include several methods with same name, as long as parameter types are different

� class Shape { public:� void Draw (); // default draw� void Draw (Screen*); // draw on screen� void Draw (int x, int y); // draw at position� void Draw (bool reverseColours); … }

� Not enough to have different return types� Call argument types determine which method

gets called (note possibility of type conversions)

Page 6: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Constructor

� A constructor for a class is called when an object of that class is created:� Local / Stack Based

� Foo F (3, 4, “Joe”);

� On Free Store� Foo *Fptr (new Foo ((3, 4, “Joe”));

� Anonymously� goo.bar (Foo (3, 4, “Joe”), moo, poo);

Page 7: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Constructor

� Constructors have the same name as the class.

� Constructors do not return a value.� The constructor, like all functions, can be

overloaded with each constructor having a different set of parameters.

Page 8: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Constructor

class Date {

private:

int d, m, y;

public:

Date (int day, int month, int year);

Date (int day, int month);

Date (int day);

Date (); // today’s date

Date (string date);

}

Page 9: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Constructor

Date::Date (int day, int month, int year) :

d (day), m (month), y (year) {}

The : syntax provides initializers for use during object construction

Body of constructor executed after construction

Page 10: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Default Constructor

� Constructor with no arguments is a default constructor

� Some compilers may supply one automatically

� May get invoked to create temporaries� More often, temporaries will be created using

copy constructor (see later)

Page 11: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Constructor

� Default Constructor� Constructor with no arguments.

� Foo F;

� If a class defines constructors but no default constructor, compiler will generate an error.

� If no constructors are defined for a class, the compiler will generate a default constructor.

Page 12: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Constructor

� Copy Constructor� Initializes an object based on the contents of another

object of the same type.Date (const Date &D) :

d (D.d), m (D.m), y (D.y) {}

� Object has access to non-public members of objects of the same class

Page 13: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Constructor

� Copy Constructor� Called when:

� A declaration is made with initialization from another object

� Date d1 (d2);

� Parameters are passed by value.� An object is returned by a function.

Page 14: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Constructor

� Copy vs. Assignment� operator= is called when an assignment is made…

� Date d1 (10, 2, 2002);

� Date d2;

� d2 = d1; // operator= called

� However, � If an assignment is made during a variable declaration, then

the copy constructor rather than the assignment operator is called

� Date d1 = d2; // Copy NOT assignment!

Page 15: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Constructor

� Copy Constructor� If no copy constructor is defined for a class,

the default copy constructor is used.� Member by member copy of data from one object

to another.� Can be troublesome if class have pointers as data

members.

� Same issues as with the default assignment operator!!!!

Page 16: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Automatic Type Conversion

� C++ will use constructors to perform automatic type conversion� void foo (Date d);� Accidentally call foo(5);

� Since Date has a constructor� Date (int day);

� C++ will call this method to create a Date from 5, and pass it to foo()

� Causes nasty bugs!

Page 17: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Constructor Summary

Date d1(3, 10, 2002); // constructor called

Date d2, d5; // default constructor called

Date d3 (d2); // copy constructor called

Date d4 = d1; // copy constructor called

d5 = d2; // assignment operator called.

Questions?

Page 18: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Constructor

� Important safety tips:� If your constructors perform any non-trivial

work (e.g. memory allocation), should define the full suite of:� Constructors� Copy constructor� operator=

Page 19: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Destructor

� A destructor for a class is called when the memory of an object of that class is reclaimed:� A global (static) object is reclaimed when the program

terminates.� A local (automatic) object is reclaimed when the

function terminates (stack is popped). � A dynamically allocated object is reclaimed when

someone invokes the delete operator on it.

� Like Java finalize

Page 20: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Destructor

void aFunction (Foo f)

{

Foo f2;

Foo *fooptr = new Foo();

...

delete fooptr; // destructor called

}

// after function is complete, destructor called on f and f2

Page 21: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Destructor

� Destructors have the same name as the class but preceded with a ~.

� Destructors do not return a value.� Destructors take no arguments and cannot be

overloaded.� Destructors are used for cleaning up object data

/ state� Allocated memory� Close files� Close network connections, etc.

Page 22: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Destructors

class Foo

{

private:

int *array_member;

int asize;

...

public:

Foo (int size);

~Foo ();

}

Page 23: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Destructors

Foo::Foo (int size) :

asize (size), array_member (new int[size])

{

for (int i=0; i<size; i++)

array_member[i] = 0;

}

Foo::~Foo ()

{

// cleanup what was allocated by

// constructor

if (array_member != 0) delete array_member;

}

Page 24: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Destructors

� Questions?

Page 25: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Enumerated Types

� An enumeration is a type that can hold a set of values defined by the user.

� Once defined, they can be used like an integer type.

� enum statement assigns sequential integer values to names and provide a type name for declaration.

Page 26: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Enumerated Types

enum TrafficLightColor {RED, YELLOW,GREEN};

TrafficLightColor x;

x = YELLOW;

Page 27: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Enumerated Types

� It's possible to control the values that are assigned to each enum constant.� enum Day {MON=1, TUE, WED, THU, FRI, SAT, SUN};

� WED == 3

� enum DayFlag {MON=1, TUE=2, WED=4, THU=8, FRI=16, SAT=32, SUN=64};

Page 28: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Enumerated Types

� Enums are types, not just integers

enum TrafficLightColor {RED, YELLOW, GREEN};

enum Gender {MALE, FEMALE};

TrafficLightColor x;

. . .

x = YELLOW; // good

x = FEMALE; // error

x = 2; // error

Page 29: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Enumerated Types

� Enums can be placed in the scope of a class:class Calendar

{

public:

enum Day {MON=1, TUE, WED, THU, FRI, SAT, SUN};

...

}

Calendar::Day x;

x = Calendar::SAT;

Page 30: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Enumerated Types

� Questions?

Page 31: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Assertions

� Debugging mechanism to check condition at a given point in the code� If condition is false, then program will abort

with an error message� Very useful for detecting coding errors and

wrong assumptions

Page 32: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Assertions

#include <cassert>

void f (int *p)

{

// At this point p should be non-null

assert (p!=0);

}

Page 33: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Assertions

� Used for debugging� Can be turned off� Removing does not affect how the program

works

� CC –DNDEBUG foo.C

Page 34: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Assertions

� When to use� Test preconditions� Test postconditions� “At this point x should be equal to …”� “We should never reach this line”

� Zero cost, adds to readability (documents assumptions)� Great coding practice!

Page 35: C++: Const Function Overloading Constructors and Destructors …sxn/cs4/constructors.pdf · 2002-09-26 · Created by Joe Geigel, modified by Swami Function prototypes — Every function

Created by Joe Geigel, modified by Swami

Summary

� Constructors� Copy Constructor� Default Constructor

� Destructor� Enum� Assertion

� Questions?