Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link
Posts

8.2 Constructor - Destructor

2 min read
Image result for Constructor - Destructor c++ 
Constructor
A constructor is a special kind of class member function that is executed when an object of that class is instantiated.
Constructors are typically used to initialize member variables of the class to appropriate default values, or to allow the user to easily initialize those member variables to whatever values are desired.

Unlike normal functions, constructors have specific rules for how they must be named:
1) Constructors should always have the same name as the class (with the same capitalization)
2) Constructors have no return type (not even void)

A constructor that takes no parameters (or has all optional parameters) is called a default constructor but if you need, a constructor can have parameters. This helps you to assign initial value to an object at the time of its creation.

Destructor
A destructor is another special kind of class member function that is executed when an object of that class is destroyed. They are the counterpart to constructors. When a variable goes out of scope, or a dynamically allocated variable is explicitly deleted using the delete keyword, the class destructor is called (if it exists) to help clean up the class before it is removed from memory.

For simple classes, a destructor is not needed because C++ will automatically clean up the memory for you. However, if you have dynamically allocated memory, or if you need to do some kind of maintenance before the class is destroyed (eg. closing a file), the destructor is the perfect place to do so.

Like constructors, destructors have specific naming rules:
1) The destructor must have the same name as the class, preceded by a tilde (~).
2) The destructor can not take arguments.
3) The destructor has no return type.

Note that rule 2 implies that only one destructor may exist per class, as there is no way to overload destructors since they can not be differentiated from each other based on arguments.

Example
class abc
{
private :
int a,b;

public :
abc()//default constructor
{
a=0;
b=1;
}

abc(int x, int y) //Parametrized constructor
{
a=x;
b=y;
}

~abc(); //destructor
.
.
.
};

                      8.3 Friend Function          NEXT >>

You may like these posts

  • Pointer is a variable that stores the address of another variable. They can make some things much easier, help improve your program's efficiency, and even allow you to handle u…
  • Array is a fixed size collection of similar data type items. Arrays are used to store and access group of data of same data type. Arrays can of any data type. Arrays must…
  • References are another variable type of C++ that act as an alias or short name to another variable. A reference variable acts just like the original variable it is referencin…
  • On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. cout is an instance of iostream class …
  • C++ allows us to create own own data types. enumerated type is the simplest method for doing so. An enumerated type is a data type where every possible value is defined as a s…
  • In Array we can store data of one type only, but structure is a variable that gives facility of storing data of different data type in one variable. Structures are variab…

Post a Comment