lecture 3: constructors and destructors

14
Bushra Riaz

Upload: analu

Post on 20-Jan-2016

51 views

Category:

Documents


1 download

DESCRIPTION

Lecture 3: Constructors and Destructors. Bushra Riaz. Initializing Objects with Constructors. A constructor can be used to automatically initialize an object of the class when the object is created. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 3: Constructors and Destructors

Bushra Riaz

Page 2: Lecture 3: Constructors and Destructors

Initializing Objects with ConstructorsA constructor can be used to automatically

initialize an object of the class when the object is created.

A constructor (abbreviated ctor) is a special member function that must be defined with the same name as the class

Cannot return a value, cannot specify a return type (not even void)

Page 3: Lecture 3: Constructors and Destructors

Initializing Objects with ConstructorsConstructor is called implicitly when an

object is created.If a class does not explicitly include a ctor,

the compiler provides a default constructor; i.e. a ctor with no arguments

Remember:

An implicit default constructor is built into the program automatically by the compiler if you don’t provide one.

Page 4: Lecture 3: Constructors and Destructors

Counter example // counter.cpp

// object represents a counter variable #include <iostream> using namespace std;

///////////////////////////////////////////////////////////////class Counter

{ private:

unsigned int count; //count public: Counter() : count(0) //constructor { /*empty body*/ }

void inc_count() //increment count { count++; }

int get_count() //return count { return count; }

}; ////////////////////////////////////////////////////////////////

Page 5: Lecture 3: Constructors and Destructors

int main() {

Counter c1, c2; //define and initialize

cout << “\nc1=” << c1.get_count(); //display cout << “\nc2=” << c2.get_count();

c1.inc_count(); //increment c1 c2.inc_count(); //increment c2 c2.inc_count(); //increment c2

cout << “\nc1=” << c1.get_count(); //display again

cout << “\nc2=” << c2.get_count();

cout << endl; return 0;

}

Page 6: Lecture 3: Constructors and Destructors

Format of the constructorSame name as the classNo return typeInitializer list

count() { count = 0; }

count() : count(0) { }

Page 7: Lecture 3: Constructors and Destructors

Overloaded constructorsTypes of Constructors

Compiler Generated ConstructorSimple ConstructorParameterized/overloaded constructors

Default constructors

• Default constructor– No arguments– May be implicit or explicit

• Overloaded constructor– Has arguments

Page 8: Lecture 3: Constructors and Destructors

Constructors with Default Arguments//time.h class Time{

private:int hour, minute, second;

public:Time(int=0, int=0, int=0); //default ctor………

};

Page 9: Lecture 3: Constructors and Destructors

Constructors with Default Arguments//time.cpp#include “time.h”Time::Time(int hr, int min, int sec){

hour=hr;minute=min;second=sec;

}………

Page 10: Lecture 3: Constructors and Destructors

Constructors with Default Arguments//TestTime.cpp…#include “time.h” void main(){

Time t1;Time t2(2);Time t3(21, 34);Time t4(12, 25, 42);…

}

Note: In C++ any function can use default argumentsA constructor that defaults all its arguments is also a

default constructor...that is , a constructor that can be invoked with no arguments

Page 11: Lecture 3: Constructors and Destructors

Copy Constructors• It is a member function which initializes an object using

another object of the same class.

• Invoked (implicitly) in following situations: ClassType obj1; ClassType obj2 = obj1; // Note: NOT assignment ClassType obj3(obj1);

• Other situations that invoke copy ctor:– An object is passed by value to a function– An object is returned as the value of a function, as in

return myGradeBook;

• Takes one argument, a reference to class type: ClassName(const ClassName & T);

Page 12: Lecture 3: Constructors and Destructors

Default Copy ConstructorIn the absence of a programmer-provided

copy constructor, the C++ compiler builds a default copy constructor for each class which performs a member-wise copy between objects.

Default copy constructors work fine unless the class contains pointer data members (more on this later)

Page 13: Lecture 3: Constructors and Destructors

class Foo{

private:

int data;

public:

Foo(): data(0) {}

~Foo(){}};

•No return values

•Same name as the class preceded by tilde (~)

•No arguments

•The most common use of destructors is to de-allocate memory that was allocated for the object (more later)

Page 14: Lecture 3: Constructors and Destructors

DestructorsDestructor does house-keeping when object

is destroyedTriggered by scope change or delete Required when user does dynamic

allocation; default is generateddestructor has form:

ClassName::~ClassName() {…}No return type or arguments: no

overloadingShould delete allocated memoryCalled immediately before object is

destroyed