Table of Contents
hide
Example : A simple C++ program to show constructor concept.
#include<iostream>
using namespace std;
class construct
{
public: // Not private
construct()
{
cout<< "object is initialized automatically "<<endl;
}
};
int main ()
{
construct obj1,obj2;
return 0;
}
Output :
object is initialized automatically
object is initialized automatically
// ---------- OR ------------
#include<iostream>
using namespace std;
class construct
{
public:
int a,b,c;
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
{
public:
int a,b,c;
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
{
public:
int a,b,c;
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
Example : A simple C++ program to show constructor & destructor concept.
#include<iostream>
using namespace std;
class construct
{
public:
int a,b,c;
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
Example : A C++ Program to show constructor overloading concept.
#include <iostream>
using namespace std;
class Student
{
string sname;
int roll;
float tfee;
public:
Student()
{
sname = "default";
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
codershelpline
120 12.0123
Codershelpline 210 300.012
Example : A C++ Program to show 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;
}
};
int main()
{
calculation obj;
cout <<"First Default Value is : " << obj.x <<endl;
cout <<"Second Default Value is : " << obj.y;
return 0;
}
Output :
First Default Value is : 22
Second Default Value is : 310
/* ------------------ OR ----------------- */
#include <iostream>
using namespace std;
class calculation
{
public:
int x, y;
calculation()
{
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();
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 parameterised 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 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;
}
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 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 creation
{
cout<<endl<<"Destructor destroy object memory allocation";
}
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
Destructor destroy object memory allocation
Destructor destroy object memory allocation
Destructor destroy object memory allocation
0 Comments