Constructor and Destructor in c++

1,177 views 14 slides Aug 04, 2021
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

What are the constructor and destructors in c++? Constructor and Destructor in c++ with example|| example of constructor and destructor in c++


Slide Content

Object Oriented Programming Constructor & Destructor in C++ BSCS(2 nd ) By: Syeda Zareen Fatima

What is Constructor??? Constructor is a Special type of function which has the same name as the class Constructor is being called automatically at the time of object declaration The return type of the constructor is the same as the type of the class

What is Destructor??? Destructor is a special member function that is executed automatically. An object is destroyed by destructor which was created by the constructor. Destructors are used to de-allocate the memory that has been allocated for the object by constructor. A destructor declaration should always begin with the tidle( ) symbol.

Ex. of Constructor and Destructor #include<iostream.h> #include<conio.h> Class test { Public: Test() {int n=10 ; Cout<<n; } --Test() { Cout<<“object destroyed ”; } }; void main() { test obj; getch(); }

Types of Constructor Default Constructor Parametric Constructor Copy Constructor

Why we use Constructors in C++ Constructor is a specialized member function of class which is used to create and initialize objects . Features A constructor can only have one access modifier which is public A constructor is never inherited & Overriden Each and every c++ class has constructor, either it is provided by compiler by default or explicitly created by user.

Example #include<stdio.h> #include<iostream.h> class A { p ublic: Int a; A() //constructor { a=100; } Void show() { c out<<a; }}; void main() { A obj; Obj.show(); g etch(); }

Difference b/w Constructor & Destructor Constructor Destructor It allocates memory to an object It de-allocates memory of an object The name of the constructor is the same as the class name The name of destructor is same as class name but precodint tildo sign. It is being automaticaly calle d at the time of object declaration It is automatically called at the time of object termination We can pass arguments through constructor We can not pass arguments through destructor We can declare multiple onstructors in a class Only one destructor is allowed in one class

Difference b/w Constructor & Destructor Constructor Destructor Constructor can be overloaded Destructor can not be overloaded Constructor has various types (default,parametrized, copy) Destructor has no various types Syntax Classname(parameter optional) { ________ ________ ____ } Syntax classname () { ________ ________ ______ }

What is Default Constructor? A constructor that accepts no parameters is called default constructor. Syntax class-name() { ________ // code _________ // code }

Example of Default Constructor #include<iostream.h> #include<conio.h> Class A { int a; Public: A() { a=100; Cout<<a; } }; void main() { c lrscr(); A obj; g etch(); }

What is Parametrized Constructor? A constructor that accets or receives parameters is called parametrized constructor. #include<iostream.h> #include<conio.h> Class A { Public: Int a,b; A(int x, int y) { a=x; b =y; } Void show() {cout<<a<<“ “<<b; }}; Void main (){ Clrscr(); A obj(10,20); Obj.show(); g etch(); }

What is Copy Constructor? A constructor that is used to copy or initialize the value of one object into another object is called copy constructor. Syntax Class-name(class-name & ref) { ________ //code ________ //code }

Example #include<iostream.h> #include<conio.h> Class A { Int a,b; Public: A(int x,int y) { a=x; b=y; } A(A & ref) { a=ref.a; b=ref.b; } Void show() { Cout<<a<<“ “<<b<<endl; } }; Void main() { A obj(10,20); A obj2=obj; Obj.show(); g etch(); }