C++ 1
C++ 1
OOP provides a clear structure for the programs. OOP helps to keep the C++ code
DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and
debug. OOP makes it possible to create full reusable applications with less code
and shorter development time.
Objective of OOP
C++ is a programming language with a special focus on the concepts of OOP and
their implementation. It has object-oriented features, which allow the programmer
to create objects within the code. This makes programming easier, more efficient,
and some would even say more fun.
. It deallocates all the memory previously used by the object of the class so
that there will be no memory leaks. The destructor also has the same name
as the class but with tilde (~) as prefix.
Difference between keywords and identifiers.
Destructor
Destructor is an instance member function that is invoked automatically
whenever an object is going to be destroyed. Meaning, a destructor is the
last function that is going to be called before an object is destroyed.
In this article, we will learn about the destructors in C++, how they work, how
and why to create the user defined destructors with the code examples. At
the end, we will look at some commonly used questions about C++
destructors.
Characteristics of a Destructor
A destructor is also a special member function like a constructor.
Destructor destroys the class objects created by the constructor.
Destructor has the same name as their class name preceded by a tilde
(~) symbol.
It is not possible to define more than one destructor.
The destructor is only one way to destroy the object created by the
constructor. Hence, destructor cannot be overloaded.
It cannot be declared static or const.
Destructor neither requires any argument nor returns any value.
It is automatically called when an object goes out of scope.
Destructor release memory space occupied by the objects created by the
constructor.
In destructor, objects are destroyed in the reverse of an object creation.
The thing is to be noted here if the object is created by using new or the
constructor uses new to allocate memory that resides in the heap memory or
the free store, the destructor should use delete to free the memory.
Static data types
Static data members are class members that are declared
using static keywords. A static member has certain special characteristics
which are as follows:
Only one copy of that member is created for the entire class and is shared
by all the objects of that class, no matter how many objects are created.
It is initialized before any object of this class is created, even before the
main starts outside the class itself.
It is visible can be controlled with the class access specifiers.
Its lifetime is the entire program.