Constructor and Destructor Malik Jahan Khan 1 Constructor









- Slides: 9
Constructor and Destructor Malik Jahan Khan 1
Constructor • Sometimes, it may be convenient if an object can initialize itself when it’s first created, without requiring a separate call to a member function • Automatic initialization is carried out using a special member function called constructor • A constructor is a member function which is executed whenever an object is created • Constructor must have the same name as that of class 2
#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 c 1, c 2; //define and initialize cout << "nc 1=" << c 1. get_count(); //display cout << "nc 2=" << c 2. get_count(); c 1. inc_count(); //increment c 1 c 2. inc_count(); //increment c 2 cout << "nc 1=" << c 1. get_count(); //display again cout << "nc 2=" << c 2. get_count(); cout << endl; return 0; } Counter Example Output C 1=0 C 2=0 C 1=1 C 2=2 3
An Alternate Way of Initializing • count() { count =0; } OR count(): count(0) //Initializer List {} • Members initialized in the initializer list are given value before the execution of the constructor 4
Initializer List • Constructor is used to initialize data members at the time of creation of an object • Initializer List: When more than one data members need to be initialized – some. Class() : m 1(7), m 2(33), m 3(4) //Initializer List {/*Empty Body*/} 5
Destructor • Constructor is a member function of a class which is automatically called when an object is created of the that class • Destructor is a member function of a class which does the opposite of a constructor – It is automatically called when an object is destroyed from memory • Destructor also has the same name as that of a class but a tilde sign before name, like ~class. Name(){…} 6
#include<iostream> using namespace std; class Person { private: int id; public: void set. ID(int d) { id=d; } void show. ID() { cout<<"n My ID is "<<id; } ~Person() { cout<<"n ID No. "<<id<<" says you bye!"; } }; int main() { Person p 1, p 2, p 3; p 1. set. ID(10); p 2. set. ID(20); p 3. set. ID(30); p 1. show. ID(); p 2. show. ID(); p 3. show. ID(); } Example Output My ID is 10 My ID is 20 My ID is 30 ID No. 30 says you bye! ID No. 20 says you bye! ID No. 10 says you bye! 7
#include<iostream> using namespace std; class Person { private: int id; public: Person() : id(0) { cout<<"n ID No. "<<id<<" says welcome!"; } void set. ID(int d) { id=d; } void show. ID() { cout<<"n My ID is "<<id; } ~Person() { cout<<"n ID No. "<<id<<" says you bye!"; } }; int main() { Person p 2, p 1, p 3; p 1. set. ID(10); p 2. set. ID(20); p 3. set. ID(30); p 1. show. ID(); p 2. show. ID(); p 3. show. ID(); } Another Example Output ID No. 0 says welcome! My ID is 10 My ID is 20 My ID is 30 ID No. 30 says you bye! ID No. 10 says you bye! ID No. 20 says you bye! 8
Homework Exercises • Chapter 6 – Exercise No. 1, 2, 3 9