Presentation is loading. Please wait.

Presentation is loading. Please wait.

CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.

Similar presentations


Presentation on theme: "CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor."— Presentation transcript:

1 CONSTRUCTOR AND DESTRUCTOR

2 Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor Default ConstructorDefault Constructor Parameterized ConstructorParameterized Constructor Copy ConstructorCopy Constructor 4. DestructorDestructor

3 Constructor These are the special function which are used to initialized the object at the time of declaration. It is a special function because its name is same as of the class name. The constructor is invoked whenever an object of its associated class is created. A constructor is declared and defined as follows:

4 class student { int rollno; int marks; public: student() // default constructor created { rollno = 0; marks = 0; } }; void main() { student s; // object created }

5 Special features of constructors: It is used to initialize the object at the time of declaration. Its name is same as that of class. It is always define in the public section. It does not return any value not even void. It is automatically executed when the object is created.

6 Types of Constructor There are three types of constructor as shown in the following diagram: Constructors DefaultParameterizedCopy

7 Default constructor: A constructor having no arguments (or parameters is called a default constructor. Following is the program for the default constructor : # include Class complex { float real; float img; public: complex(); void getno(); void putno(); } ;

8 Void complex :: getno() { cin >> real >> img; } Void complex :: putno() { if (img < 0) cout << real << img << “i”; else cout << real << “+” << img << “i”; }

9 Complex :: complex { real = img = 0; } Void main() { complex c1, c2; c1.getno(); c1.putno(); c2.putno(); }

10 Parameterized Constructor: The constructor with arguments are known as parameterized constructors. Constructor overloading is also possible. The parameter of constructor can be of any type except that of the class to which it belongs. For example: Class student { int a,b; public : student (int x, int y) // parametrized constructor { a = x; b = y; } };

11 # include Class complex { float real; float img; public: complex(); complex(float); complex(float,float); void getno(); void putno(); } ;

12 Void complex :: getno() { cin >> real >> img; } Void complex :: putno() { if (img < 0) cout << real << img << “i”; else cout << real << “+” << img << “i”; }

13 Complex :: complex { real = img = 0; } Complex :: complex(float x) { real = img = x; } Complex :: complex(float x, float y) { real = x; img = y; }

14 void main() { complex c1, c2; complex c3(5); complex c4(6,7); c1.getno(); c1.putno(); c2.putno(); c3.putno(); c4.putno(); }

15 Copy Constructor : Constructor can be created from another constructor. Copy constructor can be possible. In this case argument of the constructor will be of class type. It will not be of reference type. The copy constructor is defined in the class as a parameterized constructor receiving object as argument passed by reference as given below: class student { …………… ……………… public: student(student & obj); // copy constructor };

16 # include Class complex { float real; float img; public: complex(); complex(float,float); complex(complex &); void getno(); void putno(); } ;

17 Void complex :: getno() { cin >> real >> img; } Void complex :: putno() { if (img < 0) cout << real << img << “i”; else cout << real << “+” << img << “i”; }

18 Complex :: complex() { real = img = 0; } Complex :: complex(float x, float y) { real = x; img= y; } Complex :: complex(complex & x) { real = x.real; img = x.img; }

19 Void main() { complex c1, c2; complex c3(4,5); complex c4(c3); c1.getno(); c1.putno(); c2.putno(); c3.putno(); c4.putno(); }

20 Destructor A destructor as the name implies is used to destroy the object that has been created by a constructor. So destructors are member functions that are used to destroy the objects created by constructor. They again have same name as the class nut is preceded by a tilde sign (~). For example, the destructor for the class integer can be defined as : ~ integer () { } A destructor never takes any argument nor does it return any value. It will be invoked implicitly by the compiler upon exit from the program to clean up the storage that is no longer accessible.

21 # include int count = 0; Class alpha { public : alpha() { count++; cout << “\n No of objects created “ << count; } ~alpha() { cout << “\n No of objects destroyed “ << count; count--; } };

22 int main() { cout << “\n Enter Main \n”; alpha A1, A2, A3, A4; { cout << “ \n Enter Block 1”; alpha A5; } { cout << “ \n Enter Block 2”; alpha A6; } cout << “\n Re-enter Main \n”; }

23 THANKS


Download ppt "CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor."

Similar presentations


Ads by Google