Example : A simple C++ program to show the Constructor concept.
#include<iostream>
using namespace std;

class construct
{
	public:     // Constructor Not private, always public
	construct() 
	{
	   cout<< "object is initialized automatically "<<endl;
	}
};

int main ()
{
	construct obj1;
	return 0;
}

Output :

object is initialized automatically

----------  OR  ------------

#include<iostream>
using namespace std;

class construct
{
        private:
	   int a,b,c;
        public:
	construct() 
	{
	   a=10;
	   b=30;
	   c=a+b;
	   cout<<"The output is = "<<c<<endl;
	}
};

int main ()
{
   construct obj1,obj2;
   return 0;
}

Output :
The output is = 40
The output is = 40

----------  OR  ------------

#include<iostream>
using namespace std;
class construct
{
	private:
		int a,b,c;
	public:
        construct() 
	{
		a=10;
		b=30;
		c=a+b;		
	}
	void output()
	{
		cout<<"The output is = "<<c<<endl;
	}
};

int main ()
{
	construct obj1,obj2;
	obj1.output();
	obj2.output();
	return 0;
}

Output :
The output is = 40
The output is = 40

----------  OR  ------------

#include<iostream>
using namespace std;

class construct
{
	private:
		int a,b,c;
	public:
        construct() 
	{
		a=10;
		b=30;
		c=a+b;
		output();		
	}
	void output()
	{
		cout<<"The output is = "<<c<<endl;
	}
};

int main ()
{
	construct obj1,obj2;	
	return 0;
}

Output :
The output is = 40
The output is = 40

----------  OR  ------------

#include<iostream>
using namespace std;

class construct
{
	private:
	   int a,b,c;
	public:   
	construct() 
	{
	   input();
	   process();
	   output();
	}
	void input()
	{
		cout<<"Enter two values: ";
		cin>>a>>b;
	}
	
	void process()
	{
	c=a+b;
	}
	
	void output()
	{
		cout<<"The result are :";
		cout<<c;
	}
};

int main ()
{
   construct obj1;
   return 0;
}
Example : A simple C++ program to show the Constructor & Destructor concepts.
#include<iostream>
using namespace std;

class construct
{
	private:
		int a,b,c;
	public:
        construct() 
	{
		a=10;
		b=30;
		c=a+b;
		cout<<"The output is = "<<c<<endl;		
	}	
	~ construct()
	{
		cout<<"Memory Deallocated"<<endl;
	}
};

int main ()
{
	construct obj1,obj2;	
	return 0;
}

Output :
The output is = 40
The output is = 40

Memory Deallocated
Memory Deallocated

NB: Destructor always executed automatically in the last whenever at least one object is initialized, no matter early location in the code and should be only one in the program.
Example : A C++ Program to show the Constructor Overloading concept.
#include <iostream>
using namespace std;

class Student
{
	string sname;
	int roll;
	float tfee;
	
	public:
		Student()
		{
			sname = "default constructor";
			cout << sname << endl;
		}		
		Student( string str )
		{
			sname = str;
			cout << sname << endl;
		}		
		Student( int roll1 , float tfee1 )
		{
			roll=roll1;
			tfee=tfee1;
			cout<<roll<<"  "<<tfee<<endl;
		}		
		Student( string str2 ,int roll2 , float tfee2 )
		{
			sname=str2;
			roll=roll2;
			tfee=tfee2;
			
			cout<<sname<<" "<<roll<<"  "<<tfee<<endl;
		}	
};

int main()
{
	Student stu;
	Student stu1( "codershelpline" );
	Student stu2(120,12.0123);
	Student stu3("Codershelpline",210,300.0123);		
	return 0;
}

Output :
default constructor
codershelpline
120  12.0123
Codershelpline 210  300.012

------------  OR  -------------

#include <iostream>
using namespace std;

class Student
{
	string sname;
	int roll;
	double tfee;
	
	public:
		Student()
		{
			sname = "Default Constructor";
                        cout<<sname<< endl;			
		}		
		Student(string str)
		{
			sname = str;
			cout << sname << endl;
		}		
		Student(int eroll)
		{
			roll=eroll;			
			cout<<roll<<endl;
		}		
		Student(double tfee2)
		{
			tfee=tfee2;			
			cout<<tfee<<endl;
		}	
};

int main()
{
	Student stu;
	Student stu1("Codershelpline");
	Student stu2(12);
	Student stu3(21.5424);		
	return 0;
}

Output:
Default Constructor
Codershelpline
12
21.5424
Example : A C++ Program to show the Default Constructor concept.
#include <iostream> 
using namespace std; 
  
class calculation 
{ 
public: 
    int x, y;   
    
    calculation()  //Deffult Constructor Called & Execute automatically when object is created.
    { 
        x = 22; 
        y = 310; 
	cout<<x<<" "<<y<<endl;
    } 
}; 
  
int main() 
{ 
    calculation obj; 
    cout <<"First Default Value is : " << obj.x <<endl; 
    cout <<"Second Default Value is : " << obj.y;		  
    return 0;     
} 

Output :
22 310
First Default Value is : 22
Second Default Value is : 310

------------------  OR  -----------------

#include <iostream> 
using namespace std;  
class calculation 
{ 
public: 
    int x, y;   
    
    calculation()  //Deffult Constructor
    { 
        x = 22; 
        y = 310; 
    }
    void display()
    {		
        cout<< "The values are ="<<x<<" "<<y;	
    } 
}; 
  
int main() 
{ 
    calculation obj;
    obj.display();    		  
    return 0;     
} 
Output :
The values are =22 310

------------------  OR  -----------------

#include <iostream>
using namespace std; 

class calculation
{    
    private:
        int num1;
        int num2;    
    public:
        
        calculation();    //External Default Constructor
        
        void input() 
        {
       	cout << endl<< "Enter First Value : "; 
	cin >> num1;
		
    	cout << "Enter Second Value : "; 
	cin >> num2;
	}	    
	   
        void output()
        {
        cout <<endl<<"First Value is : " << num1;
    	cout << endl <<"Second Value: " << num2 << endl;	
        }     
}; 

calculation:: calculation()
{
    num1 = 0;
    num2 = 0;
}
  
int main()
{
    calculation obj ;    
   
    cout <<"Value before user input : " ;
    obj.output();    
 
    obj.input();
    cout<<endl<<"Value after user Input : ";
    obj.output();      
     
    return 0;
}

Output :
Value before user input :
First Value is : 0
Second Value: 0

Enter First Value : 12
Enter Second Value : 10

Value after user Input :
First Value is : 12
Second Value: 10
Example : A C++ Program to show the Parameterized-Constructor concept.
#include <iostream> 
using namespace std; 
  
class calculation 
{ 
public: 
    int x, y;
    
    calculation(int x1, int y1)      //Parameterised Constructor .
    { 
        x = x1; 
        y = y1; 
    } 
    void display()
    {
    	cout<<x<<"  "<<y;
    }
}; 
  
int main() 
{ 
    calculation obj1(400,65);
    calculation obj2(12,35);
    calculation obj3(87,68);
    
   obj1.display();
   cout<<endl;
   
   obj2.display();
   cout<<endl;
   
   obj3.display();
		  
    return 0;     
} 

Output :
400  65
12  35
87  68

/* ------------------  OR  ----------------- */

#include <iostream> 
using namespace std; 
  
class calculation 
{ 
public: 
    int x, y;
    
    calculation(int x1, int y1);      //Define Constructor outside the class.
    
    void display()
    {
    	cout<<x<<"  "<<y;
    }
}; 

calculation :: calculation(int x1, int y1)     
    { 
        x = x1; 
        y = y1; 
    } 
  
int main() 
{ 
    calculation obj1(400,65);
    calculation obj2(12,35);
    calculation obj3(87,68);
    
   obj1.display();
   cout<<endl;
   
   obj2.display();
   cout<<endl;
   
   obj3.display();
		  
    return 0;     
} 

Output :
400  65
12  35
87  68

/* ------------------  OR  ----------------- */

#include <iostream> 
using namespace std; 
  
class calculation 
{ 
public: 
    int x, y;
    
    calculation(int x1, int y1)      //Parameterised Constructor .
    { 
        x = x1; 
        y = y1; 
    } 
};
  
int main() 
{ 
    calculation obj1(400,65);
    calculation obj2(12,35);
    calculation obj3(87,68);
    
    cout <<"First Parameterised Value is : " << obj1.x <<endl; 
    cout <<"Second Parameterised Value is : " << obj1.y<<endl;;
    
    cout <<endl<<"First Parameterised Value is : " << obj2.x <<endl; 
    cout <<"Second Parameterised Value is : " << obj2.y<<endl;;
    
    cout <<endl<<"First Parameterised Value is : " << obj3.x <<endl; 
    cout <<"Second Parameterised Value is : " << obj3.y;
		  
    return 0;    
}
 
Output :
First Parameterised Value is : 400
Second Parameterised Value is : 65

First Parameterised Value is : 12
Second Parameterised Value is : 35

First Parameterised Value is : 87
Second Parameterised Value is : 68
Example : A Program in C++ to show the Copy Constructor concept.
#include<iostream>
using namespace std;

class Example 
{   
   int a, b;
   public: 
   Example(int x, int y) 
   {
      a = x;
      b = y;      
      cout <<"\nParameterized Constructor values are = "<<a<<" "<<b;
   }   

   Example(Example &obj4) 
   {
      a = obj4.a;
      b = obj4.b;
      cout <<"\nCopy Constructor values are = "<<a<<" "<<b;
      // Here, a and b are the data members of obj2.
   }

   void display()
   {   	
   	cout <<"\nThe copied values are = "<<a<<" "<<b;
   }   
};

int main() 
{
   
   Example obj1(240, 500);
   
   Example obj2(obj1);
   
   Example obj3 = obj1;

   obj1.display();
   obj2.display();
   obj3.display();   
   
   return 0;
}

Output :
Parameterized Constructor values are = 240 500

Copy Constructor values are = 240 500
Copy Constructor values are = 240 500

The copied values are = 240 500
The copied values are = 240 500
The copied values are = 240 500

/* ------------------  OR  ----------------- */

//Factorial using copy constructor
#include<iostream>
using namespace std;

class Example 
{
    int val, fact;
    public:   

   Example(int temp) 
   {
      val = temp;
   }
   int factorial() 
   {
      fact = 1;
      for (int i = 1; i <= val; i++)
       {
         fact = fact * i;
       }
      return fact;
   }
};

int main() 
  {
   int n;
   cout << "\nEnter a value for factorial : ";
   cin>>n;
   
   Example obj1(n);
   
   Example obj2 = obj1;
   
   cout << "\n" <<"The factorial of "<< n << " is :" << obj1.factorial();
   cout << "\n" <<"The factorial of " << n << "after copy constructor is :" << obj2.factorial();
   
   return 0;
}

Output :
Enter a value for factorial : 5

The factorial of 5 is :120
The factorial of 5after copy constructor is :120
Example : A C++ Program to show the Destructor concept.
#include <iostream> 
using namespace std; 
  
class calculation 
{ 
public: 
    int x, y;
    
    calculation(int x1, int y1)      //Parameterised Constructor .
    { 
        x = x1; 
        y = y1; 
    } 
    
    ~calculation()     //Destructor Execution
    {
    	cout<<endl<<"Destructor destroy object's memory allocation";
    }
    
    void display()
    {
    	cout<<x<<"  "<<y;
    }
	
    calculation()      
    { 
        cout<<"Default Constructor\n" ;
    } 
   
}; 
  
int main() 
{ 
    calculation obj1(400,65);
    calculation obj2(12,35);
    calculation obj3(87,68);
    calculation obj4;
    
   obj1.display();
   cout<<endl;
   
   obj2.display();
   cout<<endl;
   
   obj3.display();
		  
    return 0;     
} 

Output :
Default Constructor
400  65
12  35
87  68
Destructor destroy object's memory allocation
Destructor destroy object's memory allocation
Destructor destroy object's memory allocation
Destructor destroy object's memory allocation

NB: Destructor always executed automatically in the last whenever at least one object is initialized, no matter early location in the code and should be only one in the program.

Loading

Categories: C++

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.