Lecture 4 1 UNIT 1 Constructor Destructor SUNIL

  • Slides: 12
Download presentation
Lecture 4. 1 (UNIT -1) Constructor & Destructor SUNIL KUMAR CIT-UPES | Jan 2016|

Lecture 4. 1 (UNIT -1) Constructor & Destructor SUNIL KUMAR CIT-UPES | Jan 2016| © 2012 UPES

Objectives (4. 1 +4. 2) Upon completion of today lecture, You will be :

Objectives (4. 1 +4. 2) Upon completion of today lecture, You will be : - § Able to understand how to initialize the member of the class. § Able to understand the concept of Inline Function § Able to apply the concept of destructor. Jan 2016 © 2012 UPES

Contents (4. 1+4. 2) o. Introduction o. Inline Function o. Characteristics of Constructor. o.

Contents (4. 1+4. 2) o. Introduction o. Inline Function o. Characteristics of Constructor. o. Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor o. Overloaded Constructor -Multiple Constructor in a class -Constructor with default argument o. Destructor o. Difference between constructor & destructor. o. Conclusion Jan 2016 © 2012 UPES

Concept of Constructor ØIn C++, a constructor is a ‘special’ member function whose task

Concept of Constructor ØIn C++, a constructor is a ‘special’ member function whose task is to initialize the objects of it’s class when it is created. ØIt is special because it’s name is the same as the class name. ØIt is called constructor because it constructs the value of data members of the class. ØThe constructor is invoked whenever an object of it’s associated class is created. Jan 2016 © 2012 UPES

Characteristics of Constructor Ø Constructor Functions have same name as that of class name.

Characteristics of Constructor Ø Constructor Functions have same name as that of class name. Ø It is a special member function of a class , which is used to construct the memory of object & provides initialization. Ø It must be declared in public part otherwise result will be error. Ø Cannot be virtual. Ø Cannot be inherited through a derived class can call the base class constructor. Ø They do not have return types, not even void Jan 2016 © 2012 UPES

Characteristics of Constructor Ø C++ has default constructors that are called whenever a class

Characteristics of Constructor Ø C++ has default constructors that are called whenever a class is declared even if no function with the same name exists ØIt can be defined by inline /offline method. ØDoes not need to call because it get call automatically whenever object is created. ØIt can be called explicitly also. ØIt can take parameters. ØConstructer can be overloaded. Jan 2016 © 2012 UPES

Program to explain concept of constructor #include<iostream. h> #include<conio. h> class stud { int

Program to explain concept of constructor #include<iostream. h> #include<conio. h> class stud { int roll_no; char name[20]; public: stud(); void show() { cout<<“roll_no”<<roll_no<<endl; cout<<“name”<<name<<endl; } }; Jan 2016 stude: : stud() { cout<<“enter roll_no”<<endl; cin>>roll_no; cout<<“enter name”<<endl; cin>>name; } void main() { stud s; clrscr(); s. show(); getch(); } © 2012 UPES

Types of constructor Constructor 1. Default Jan 2016 2. Parameterized 3. Copy © 2012

Types of constructor Constructor 1. Default Jan 2016 2. Parameterized 3. Copy © 2012 UPES

1. Default Constructor Ø Constructor that accepts no parameters is called the default constructor.

1. Default Constructor Ø Constructor that accepts no parameters is called the default constructor. Ø If no such constructor is defined , then the compiler supplies a default constructor. #include<iostream. h> class integer { int x; public: integer(); }; Jan 2016 integer: : integer() { x= 10; } void main() { integer int 1; getch(); } © 2012 UPES

2. Parameterized constructor Ø The constructors that can take arguments are as shown bellow

2. Parameterized constructor Ø The constructors that can take arguments are as shown bellow class integer { int m, n; public: integer(intx, inty); //parameterized constructor ---------------}; Jan 2016 integer: : integer(int x, int y) { m=x; n=y; } © 2012 UPES

Calling to Constructor ØImplicitly call integer int 1(0, 100); ØExplicitly call integer int 1=integer(0,

Calling to Constructor ØImplicitly call integer int 1(0, 100); ØExplicitly call integer int 1=integer(0, 100); In explicit call, an integer object int 1 is created and passes the values 0 and 100 to it. Jan 2016 © 2012 UPES

The constructor functions can also be defined as inline functions, class integer { int

The constructor functions can also be defined as inline functions, class integer { int m, n; public: integer(int x, int y) { m=x; n=y; } ---------------}; Jan 2016 e. g. class a { ------public: a(a); }; it is illegal class a { -- - - - public: a(a&); }; is valid © 2012 UPES