Pages

January 30, 2013

Constructors & Destructors in C++ (OOPs)


constructors and destructors in C++ (OOPs) programming language

In C++, a class only creates a data type and the objects of a class are created and initialized as separate. A constructor (having the same name as that of a CLASS) is a member function which is automatically used to initialize the objects of the CLASS type with legal initial values.


Basic thing about CONSTRUCTORS

1. Constructor is similar to that of a function.
2. Displayed similar to that of a CLASS.
3. Constructor does not have any return type statements.
4. There is no need to call a constructor, when we create object of that CLASS it calls itself by default.
5. It’s a member function of a CLASS.

PROPERTIES OF Constructor

1. A constructor does not return any value.
2. No return type can be specified for a constructor. Not even void type can be used.
3. The constructor cannot be virtual.
4. Constructor cannot be static.
5. Constructor functions are called automatically when objects are called.
6. A constructor initializes all the objects created under CLASS.
7. If a CLASS has a constructor, each object of that CLASS will be initialized before any use is made in OBJECTS.
8. If no constructor is defined, then compiler will provide default constructor automatically with NULL initialization.
9. Member function can be accessed within a constructor function.
10. A constructor can have default arguments or default parameters.
11. Constructor also obeys access rules i.e. private and protected constructors are available only for friend function and private member function whereas public constructor is used for all the member functions.
12. Address of constructor can’t be copied.
13. All objects of CLASS with constructor cannot be a member of UNION.
14. Member function can be called from within the constructor.
15. The name of the constructor must be same as that of CLASS.

TYPES OF Constructors

Default Constructor 

When there is no constructor defined in a CLASS, the compiler automatically supplies a default constructor and if a constructor is declared with arguments it hides the default constructor. The default constructor supplied by the compiler does not do anything specific but initializes the data members of any dummy value when the objects are created.

/* Program to show use of default constructor*/


#include<iostream.h>
#include<conio.h>
class student
{
    public:
             char name[10],clas[10];
             int rol,age;
   student()
   {
    cout<<"\n Enter student name, age, roll number and class\n ";
    cin>>name>>age>>rol>>clas;
    cout<<"\n R.No.\tAge\tName\tClass";
    cout<<"\n "<<rol<<"\t"<<age<<"\t"<<name<<"\t"<<clas;
     }
};
main()
{
      class student s;
      getch();
      }
output of program to display student details using default constructor in C++ (OOPs)


Parameterized Constructor

A constructor with arguments is called a Parameterized Constructor. With the help of such constructor the data elements of various objects can be initialized with different values. This is performed by passing different values to arguments of the constructor function while creating an object.

/* Program to show use of Parameterized Constructor */


#include<iostream.h>
#include<conio.h>
class student
{
      public:
      student(char name[10],int age,int rol,char clas[10])
      {
           cout<<"\n R.No.\tAge\tName\tClass";
           cout<<"\n "<<rol<<"\t"<<age<<"\t"<<name<<"\t"<<clas;
                   }
};
main()
{
      char name[10],clas[10];
      int age,rol;
      cout<<"\n Student name, age, roll number and class";
      cin>>name>>age>>rol>>clas;
      class student s(name,age,rol,clas);
      getch();
      }
output of program to show student details using parameterized constructors in C++ (OOPs)


Copy Constructor

It is the form   class_name (class_name &) and used for the initialization of an object from another object of same type. A Copy Constructor takes a reference to an object of the same CLASS as itself as a parameter.

/* Program to show use of Copy Constructor*/


#include<iostream.h>
#include<conio.h>
class operation
{
      public:
             int a,b,c,d,e;
      operation()
      {
                 cout<<"\n Enter any interger value of a and b";
                 cin>>a>>b;
                 c=a+b;
                 cout<<"\n Addition is "<<c;
                 }
      operation(operation &o)
      {
           d=o.c;
           e=d*10;
           cout<<"\n On multiplication with 10 answer is "<<e;
                          }
};
main()
{
      class operation o1;
      operation o2(o1);
      getch();
      }
output of program to perform arithmetic operation using copy constructor in C++ (OOPs)
            

Constructor Overloading

/* Program to overload arithmetic operators using constructors*/

#include<iostream.h>
#include<conio.h>
class operation
{
      public:
             int a,b,c,e;
             float d;
      operation()
      {
            cout<<"\n Enter any interger value of a and b";
            cin>>a>>b;
            c=a+b;
            cout<<"\n Addition of "<<a<<" and "<<b<<" is "<<c;
                 }
      operation(int a,float b)
      {
           d=a-b;
           cout<<"\n Subtraction of "<<b<<" from "<<a<<" is "<<d;
                    }
      operation(operation &o)
      {
           e=(o.c);
           int f=e*10;
           cout<<"\n\n Multiplication of "<<e<<" and 10 is "<<f;
                          }
};
main()
{
      int a;
      float b;
      class operation o1;
      operation o3(o1);
      cout<<"\n\n Enter any interger and real value";
      cin>>a>>b;
      operation o2(a,b);
      getch();
      }
output of program to perform arithmetic operation using constructor overloading in C++ (OOPs)
                          


DESTRUCTORS

These are the functions that are complimentary to constructors. These are used to re-initialize objects when they are destroyed. A Destructor is called when an OBJECT of the CLASS goes out of scope, or when the memory space used by it is de-located with the help of delete operator.
Basic thing about Destructors
1. Destructor re-initializes the value of constructors.
2. Destructor can’t be overloaded.
PROPERTIES OF Destructors
1. Destructor functions are evoked automatically when the objects are destroyed.
2. Destructor may not be static.
3. Destructor can’t be inherited.
4. Destructor does not take any value and also does not return any value.
5. An object of a CLASS with a destructor can’t become member of UNION.
6. Member function may be called or accessed within the destructor.
7. It is not possible to take address of destructor.
8. No argument is provided to destructor.
9. Destructor level also access 3 access levels private, protected and public similar to constructors.
10. If a CLASS has a destructor, each object of that class will be re-initialized before the object goes out of scope.

/* Program to show use of destructor*/

#include<iostream.h>
#include<conio.h>
class stud
{
      public:
             char name[10],clas[10];
             int age;
      stud()
      {
            cout<<"\n Enter student name, age and class";
            cin>>name>>age>>clas;
            cout<<"\n Age\tName\tClass";
            cout<<"\n "<<age<<"\t"<<name<<"\t"<<clas;
            }
      ~stud()
      {
          cout<<"\n\n Now this is the application of Destructor";
          cout<<"\n\n Enter student name, age and class";
          cin>>name>>age>>clas;
          cout<<"\n Age\tName\tClass";
          cout<<"\n "<<age<<"\t"<<name<<"\t"<<clas;
          }
};
main()
{
      class stud s;
      getch();
      }
output of program to display student details using destructor and its use in C++ (OOPs)

1 comment:

  1. * Description of Destructors in C++
    * Characteristics of Destrutors
    * Programs for knowing use of destructors

    http://geeksprogrammings.blogspot.in/2013/10/destructor-in-cplusplus.html

    ReplyDelete