C++ Constructor | Types of Constructor | Default, Parameterized, Copy and overloading of constructor | member function | My CS Tutorial
A class can include a special function called constructor, which is automatically called whenever a new object of this class is created. This constructor function must have the same name as the class,
and cannot have any return type; not even void.
Constructor is a special member function of a class that initializes the object of the class. Constructor name is same as class name and it doesn’t have a return type. Lets take a simple example to understand the working of constructor.
How to use constructor in C++ : Simple example :-
Read the comments in the following program to understand each part of the program.
#include <iostream>
using namespace std;
class constructorDemo{
public:
int num;
char ch;
/* This is a default constructor of the class, do note that it's name is same as class name and it doesn't have return type.
*/
constructorDemo() {
num = 100; ch = 'A';
}
};
int main(){
/* This is how we create the object of class, I have given the object name as obj, you can give any name, just remember the syntax:
* class_name object_name;
*/
constructorDemo obj;
/* This is how we access data members using object. We are just checking that the value we have initialized in constructor are reflecting or not.
*/
cout<<"num: "<<obj.num<<endl;
cout<<"ch: "<<obj.ch;
return 0;
}
Output:
num: 100
ch: A
Now that we know what is constructor, lets discuss how a constructor is different from member function of the class.
【1】 Constructor doesn’t have a return type. Member function has a return type.
【2】 Constructor is automatically called when we create the object of the class. Member function needs to be called explicitly using object of class.
【3】 When we do not create any constructor in our class, C++ compiler generates a default constructor and insert it into our code. The same does not apply to member functions.
This is how a compiler generated default constructor looks:
class XYZ
{
....
XYZ()
{
//Empty no code
}
};
There are two types of constructor in C++
【1】 Default constructor
【2】 Parameterized constructor
【3】 Copy constructor
【4】 Construtor overloading
A default constructor doesn’t have any arguments (or parameters)
#include <iostream>
using namespace std;
class Website{
public:
//Default constructor
Website() {
cout<<"Welcome to My CS Tutorial"<<endl;
}
};
int main(void){
/*creating two objects of class Website.
* This means that the default constructor
* should have been invoked twice.
*/
Website obj1;
Website obj2;
return 0;
}
Output:
Welcome to My CS Tutorial
Welcome to My CS Tutorial
When you don’t specify any constructor in the class, a default constructor with no code (empty body) would be inserted into your code by compiler.
A default constructor does not have any parameter, but if you need, a constructor can have parameters. This helps you to assign initial value to an object at the time of its creation.
OR
Constructors with parameters are known as Parameterized constructors. These type of constructor allows us to pass arguments while object creation. Lets see how they look:
Lets say class name is XYZ
Default constructor:
XYZ() {
}
....
XYZ obj;
....
Parameterized Constructor:
XYZ(int a, int b) {
}
...
XYZ obj(10, 20);
Example:
#include <iostream>
using namespace std;
class Add{
public:
//Parameterized constructor
Add(int num1, int num2) {
cout<<(num1+num2)<<endl;
}
};
int main(void){
/* One way of creating object. Also
* known as implicit call to the
* constructor
*/
Add obj1(10, 20);
/* Another way of creating object. This
* is known as explicit calling the
* constructor.
*/
Add obj2 = Add(50, 60);
return 0;
}
Output:
30
110
These qre special type of constructor which takes an object as arguments, and is used to copy values of data member of one objects into other object. We will study this constructor in detail later.
Like any other function, a constructor can also be overloaded with more than one function that have the same name but different types or number of parameters. Remember that for overloaded functions the compiler will call the one whose parameters match the arguments used in the function call. In the case of constructors, which are automatically called when an object is created, the one executed is the one that matches the arguments passed on the object declaration:
// overloading class constructors
#include <iostream>
using namespace std;
class CRectangle {
int width, height;
public:
CRectangle ();
CRectangle (int,int);
int area (void) {return (width*height);}
};
CRectangle::CRectangle () {
width = 5;
height = 5;
}
CRectangle::CRectangle (int a, int b) {
width = a;
height = b;
}
int main () {
CRectangle rect (3,4);
CRectangle rectb;
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
output:
rect area: 12
rectb area: 25
In this case, rectb was declared without any arguments, so it has been initialized with the constructor that has no
parameters, which initializes both width and height with a value of 5.
Important: Notice how if we declare a new object and we want to use its default constructor (the one without parameters), we do not include parentheses ():
CRectangle rectb; // right
CRectangle rectb(); // wrong!
C++ Constructor | Types of Constructor | Default, Parameterized, Copy and overloading of constructor | member function | My CS Tutorial
_______________________________________
Cconstructor in c++ | My CS Tutorial |
A class can include a special function called constructor, which is automatically called whenever a new object of this class is created. This constructor function must have the same name as the class,
and cannot have any return type; not even void.
Constructors in C++
Constructor is a special member function of a class that initializes the object of the class. Constructor name is same as class name and it doesn’t have a return type. Lets take a simple example to understand the working of constructor.
How to use constructor in C++ : Simple example :-
Read the comments in the following program to understand each part of the program.
#include <iostream>
using namespace std;
class constructorDemo{
public:
int num;
char ch;
/* This is a default constructor of the class, do note that it's name is same as class name and it doesn't have return type.
*/
constructorDemo() {
num = 100; ch = 'A';
}
};
int main(){
/* This is how we create the object of class, I have given the object name as obj, you can give any name, just remember the syntax:
* class_name object_name;
*/
constructorDemo obj;
/* This is how we access data members using object. We are just checking that the value we have initialized in constructor are reflecting or not.
*/
cout<<"num: "<<obj.num<<endl;
cout<<"ch: "<<obj.ch;
return 0;
}
Output:
num: 100
ch: A
Constructor vs Member function
Now that we know what is constructor, lets discuss how a constructor is different from member function of the class.
【1】 Constructor doesn’t have a return type. Member function has a return type.
【2】 Constructor is automatically called when we create the object of the class. Member function needs to be called explicitly using object of class.
【3】 When we do not create any constructor in our class, C++ compiler generates a default constructor and insert it into our code. The same does not apply to member functions.
This is how a compiler generated default constructor looks:
class XYZ
{
....
XYZ()
{
//Empty no code
}
};
Types of Constructor in C++
There are two types of constructor in C++
【1】 Default constructor
【2】 Parameterized constructor
【3】 Copy constructor
【4】 Construtor overloading
【1】 Default Constructor
A default constructor doesn’t have any arguments (or parameters)
#include <iostream>
using namespace std;
class Website{
public:
//Default constructor
Website() {
cout<<"Welcome to My CS Tutorial"<<endl;
}
};
int main(void){
/*creating two objects of class Website.
* This means that the default constructor
* should have been invoked twice.
*/
Website obj1;
Website obj2;
return 0;
}
Output:
Welcome to My CS Tutorial
Welcome to My CS Tutorial
When you don’t specify any constructor in the class, a default constructor with no code (empty body) would be inserted into your code by compiler.
【2】 Parameterized Constructor
A default constructor does not have any parameter, but if you need, a constructor can have parameters. This helps you to assign initial value to an object at the time of its creation.
OR
Constructors with parameters are known as Parameterized constructors. These type of constructor allows us to pass arguments while object creation. Lets see how they look:
Lets say class name is XYZ
Default constructor:
XYZ() {
}
....
XYZ obj;
....
Parameterized Constructor:
XYZ(int a, int b) {
}
...
XYZ obj(10, 20);
Example:
#include <iostream>
using namespace std;
class Add{
public:
//Parameterized constructor
Add(int num1, int num2) {
cout<<(num1+num2)<<endl;
}
};
int main(void){
/* One way of creating object. Also
* known as implicit call to the
* constructor
*/
Add obj1(10, 20);
/* Another way of creating object. This
* is known as explicit calling the
* constructor.
*/
Add obj2 = Add(50, 60);
return 0;
}
Output:
30
110
Copy Constructor
These qre special type of constructor which takes an object as arguments, and is used to copy values of data member of one objects into other object. We will study this constructor in detail later.
Overloading Constructors
Like any other function, a constructor can also be overloaded with more than one function that have the same name but different types or number of parameters. Remember that for overloaded functions the compiler will call the one whose parameters match the arguments used in the function call. In the case of constructors, which are automatically called when an object is created, the one executed is the one that matches the arguments passed on the object declaration:
// overloading class constructors
#include <iostream>
using namespace std;
class CRectangle {
int width, height;
public:
CRectangle ();
CRectangle (int,int);
int area (void) {return (width*height);}
};
CRectangle::CRectangle () {
width = 5;
height = 5;
}
CRectangle::CRectangle (int a, int b) {
width = a;
height = b;
}
int main () {
CRectangle rect (3,4);
CRectangle rectb;
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
output:
rect area: 12
rectb area: 25
In this case, rectb was declared without any arguments, so it has been initialized with the constructor that has no
parameters, which initializes both width and height with a value of 5.
Important: Notice how if we declare a new object and we want to use its default constructor (the one without parameters), we do not include parentheses ():
CRectangle rectb; // right
CRectangle rectb(); // wrong!
C++ Constructor | Types of Constructor | Default, Parameterized, Copy and overloading of constructor | member function | My CS Tutorial
_______________________________________
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment