SlideShare a Scribd company logo
Introduction to Object Oriented
Programming (OOP)
Prepared By
Anita Vikram Shinde
Procedural Oriented Language
• Conventional programming, using high-level language such as COBOL,
FORTRAN and C are commonly known as Procedure oriented language
(POP).
• In POP numbers of functions are written to accomplish the tasks
2
Procedural Oriented Language
- Structure -
Function-1 Function-2 Function-3
Function-4 Function-5
Function-6 Function-7 Function-8
Main
program
3
Procedure Oriented Language
- Characteristics -
• Emphasis is on doing things (algorithms).
• Larger programs are divided into smaller programs known as functions.
• Most of the functions share global data.
• Data move openly around the system from function to function.
• Employs top-down approach in program design.
4
Procedural Oriented Language
- Limitations -
• Data move freely around the program and are therefore vulnerable to
changes caused by any function in the program.
• It does not model very well the real world problems.
5
70 + OOP Based Programming Languages
Object Oriented Programming
• OOP treats data as critical element
• Ties data more closely to the functions that operate on it & allows
decomposition of problem into objects.
OBJECT
Operations
Data
OBJECT
Operations
Data
OBJECT
Operations
Data
Communication
7
Procedure Oriented Programming Object Oriented Programming
Divided Into In POP, program is divided into small parts
called functions
In OOP, program is divided into parts called objects
Importance In POP, Importance is not given to data but to
functions as well as sequence of actions to be
done
In OOP, Importance is given to the data rather than
procedures or functions because it works as a real
world
Approach POP follows Top Down approach OOP follows Bottom Up approach
Access Specifiers POP does not have any access specifier. OOP has access specifiers named Public, Private,
Protected, etc.
Data Moving In POP, Data can move freely from function to
function in the system
In OOP, objects can move and communicate with
each other through member functions
Expansion To add new data and function in POP is not so easy OOP provides an easy way to add new data and
function
Data Access In POP, Most function uses Global data for sharing
that can be accessed freely from function to
function in the system
In OOP, data can not move easily from function to
function, it can be kept public or private so we can
control the access of data
Data Hiding POP does not have any proper way for hiding data
so it is less secure
OOP provides Data Hiding so provides more
security
Examples Example of POP are : C, VB, FORTRAN, Pascal Example of OOP are : C++, JAVA, VB.NET, C#.NET
8
Fundamentals of OOP
• Objects
• Classes
• Encapsulation
• Data Abstraction
• Inheritance
• Polymorphism
• Dynamic Binding
• Message Passing
9
Four Pillars of OOPs
Objects
• OOP uses objects as its fundamental building blocks.
• Objects are the basic run-time entities in an object-oriented system.
• Every object is associated with data and functions which define meaningful
operations on that object.
• Object is a real world existing entity.
• Object is an Instance of a particular class.
11
Object
Attributes
Operation
Operation
Operation
Operation
12
Example: StudentObject
st_name
st_id
branch
semester
Enroll()
Displayinfo()
Performance()
Result()
13
Object
Class
• Class is a collection of similar objects.
Class
15
Class
• Define a attribute and behaviours of an object
Object Oriented Programming  Constructors &  Destructors
Object Oriented Programming  Constructors &  Destructors
Class Specification
• Syntax:
class class_name
{
};
Data members
Members functions
19
A Class is a way to bind the data and its associated functions together
General form of class declaration is:
class Class-name
{
private :
Variables declarations;
functions declarations;
public:
Variables declarations;
functions declarations;
};
Specifying Class
Class Specification
• class Student
{
int st_id;
char st_name[];
void read_data();
void print_data();
};
Data Members or Properties of
Student Class
Members Functions or
Behaviours of Student Class
21
Class Specification
• Visibility of Data members & Member functions
public –
Accessed by member functions and all other
non-member functions in the program.
private –
Accessed by only member functions of the class.
protected –
Similar to private, but accessed by all the member
functions of immediate derived class
Default –
All items defined in the class are private.
22
■ The class members that have been declared as private can be
accessed only from within the class.
■ The public members can be accessed from outside the class also.
■ The use of the keyword private is optional. By default , the members
of class are private
■ Data members & Member functions.
■ Only member functions can have access to the private data members
and private functions
Example of class
class student
{
int roll_no;
float marks;
public:
void input(int a, float b);
void display(void);
};
Creating Objects of class
■ student x,y,z;
OR
■ class student
{
……..
}x, y, z;
Accessing class data
main() can not contain statement that access roll_no and marks.
Only member functions can access private data .
Member functions can be called only by object using dot(.) operator.
Format for Calling member function :
object-name . Function-name (actual- arg)
ex. x.input(25, 32.5);
Defining member function
Member functions can be defined in two
places:
1. Outside the class definition
2. Inside the class definition
Outside the class definition
The imp difference between member function and
normal function is that member function
incorporates a membership ‘identity label’ in
the header.
This label tells compiler which class the function
belongs to.
■ The general form of member function definition outside class
is:
Return-type class-name :: fun–name(arg-list)
{
function body;
}
Ex. void student :: input(int a, float b)
{
rollno= a;
marks = b;
}
Member Function definition inside the class
class student
{
int rollno;
float marks;
public:
void input( int a, float b);
void display(void)
{
cout<<rollno<< marks;
}
};
Nesting of Member Functions
A member function can be called by using its name
inside another member function of the same class.
This is known as nesting of member functions.
class student
{ int roll_no;
float marks;
public:
void input(int a, float b);
void display(void);
};
void student ::input(int a, float b)
{
rollno= a;
marks = b;
display();
}
void student ::display()
{
cout<<rollno;
cout<<marks;
}
int main()
{
student s1;
s1.input(10, 75.55);
return 0;
}
Memory allocation for Objects
■ The memory place for objects is allocated when they are declared and
not when the class is specified. This is partly true.
■ The member functions are created and placed in memory space only
once when they are defined as part of class specification.
■ All objects belonging to that class use same member functions so no
separate space is allocated for member functions when objects are
created.
■ Only space for member variables is allocated separately for each object.
Arrays of Objects
• Several objects of the same class can be declared as an array and
used just like an array of any other data type.
• The syntax for declaring and using an object array is exactly the
same as it is for any other type of array.
34
Array of objects
Class employee
{
char name[10];
int emp_code ;
public:
void input(void);
void display(void);
};
employee manager[10];
employee worker[10];
manager[i].display();
Constructors
• A constructor function is a special member function that is a member of a class
and has the same name as that class, used to create, and initialize objects of
the class.
• Constructor function do not have return type.
• Should be declared in public section.
• Invoked automatically when objects are created
Constructors
Syntax:
class class_name
{
public:
class_name();
};
Example:
class student
{ int st_id;
public:
student()
{
st_id=0;
}
Constructors
• How to call this special function…?
int main()
{
student st;
…………
…………
};
class student
{
int st_id;
public:
student()
{
st_id=0;
}
};
38
Types of Constructors
• Parameterized constructors
• Constructors with default argument
• Copy constructors
• Dynamic constructors
39
Example of constructor:
A constructor is declared and defined as follows:
class integer
{
int m,n;
public:
integer (void);
…
};
integer :: integer (void)
{
m=0;
n=0;
}
Example of constructor:
integer int1;
A constructor that accepts no parameters is called default constructor.
Special characteristics of constructor:
They should be declared in public section.
They are invoked automatically when the objects are created
They don’t have any return type , Not even void. So they can not return
values.
They can have default arguments.
We can not refer to their addresses
They can not be inherited.
Parameterized Constructors
class Addition
{
int num1;
int num2;
int res;
public:
Addition(int a, int b); // constructor
void add( );
void print();
};
Constructor with parameters
B’Coz it’s also a function!
Constructor that can take arguments
is called parameterized constructor.
42
Parameterized Constructors:
• The constructor that can take arguments are called
parameterized constructor.
class integer
{
int m,n;
public:
integer (int a, int b);
}
integer :: integer (int a, int b)
{
m=a;
n=b;
}
• By calling constructor explicitly
integer int1= integer(10,20);
• By calling constructor implicitly
integer int1(10,20);
Constructor with default arguments:
Ex. Complex(float real, float imag=10);
Complex c1(2.0);
Complex c2(3.0, 5.0);
Constructors with Default Argument
class Addition
{
int num1;
int num2;
int res;
public:
Addition(int a, int b=0); // constructor
void add( );
void print();
};
Constructor with default
parameter.
45
Copy constructor
Copy Constructor is used to declare and initialize object
from another object.
integer (integer &I2); //Function prototyping
ex. integer I2(I1);
OR
integer I2=I1;
But I2=I1; Won’t invoke copy constructor.
Copy Constructor
class code
{
int id;
public:
code() //default constructor
{ id=0;}
code(int a){id=a;} // parameterized constructor
code(code &obj) // copy constructor
{
id=obj.id;
}
void display()
{
cout<<id;
}
};
int main()
{ code A(100);
code B(A);
code C=A;
code D;
D=A; // wrong syntax for copy construcor
cout<<“ id of A:”; A.display();
cout<<“ id of B:”; B.display();
cout<<“ id of C:”; C.display();
cout<<“ id of D:”; D.display();
}
47
Multiple constructors in a class/ Can we overload constructors
class integer
{
int m,n;
public:
integer( ){m=0;n=0;} //Default constructor
integer(int a , int b) //Parameterized constructor
{m=a;n=b;}
integer(integer &j) //Copy constructor
{m=j.m; n=j.n;}
};
• integer I1; invoke Default constructor
• integer I2(5,10); invoke Parameterized constructor
• integer I3(I2); invoke Copy constructor
• This is known as overloading of constructor
Dynamic constructor:
• The constructor can be used to allocate memory while creating
objects.
• Allocation of memory to objects at the time of their construction is
known as dynamic construction of object.
• The memory is allocated with the help of new operator.
Destructors
• A destructor is used to destroy the objects that have been created by constructor.
• The destructor is a member function whose name is same as the class name but preceded by
tilde(~).
Ex. ~integer ( );
• A destructor never takes any arguments nor does it return any value
• It will be invoked implicitly by the compiler upon exit from the program (or block or function) to
clean up storage that is no longer accessible.
• It's good practice to use destructor to release memory for future use.
• When new is used in constructor then use delete in destructor.
• The objects are destroyed in the reverse order of creation.
#include<iostream.h>
int count=0;
class alpha
{
public:
alpha( )
{
count++;
cout<<“No of object created<< count;
}
~alpha( )
{
cout<<“No of object destroyed<< count;
count--;
}
int main( )
{
cout<< “Enter main”;
alpha A1, A2, A3, A4;
{
cout<<“Enter Block1”;
alpha A5;
}
{
cout<<“Enter Block2”;
alpha A6;
}
cout<<“ Re-enter Main”;
return 0; }
• OUTPUT
Enter Main
No of object created 1
No of object created 2
No of object created 3
No of object created 4
Enter Block1
No of object created 5
No of object destroyed 5
Enter Block2
No of object created 5
No of object destroyed 5
Re-Enter Main
No of object destroyed 4
No of object destroyed 3
No of object destroyed 2
No of object destroyed 1

More Related Content

PDF
Qlik view server reference manual eng
PDF
Introduction to C++ Class & Objects. Book Notes
PPT
classandobjectunit2-150824133722-lva1-app6891.ppt
PDF
Class and object
PPTX
Classes and objects
PPT
Object and class presentation
PPTX
Class and objects
PPT
classes data type for Btech students.ppt
Qlik view server reference manual eng
Introduction to C++ Class & Objects. Book Notes
classandobjectunit2-150824133722-lva1-app6891.ppt
Class and object
Classes and objects
Object and class presentation
Class and objects
classes data type for Btech students.ppt

Similar to Object Oriented Programming Constructors & Destructors (20)

PPTX
Concept of Object-Oriented in C++
PDF
Implementation of oop concept in c++
PPTX
Class and object
PPT
Classes, objects and methods
PPTX
oop lecture 3
PPTX
Object Oriented Programming Using C++
PPT
Class and object in C++
PPTX
Oop objects_classes
PPT
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
PPTX
oopusingc.pptx
PPTX
Presentation on class and object in Object Oriented programming.
PPTX
Lecture 2 (1)
PPTX
Introduction to Fundamental of Class.pptx
PPT
cpp class unitdfdsfasadfsdASsASass 4.ppt
PPTX
Classes and objects till 16 aug
PPTX
OBJECT ORIENTED PROGRAMING IN C++
PDF
PPTX
Classes and objects1
PPTX
B.sc CSIT 2nd semester C++ Unit3
PPTX
Classes and objects
Concept of Object-Oriented in C++
Implementation of oop concept in c++
Class and object
Classes, objects and methods
oop lecture 3
Object Oriented Programming Using C++
Class and object in C++
Oop objects_classes
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
oopusingc.pptx
Presentation on class and object in Object Oriented programming.
Lecture 2 (1)
Introduction to Fundamental of Class.pptx
cpp class unitdfdsfasadfsdASsASass 4.ppt
Classes and objects till 16 aug
OBJECT ORIENTED PROGRAMING IN C++
Classes and objects1
B.sc CSIT 2nd semester C++ Unit3
Classes and objects
Ad

Recently uploaded (20)

PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PDF
composite construction of structures.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
Queuing formulas to evaluate throughputs and servers
PDF
ETO & MEO Certificate of Competency Questions and Answers
PPT
Drone Technology Electronics components_1
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Practice Questions on recent development part 1.pptx
PPTX
Lecture Notes Electrical Wiring System Components
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
web development for engineering and engineering
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
composite construction of structures.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
Internet of Things (IOT) - A guide to understanding
Arduino robotics embedded978-1-4302-3184-4.pdf
Queuing formulas to evaluate throughputs and servers
ETO & MEO Certificate of Competency Questions and Answers
Drone Technology Electronics components_1
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
CYBER-CRIMES AND SECURITY A guide to understanding
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Practice Questions on recent development part 1.pptx
Lecture Notes Electrical Wiring System Components
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
web development for engineering and engineering
Ad

Object Oriented Programming Constructors & Destructors

  • 1. Introduction to Object Oriented Programming (OOP) Prepared By Anita Vikram Shinde
  • 2. Procedural Oriented Language • Conventional programming, using high-level language such as COBOL, FORTRAN and C are commonly known as Procedure oriented language (POP). • In POP numbers of functions are written to accomplish the tasks 2
  • 3. Procedural Oriented Language - Structure - Function-1 Function-2 Function-3 Function-4 Function-5 Function-6 Function-7 Function-8 Main program 3
  • 4. Procedure Oriented Language - Characteristics - • Emphasis is on doing things (algorithms). • Larger programs are divided into smaller programs known as functions. • Most of the functions share global data. • Data move openly around the system from function to function. • Employs top-down approach in program design. 4
  • 5. Procedural Oriented Language - Limitations - • Data move freely around the program and are therefore vulnerable to changes caused by any function in the program. • It does not model very well the real world problems. 5
  • 6. 70 + OOP Based Programming Languages
  • 7. Object Oriented Programming • OOP treats data as critical element • Ties data more closely to the functions that operate on it & allows decomposition of problem into objects. OBJECT Operations Data OBJECT Operations Data OBJECT Operations Data Communication 7
  • 8. Procedure Oriented Programming Object Oriented Programming Divided Into In POP, program is divided into small parts called functions In OOP, program is divided into parts called objects Importance In POP, Importance is not given to data but to functions as well as sequence of actions to be done In OOP, Importance is given to the data rather than procedures or functions because it works as a real world Approach POP follows Top Down approach OOP follows Bottom Up approach Access Specifiers POP does not have any access specifier. OOP has access specifiers named Public, Private, Protected, etc. Data Moving In POP, Data can move freely from function to function in the system In OOP, objects can move and communicate with each other through member functions Expansion To add new data and function in POP is not so easy OOP provides an easy way to add new data and function Data Access In POP, Most function uses Global data for sharing that can be accessed freely from function to function in the system In OOP, data can not move easily from function to function, it can be kept public or private so we can control the access of data Data Hiding POP does not have any proper way for hiding data so it is less secure OOP provides Data Hiding so provides more security Examples Example of POP are : C, VB, FORTRAN, Pascal Example of OOP are : C++, JAVA, VB.NET, C#.NET 8
  • 9. Fundamentals of OOP • Objects • Classes • Encapsulation • Data Abstraction • Inheritance • Polymorphism • Dynamic Binding • Message Passing 9
  • 11. Objects • OOP uses objects as its fundamental building blocks. • Objects are the basic run-time entities in an object-oriented system. • Every object is associated with data and functions which define meaningful operations on that object. • Object is a real world existing entity. • Object is an Instance of a particular class. 11
  • 15. Class • Class is a collection of similar objects. Class 15
  • 16. Class • Define a attribute and behaviours of an object
  • 19. Class Specification • Syntax: class class_name { }; Data members Members functions 19
  • 20. A Class is a way to bind the data and its associated functions together General form of class declaration is: class Class-name { private : Variables declarations; functions declarations; public: Variables declarations; functions declarations; }; Specifying Class
  • 21. Class Specification • class Student { int st_id; char st_name[]; void read_data(); void print_data(); }; Data Members or Properties of Student Class Members Functions or Behaviours of Student Class 21
  • 22. Class Specification • Visibility of Data members & Member functions public – Accessed by member functions and all other non-member functions in the program. private – Accessed by only member functions of the class. protected – Similar to private, but accessed by all the member functions of immediate derived class Default – All items defined in the class are private. 22
  • 23. ■ The class members that have been declared as private can be accessed only from within the class. ■ The public members can be accessed from outside the class also. ■ The use of the keyword private is optional. By default , the members of class are private ■ Data members & Member functions. ■ Only member functions can have access to the private data members and private functions
  • 24. Example of class class student { int roll_no; float marks; public: void input(int a, float b); void display(void); };
  • 25. Creating Objects of class ■ student x,y,z; OR ■ class student { …….. }x, y, z;
  • 26. Accessing class data main() can not contain statement that access roll_no and marks. Only member functions can access private data . Member functions can be called only by object using dot(.) operator. Format for Calling member function : object-name . Function-name (actual- arg) ex. x.input(25, 32.5);
  • 27. Defining member function Member functions can be defined in two places: 1. Outside the class definition 2. Inside the class definition
  • 28. Outside the class definition The imp difference between member function and normal function is that member function incorporates a membership ‘identity label’ in the header. This label tells compiler which class the function belongs to.
  • 29. ■ The general form of member function definition outside class is: Return-type class-name :: fun–name(arg-list) { function body; } Ex. void student :: input(int a, float b) { rollno= a; marks = b; }
  • 30. Member Function definition inside the class class student { int rollno; float marks; public: void input( int a, float b); void display(void) { cout<<rollno<< marks; } };
  • 31. Nesting of Member Functions A member function can be called by using its name inside another member function of the same class. This is known as nesting of member functions.
  • 32. class student { int roll_no; float marks; public: void input(int a, float b); void display(void); }; void student ::input(int a, float b) { rollno= a; marks = b; display(); } void student ::display() { cout<<rollno; cout<<marks; } int main() { student s1; s1.input(10, 75.55); return 0; }
  • 33. Memory allocation for Objects ■ The memory place for objects is allocated when they are declared and not when the class is specified. This is partly true. ■ The member functions are created and placed in memory space only once when they are defined as part of class specification. ■ All objects belonging to that class use same member functions so no separate space is allocated for member functions when objects are created. ■ Only space for member variables is allocated separately for each object.
  • 34. Arrays of Objects • Several objects of the same class can be declared as an array and used just like an array of any other data type. • The syntax for declaring and using an object array is exactly the same as it is for any other type of array. 34
  • 35. Array of objects Class employee { char name[10]; int emp_code ; public: void input(void); void display(void); }; employee manager[10]; employee worker[10]; manager[i].display();
  • 36. Constructors • A constructor function is a special member function that is a member of a class and has the same name as that class, used to create, and initialize objects of the class. • Constructor function do not have return type. • Should be declared in public section. • Invoked automatically when objects are created
  • 38. Constructors • How to call this special function…? int main() { student st; ………… ………… }; class student { int st_id; public: student() { st_id=0; } }; 38
  • 39. Types of Constructors • Parameterized constructors • Constructors with default argument • Copy constructors • Dynamic constructors 39
  • 40. Example of constructor: A constructor is declared and defined as follows: class integer { int m,n; public: integer (void); … }; integer :: integer (void) { m=0; n=0; }
  • 41. Example of constructor: integer int1; A constructor that accepts no parameters is called default constructor. Special characteristics of constructor: They should be declared in public section. They are invoked automatically when the objects are created They don’t have any return type , Not even void. So they can not return values. They can have default arguments. We can not refer to their addresses They can not be inherited.
  • 42. Parameterized Constructors class Addition { int num1; int num2; int res; public: Addition(int a, int b); // constructor void add( ); void print(); }; Constructor with parameters B’Coz it’s also a function! Constructor that can take arguments is called parameterized constructor. 42
  • 43. Parameterized Constructors: • The constructor that can take arguments are called parameterized constructor. class integer { int m,n; public: integer (int a, int b); } integer :: integer (int a, int b) { m=a; n=b; }
  • 44. • By calling constructor explicitly integer int1= integer(10,20); • By calling constructor implicitly integer int1(10,20); Constructor with default arguments: Ex. Complex(float real, float imag=10); Complex c1(2.0); Complex c2(3.0, 5.0);
  • 45. Constructors with Default Argument class Addition { int num1; int num2; int res; public: Addition(int a, int b=0); // constructor void add( ); void print(); }; Constructor with default parameter. 45
  • 46. Copy constructor Copy Constructor is used to declare and initialize object from another object. integer (integer &I2); //Function prototyping ex. integer I2(I1); OR integer I2=I1; But I2=I1; Won’t invoke copy constructor.
  • 47. Copy Constructor class code { int id; public: code() //default constructor { id=0;} code(int a){id=a;} // parameterized constructor code(code &obj) // copy constructor { id=obj.id; } void display() { cout<<id; } }; int main() { code A(100); code B(A); code C=A; code D; D=A; // wrong syntax for copy construcor cout<<“ id of A:”; A.display(); cout<<“ id of B:”; B.display(); cout<<“ id of C:”; C.display(); cout<<“ id of D:”; D.display(); } 47
  • 48. Multiple constructors in a class/ Can we overload constructors class integer { int m,n; public: integer( ){m=0;n=0;} //Default constructor integer(int a , int b) //Parameterized constructor {m=a;n=b;} integer(integer &j) //Copy constructor {m=j.m; n=j.n;} };
  • 49. • integer I1; invoke Default constructor • integer I2(5,10); invoke Parameterized constructor • integer I3(I2); invoke Copy constructor • This is known as overloading of constructor Dynamic constructor: • The constructor can be used to allocate memory while creating objects. • Allocation of memory to objects at the time of their construction is known as dynamic construction of object. • The memory is allocated with the help of new operator.
  • 50. Destructors • A destructor is used to destroy the objects that have been created by constructor. • The destructor is a member function whose name is same as the class name but preceded by tilde(~). Ex. ~integer ( ); • A destructor never takes any arguments nor does it return any value • It will be invoked implicitly by the compiler upon exit from the program (or block or function) to clean up storage that is no longer accessible. • It's good practice to use destructor to release memory for future use. • When new is used in constructor then use delete in destructor. • The objects are destroyed in the reverse order of creation.
  • 51. #include<iostream.h> int count=0; class alpha { public: alpha( ) { count++; cout<<“No of object created<< count; } ~alpha( ) { cout<<“No of object destroyed<< count; count--; }
  • 52. int main( ) { cout<< “Enter main”; alpha A1, A2, A3, A4; { cout<<“Enter Block1”; alpha A5; } { cout<<“Enter Block2”; alpha A6; } cout<<“ Re-enter Main”; return 0; }
  • 53. • OUTPUT Enter Main No of object created 1 No of object created 2 No of object created 3 No of object created 4 Enter Block1 No of object created 5 No of object destroyed 5 Enter Block2 No of object created 5 No of object destroyed 5 Re-Enter Main No of object destroyed 4 No of object destroyed 3 No of object destroyed 2 No of object destroyed 1