lecture 3: constructors and destructors
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 PresentationTRANSCRIPT
Bushra Riaz
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)
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.
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; }
}; ////////////////////////////////////////////////////////////////
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;
}
Format of the constructorSame name as the classNo return typeInitializer list
count() { count = 0; }
count() : count(0) { }
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
Constructors with Default Arguments//time.h class Time{
private:int hour, minute, second;
public:Time(int=0, int=0, int=0); //default ctor………
};
Constructors with Default Arguments//time.cpp#include “time.h”Time::Time(int hr, int min, int sec){
hour=hr;minute=min;second=sec;
}………
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
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);
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)
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)
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