Constructors in C++
C++ provides a special member function called constructor . A constructor initializes an instance(object) of its class.
It is special because its name same as the class.
Constructor does'nt have return type,even void.
Its invoked automatically when the object is created.
Like other C++ functions, constructor can also have default arguments.
Constructor can be overload.
Constructors may have any accessibility, public, protected or private.
Note If you don't define any constructors, the compiler will generate a default constructor that takes no parameters; you can override this behavior by declaring a default constructor as deleted.
#include<iostream> using namespace std; class Test { public: int x; //Constructor Test( ){ cout<<"CONSTRUCTOR CALLED"<< endl; x = 30; } }; int main() { Test ob; cout<<"VALUE OF X = "<< ob.x; return 0; }OUTPUT:
CONSTRUCTOR CALLED VALUE OF X = 30
Types of Constructors :
Constructors are of three types:
- Default Constructor
- Parameterized Constructor
- Copy Constructor
Default constructors have no parameters; they follow slightly different rules:
Default constructors are one of the special member functions; if no constructors are declared in a class, the compiler provides a default constructor:
#include<iostream> using namespace std; class Test { public: int x; }; int main() { /*This will call compiler generated constructor */ Test ob; //OR Test obj(); ob.x = 30; cout<<"VALUE OF X = "<< ob.x; return 0; }
You can define default constructor in your class as well.
#include<iostream> using namespace std; class Test { public: int x; //Default Constructor Test( ){ cout<<"DEFAULT CONSTRUCTOR"<< endl; x = 30; } }; int main() { Test ob; cout<<"VALUE OF X = "<< ob.x; return 0; }
Note:If any non-default constructors are declared, the compiler does not provide a default constructor.
#include<iostream> using namespace std; class Test { public: int x; //Non-Default Constructor Test(int m_x){ x = m_x; } }; int main() { Test ob; //ERROR Test ob1(20); return 0; }
Note:If a class has no default constructor, an array of objects of that class cannot be constructed by using square-bracket syntax alone. For example, given the previous code block, an array of 'Test' cannot be declared like this.
Test ob[5];//ERROR
However, you can use a set of initializer lists to initialize an array of Test:
Test ob[3]{20,50,100};Code Example:
#include<iostream> using namespace std; class Test { public: int x; //Non-Default Constructor Test(int m_x){ x = m_x; } void display() { cout<<"x = "<< x<< endl; } }; int main() { Test ob[3]{20,50,100}; ob[0].display(); ob[1].display(); ob[2].display(); return 0; }OUTPUT:
VALUE OF X = 20 VALUE OF X = 50 VALUE OF X = 100
2. Parameterized Constructor :
Constructor with argument is called parameterized constructor.
Using this constructor you can provide different valued to data members of different objects, by passing the appropriate values as argument.
#include<iostream> using namespace std; class Test { int x; public: //Default Constructor Test(){ x = 30; } //Parameterized Constructor Test(int m_x){ x = m_x; } void display() { cout<<"x = "<< x<< endl; } }; int main() { //using default constructor Test ob; //using parameterized constructor Test ob1(50); ob.display(); ob1.display(); return 0; }OUTPUT:
VALUE OF X = 30 VALUE OF X = 50
3. Copy Constructor :
Its a special type of constructor which takes an object of same class as argument.
It is used to copy values of data members of one object into other object.
We use reference ( & )type of argument to receive the object in copy constructor.
#include<iostream> using namespace std; class Test { public: int x; int y; //Default Constructor Test(){ x = 10; y = 20; } //Copy Constructor Test(Test &ref){ x = ref.x; y = ref.y; } }; int main() { Test ob1; ob1.y = 13; //copy constructor calling Test ob2(ob1); Test ob3; cout<<"OB1’s X = "<< ob1.x<<" and Y = "<< ob1.y; cout<< endl; cout<<"OB2’s X = "<< ob2.x<<" and Y = "<< ob2.y; cout<< endl; cout<<"OB3’s X = "<< ob3.x<<" and Y = "<< ob3.y; cout<< endl; return 0; }OUTPUT:
OB1’s X = 10 and Y = 13 OB2’s X = 10 and Y = 13 OB3’s X = 10 and Y = 20
Next topic is Destructor
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in