C++ Constructor Destructor

Advertisements

C++ Constructor

We can't initialize class data members directly as given below:


	class Student
	{

		int Roll=1;                       // Error : Cannot initialize a class member here
		char Name[]="Kumar";   // Error : Cannot initialize a class member here
		float Marks=78.53;         // Error : Cannot initialize a class member here

	};

Constructor is a special function used to initialize class data members or we can say constructor is used to initialize the object of class.

Characteristics constructor.

  • Constructor name class name must be same.
  • Constructor doesn't return value.
  • Constructor is invoked automatically, when the object of class is created.

Types of Constructor

  • Default Constructor
  • Parameterize Constructor
  • Copy Constructor

C++ Default Constructor

Construct without parameter is called default constructor.

Example of C++ default constructor


	#include<iostream.h>
	#include<string.h>

	class Student
	{

					int Roll;
					char Name[25];
					float Marks;

					public:

					Student()           //Default Constructor
					{
						Roll = 1;
						strcpy(Name,"Kumar");
						Marks = 78.42;
					}

					void Display()
					{
						cout<<"\n\tRoll : "<<Roll;
						cout<<"\n\tName : "<<Name;
						cout<<"\n\tMarks : "<<Marks;
					}
	};


	void main()
	{

					Student S;          //Creating Object

					S.Display();        //Displaying Student Details

	}


	Output :

			  Roll : 1
			  Name : Kumar
			  Marks : 78.42

				

C++ Parameterize Constructor

Construct with parameter is called parameterize constructor.

Example of C++ parameterize constructor


	#include<iostream.h>
	#include<conio.h>
	#include<string.h>

	class Student
	{

		int Roll;
		char Name[25];
		float Marks;

		public:

		Student(int r,char nm[],float m)           //Parameterize Constructor
		{
			Roll = r;
			strcpy(Name,nm);
			Marks = m;
		}

		void Display()
		{
			cout<<"\n\tRoll : "<<Roll;
			cout<<"\n\tName : "<<Name;
			cout<<"\n\tMarks : "<<Marks;
		}
	};


	void main()
	{

		Student S(2,"Sumit",89.63);
		//Creating Object and passing values to Constructor


		S.Display();
		//Displaying Student Details

	}


	Output :

			  Roll : 2
			  Name : Sumit
			  Marks : 89.63

				

In parameterize constructor, we have to pass values to the constructor through object.

C++ Copy Constructor

Initialization of an object through another object is called copy constructor. In other words, copying the values of one object into another object is called copy constructor.

Example of C++ copy constructor


	#include<iostream.h>
	#include<conio.h>
	#include<string.h>

	class Student
	{

		int Roll;
		char Name[25];
		float Marks;

		public:

		Student(int r,char nm[],float m)   //Constructor 1 : Parameterize Constructor
		{
			Roll = r;
			strcpy(Name,nm);
			Marks = m;
		}

		Student(Student &S)   //Constructor 2 : Copy Constructor
		{
			Roll = S.Roll;
			strcpy(Name,S.Name);
			Marks = S.Marks;
		}

		void Display()
		{
			cout<<"\n\tRoll : "<<Roll;
			cout<<"\n\tName : "<<Name;
			cout<<"\n\tMarks : "<<Marks;
		}
	};


	void main()
	{

		Student S1(2,"Sumit",89.63);

		Student S2(S1);    //Statement 1

		cout<<"\n\tValues in object S1";
		S1.Display();

		cout<<"\n\tValues in object S2";
		S2.Display();

	}


	Output :

	Values in object S1
	Roll : 2
	Name : Sumit
	Marks : 89.63

	Values in object S2
	Roll : 2
	Name : Sumit
	Marks : 89.63

				

In the above example, Statement 1 is creating an object S2 and passing another object S1 as parameter to the constructor 2.

Constructor 2 will take the reference of object S1 passed by the statement 1 and copy all the values of object S1 to data members associated to the object S2.

C++ Destructor

Constructor allocates the memory for an object.
Destructor deallocate the memory occupied by an object. Like constructor, destructor name and class name must be same, preceded by a tilde(~) sign. Destructor take no argument and have no return value.

Constructor is invoked automatically when the object created.
Destructor is invoked when the object goes out of scope. In other words, Destructor is invoked, when compiler comes out form the function where an object is created.

Example of C++ destructor


	#include<iostream.h>
	#include<conio.h>
	#include<string.h>

	class Student
	{

		int Roll;
		char Name[25];
		float Marks;

		public:

		Student()   //Default Constructor
		{
			Roll = 4;
			strcpy(Name,"Sumit");
			Marks = 84.56;
		}

		void Display()
		{
			cout<<"\n\tRoll : "<<Roll;
			cout<<"\n\tName : "<<Name;
			cout<<"\n\tMarks : "<<Marks;
		}

		~Student()   //Destructor
		{
			cout<<"\n\tEnd of program.";
		}

	};


	void main()
	{

		Student S;

		S.Display();

	}


	Output :

	Roll : 4
	Name : Sumit
	Marks : 89.56

	End of program.

				

Advertisement