Class Constructors and destructors in C++
Class Constructors and destructors in C++
In this C++ tutorial
you will learn about Class Constructors and destructors in C++ viz.,
Constructors, What is the use of Constructor, General Syntax of
Constructor, Destructors, What is the use of Destructors and General
Syntax of Destructors.
Constructors:
What is the use of Constructor
The main use of
constructors is to initialize objects. The function of initialization
is automatically carried out by the use of a special member function
called a constructor.
General Syntax of Constructor
Constructor is a
special member function that takes the same name as the class name.
The syntax generally is as given below:
<class name> {
arguments};
The default constructor
for a class X has the form
X::X()
In
the above example the arguments is optional.
The constructor is automatically named when an object is created. A constructor is named whenever an object is defined or dynamically allocated using the "new" operator.
There are several forms in which a constructor can take its shape namely:
The constructor is automatically named when an object is created. A constructor is named whenever an object is defined or dynamically allocated using the "new" operator.
There are several forms in which a constructor can take its shape namely:
Default Constructor:
This
constructor has no arguments in it. Default Constructor is also
called as no argument
constructor.
For example:
class Item
{ private: int a,b; public: Item(); ... }; Item :: Item() { a=0; b=0; } |
Copy constructor:
This
constructor takes one argument. Also called one argument constructor.
The main use of copy constructor is to initialize the objects while
in creation, also used to copy an object. The copy constructor allows
the programmer to create a new object from an existing one by
initialization.
For example to invoke a copy constructor the programmer writes:
For example to invoke a copy constructor the programmer writes:
Item
e3(e2);
or
Item e3=e2;
or
Item e3=e2;
Both the above formats
can be sued to invoke a copy constructor.
For Example:
For Example:
#include
<iostream.h>
class Item() { private: int a; public: Item() { } Item(int w) { a=w; } Item(Item& e) { a=e.a; cout<<” Example of Copy Constructor”; } void result() { cout<< a; } };
void
main()
{ Item e1(50); Item e3(e1); cout<< “\ne3=”;e3.result(); } |
In
the above the copy constructor takes one argument an object of type
Item which is passed by reference. The output of the above program is
Example of Copy
Constructor
e3=50
e3=50
Some important points
about constructors:
- A constructor takes the same name as the class name.
- The programmer cannot declare a constructor as virtual or static, nor can the programmer declare a constructor as const, volatile, or const volatile.
- No return type is specified for a constructor.
- The constructor must be defined in the public. The constructor must be a public member.
- Overloading of constructors is possible. This will be explained in later sections of this tutorial.
Destructors
What is the use of Destructors
Destructors are also
special member functions used in C++ programming language.
Destructors have the opposite function of a constructor. The main use
of destructors is to release dynamic allocated memory. Destructors
are used to free memory, release resources and to perform other clean
up. Destructors are automatically named when an object is destroyed.
Like constructors, destructors also take the same name as that of the
class name.
General Syntax of Destructors
~ classname();
The above is the
general syntax of a destructor. In the above, the symbol tilda ~
represents a destructor which precedes the name of the class.
Some
important points about destructors:
- Destructors take the same name as the class name.
- Like the constructor, the destructor must also be defined in the public. The destructor must be a public member.
- The Destructor does not take any argument which means that destructors cannot be overloaded.
- No return type is specified for destructors.
For example:
class
Item
{ private: …………… public: Item() { } ~ Item() { } } |
No comments:
Post a Comment