What is Constructor overloading, Examples and purpose of constructor overloading in C++, OOP
What is Constructor overloading? Give Examples and purpose of constructor overloading in C++ and OOP?
What is Constructor overloading?
Different constructors with the same name is called constructor overloading.
The constructor must obey one or both of the following rules.
- All constructors with the same name have a different number of parameters.
- For example numbers_sum(int n1, int n2) and another constructor as numbers_sum(int n1, int n2, int n3) is legal.
- All constructors with the same name and have the same number of parameters but of different data types is also legal.
- numbers_sum(int n1, int n2) and another constructor as numbers_sum(float n1, float n2).
Examples of legal and illegal constructor overloading
- numbers_sum(int n1, int n2) and numbers_sum(double n1, double n2) is legal in constructor overloading.
. - numbers_sum(int n1, int n2) and numbers_sum(int n1, double n2) is legal in constructor overloading.
- numbers_sum(int n1, int n2) and numbers_sum(double n1, int n2) is legal in constructor overloading.
- numbers_sum(int n1, int n2) and numbers_sum(int n1, int n2) is illegal in constructor overloading.
- numbers_sum(double n1, double n2) and numbers_sum(double n1, double n2) is illegal in constructor overloading.
- numbers_sum(int n1) and numbers_sum(int n1, int n2) is legal in constructor overloading.
- numbers_sum(int n1, int n2) and numbers_sum(int n1) is legal in constructor overloading.
Example of constructor overloading
Example of constructor overloading with a different number of parameters and constructors with the same name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<iostream> using namespace std; class numbers_sum { public: numbers_sum(int n1, int n2) { cout<<"sum of 2 numbers is = "<<endl<<n1+n2<<endl; } numbers_sum(int n1, int n2, int n3) { cout<<"sum of 3 numbers is = "<<endl<<n1+n2+n3<<endl; } }; int main() { numbers_sum a(3,4); numbers_sum b(5,5,2); } |
Output
Sum of two numbers is =
7
the sum of three numbers is =
12
Example of constructors with the same name and have the same number of parameters but of different data types.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<iostream> using namespace std; class numbers_sum { public: numbers_sum(int n1, int n2) { cout<<"sum of 2 numbers is = "<<endl<<n1+n2<<endl; } numbers_sum(double n1, double n2) { cout<<"sum of 2 numbers is = "<<endl<<n1+n2<<endl; } }; int main() { numbers_sum a(3,4); numbers_sum b(1.5,2.5); } |
Output
Sum of two numbers is =
7
the sum of three numbers is =
10
Another Example of Constructor Overloading In C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /* Constructor Overloading In C++ https://t4tutorials.com */ #include<iostream> #include<conio.h> using namespace std; class Example { // Variable Declaration int n1, n2; public: //Constructor wuithout Argument Example() { // Assign Values In Constructor n1 = 3; n2 = 7; cout << "\nIm Constructor"; } //Constructor with Argument Example(int x, int y) { // Assign Values In Constructor n1 = x; n2 = y; cout << "\nI am Constructor"; } void Display() { cout << "\nValues :" << n1 << "\t" <<"+"<< n2; cout<<"="<<n1+n2; } }; int main() { Example Object(33, 7); Example Object2; // Constructor invoked. Object.Display(); Object2.Display(); // Wait For Output Screen getch(); return 0; } |
Output
I am Constructor
Im Constructor
Values :33 +7=40
Values :3 +7=10
Constructor Outside the Class
The Syntax for Constructor Outside the Class
class class_name {
public:
//Constructor declaration
class_name();//… other Variables & Functions
}// Constructor definition outside Class
class_name::class_name() {
// Constructor code
}
Program of Constructor Outside the Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /* Constructor Outside the Class https://t4tutorials.com */ #include <iostream> #include<conio.h> using namespace std; // Class Declaration class T4Tutorials { int a, b; //Access - Specifier public: //Constructor declaration T4Tutorials(); //Member Functions for display 'a & b' Values. void Display() { cout << "Values :" << a << "\t" << b; } }; // Constructor definition outside Class T4Tutorials::T4Tutorials() { // Assign Values In Constructor a = 33; b = 7; cout << "This is Constructor Outside the Class\n"; } int main() { // Object Creation For Class T4Tutorials Object; // Constructor invoked. Object.Display(); // Wait For Output Screen getch(); return 0; } |
Copy Constructor
A copy constructor is just like a normal Constructor with the parameters, but the parameter is the same class object. The Copy constructor is helpful to initialize an object using another object of the same class.
C++ Program of Copy Constructor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | /* Of Copy Constructor Overloading In C++ https://T4Tutorials.com */ #include<iostream> #include<conio.h> using namespace std; class T4Tutorials { // Member Variable Declaration int a, b; public: //Normal Constructor with Argument T4Tutorials(int x, int y) { // Assign Values In Constructor a = x; b = y; cout << "\n I am Constructor"; } //Copy Constructor with Obj Argument T4Tutorials(const T4Tutorials& obj) { // Assign Values In Constructor a = obj.a; b = obj.b; cout << "\nI am Copy Constructor"; } void Display() { cout << "\nValues :" << a << "\t" << b; } }; int main() { //Normal Constructor Invoked T4Tutorials Object(10, 20); //Copy Constructor Invoked - Method 1 T4Tutorials Object2(Object); //Copy Constructor Invoked - Method 2 T4Tutorials Object3 = Object; Object.Display(); Object2.Display(); Object3.Display(); return 0; } |
What are the advantages of constructor overloading?
- It acts as compile-time polymorphism.
- Objects can be constructed in different ways.
- Helpful when we are programming the big problem because it is easy to remember the constructor name if many constructors are with the same name.
Constructor Destructor C++ Exercise with Solution
- Write a program in C++ to convert a decimal number into binary without using an array and using the constructor and destructor. – Solution
- Write a program in C++ to convert a decimal number into binary without using an array by using the constructor overloading. – Solution
- Write a c++ program to find out the sum of an A.P. series using constructor and destructor. – Solution
- Write a c++ program to find out the sum of an A.P. series by using the constructor overloading. – Solution
- Write a program in C++ to print Floyd’s Triangle by using the constructor destructor. – Solution
- Write C++ Program to display the cube of the number upto a given integer using Destructor. – Solution
- Write C++ Program to display the cube of the number upto a given integer using constructor overloading. – Solution
- Write a C++ program to find Strong Numbers within a range of numbers using constructor C++. – Solution
- Write a C++ program to find Strong Numbers within a range of numbers Using destructor in C++. – Solution
- Write a C++ program to find Strong Numbers within a range of numbers by using constructor overloading in C++. – Solution
- Let’s see the C++ program to show the Sum of n number of odd natural numbers by using the Constructor Overloading. – Solution
- Let’s see the Sum of n number of an odd natural number using constructor and destructor in C++. Solution
- Sum of the series Using Constructor Overloading in C++. Solution
- Sum of the series Using Destructor in C++. Solution
- Sum of the series Using Constructor in C++. Solution
- Write a program in C++ to find the sum of the series using the constructor overloading. Solution
- Write a program in C++ to find the sum of the series by using the constructor and destructor. Solution
- Write a program in C++ to print a pattern of right angle triangle with a number that will repeat a number in the row by using the constructor and destructor. Solution
- Write a program in C++ to print a pattern of right angle triangle with a number that will repeat a number in the row by using the constructor overloading. Solution
- Write a C++ program to display Pascal’s triangle using the Constructor Destructor. Solution
- Write a C++ program to display Pascal’s triangle using the Constructor Overloading. Solution
- C++ program of Constructor Destructor to display a pattern for a number of rows. Solution
- C++ program Constructor Destructor program to print the hollow square or rectangle star pattern. Solution.
- C++ program to display the diamond-like pattern using the Constructor Destructor. Solution
- C++ program to display the diamond-like pattern using the Constructor Overloading. Solution
- Write a program in C++ to convert an octal number into binary using constructor and destructor. Solution
- Write a program in C++ to convert an octal number into binary using constructor overloading. Solution
- C++ Program with constructor destructor to display the pattern like pyramid. Solution
- Write a C++ Program to display the reverse of a number using the constructor destructor. Solution
- Write a C++ Program to display the reverse of a number using the constructor overloading. Solution
- Write a C++ program to print rhombus star pattern of Number of rows using constructor overloading and destructor. Solution
- Write a program in C++ to Check Whether a Number can be Express as Sum of Two Prime Numbers using constructor and destructor. Solution
- Write a program in C++ to find the sum of the series using constructor destructor. Solution
- Write a C++ program of binary to octal conversion with Constructor with constructor. Solution
- Write the Octal to Decimal number program in C++ using constructor overloading and destructor? Solution
- Write a program in C++ to make such a pattern like a pyramid with a number which will repeat the number in the same row using constructor overloading and destructor. Solution
- Write a C++ program to find the number and sum of all integer between 100 and 200 which are divisible by 9 with constructor destructor. Solution
- Constructor Fibonacci series C++ Program. Solution
Test Your Understandings |
Test Your Understandings |
1. Constructor overloading means different function name same number of parameters and with same data types? YES / NO
2. int sum(int a, int b) and another constructor is sum(int a, int b). Here constructor is overloaded? YES / NO
3. int sum(int a, int b) and another constructor is sum(int a, int b, int c). Here constructor is overloaded? YES / NO
Topic Covered
What is Constructor overloading, Examples and purpose of constructor overloading in C++, OOP.