Skip to main content

C++ Constructor

Constructor are used to assign initialize the primary value of data member of class. This are special member function. they will active when created relative object of class. So in this post we are learning everything about constructor.

Declare and define constructor

Constructor name is same as class name. and there are not any return type so we are easily to find and this is speciality of constructor.

/*
  Example of constructor declared and defined
*/
#include <iostream>
using namespace std;
//create class
class Student
{
public:
  //Data member
  int status,marks;
  /*
  Declared constructor of class.
  This is an inline constructor
  */
  Student(){
    //definition of constructor
    //logical code
    status=0;
    marks=0;
    cout<<"Initialize value";
  }
};

int main(){
  //Create object s1 of student class
  Student s1;
}
Output
Initialize value

Note that the constructor are invoked when we are created relative and associative object of class. and it will be automatically call. In this program we have create a inline constructor (defined and declared inside a class). We can also defined constructor in outside of class. see example.

/*
  Example How to Defined the constructor 
  outside of class.
*/
#include <iostream>
using namespace std;
//create class
class example{

  //Data member
  int data1,data2;

  public:
  //Declared constructor of class
  example();
};

//Defined the constructor outside of class
example::example(){

  cout<<"Constructor Are invoked"<<endl;

  //Initialize values
  data1=10,data2=20;
  //Display of data member value
  cout<<"data1 :"<<data1<<endl;
  cout<<"data2 :"<<data2;
}

int main(){
  //Create object obj of example class
  example obj;
}
Output
Constructor Are invoked
data1 :10
data2 :20

Important point

1) Constructor are only declare in public section of class. We can not declare constructor private and protected section. look at view the example.

/*
 Example Can't declare constructor in private
 and protected section of class.
*/
#include <iostream>
using namespace std;
//create class
class example{
  //change also private here
  protected:
  //Declared constructor of class
  example();
};
//Defined the constructor outside of class
example::example(){
  cout<<"Constructor Example";
}
int main(){
  //Create object obj of example class
  example obj;
}
Error
error: 'example::example()' is protected within this context
  example obj;

2) They are not inherited of any other class or friend class.

3) Constructor can overloaded. discussed in later

4) Constructor are not any return type. and they can not defined by virtual.

5) We can defined constructor inside of class and also outside of class.

//Inside a class
class foo{
  public:
  //Declared and defind constructor of class
  foo(){
    //code
  }
};

//Defined outside of class
class bar{
  public:
  bar();
}
bar::bar(){
  //code
}

Using of following syntax we have defined constructor in outside of class. here (::) is scope resolution operator

class_name :: constructor_name(paramter_list);

Parameterized Constructors

In previous example we was assign static value of data member using constructor. but there are possible to we assign different value of every object of class. In this case parameterized constructor are used. So how to declare parameterized.

//create class Student
class Student{
  //data member
public:
  //member function
  //defined  Parameterized constructor
  Student(int,int);
};

Note that in this case we are declared parameterized constructor of inside a class. this constructor are accept two integer parameter. See the another example.

/*
  Parameterized Constructors Example
*/
#include <iostream>
using namespace std;
//create class
class calculator{
  //Data member
  int no1,no2;
  public:
   //Member function
    calculator(int,int);
    int addition(calculator);
};
calculator ::calculator(int value1,int value2){
  //initialize initial value to data member
  no1=value1;
  no2=value2;
}
int calculator::addition(calculator obj){
  //return the sum of two number  
  //In this case passing object of addition function
  return obj.no1+obj.no2;
}

int main(){
  //Create object obj1,obj2 of calculator class
  calculator obj1(10,20); //passing value to constructor
  calculator obj2(50,20); //passing value to constructor

  //Calling function
  cout<<"Sum "<<obj1.addition(obj1)<<endl;
  cout<<"Sum "<<obj2.addition(obj2);

}
Output
Sum 30
Sum 70

Note that in above program we are create two object obj1 and obj2. observe that we are also pass two values in those object. In this program if we are not create parameterized object so it will not execute constructor.

 calculator obj;// not call parameterized constructor

Multiple Constructor

We can create multiple constructor in a class. c++ are allowing to overload constructors. so we can utilize those special feature. we can decide which object are utilize of specific constructor. look at this example.

//create class demo
class demo
{
public:
  demo(); //constructor are no arguments
  demo(int);//constructor with one integer arguments
  demo(int,int);//constructor with two integer arguments  
};

In this example there are create three constructor for demo class. first constructor are not accept any arguments that means this can be assign the constant value of data member. second constructor are accept one parameter. that means they can be assign one integer value to data member. and similarly third can assign two integer value of class data member. understand of this point using of given example

/*
Example multiple constructor in a class
*/
#include<iostream>
using namespace std;
//create class demo
class demo
{
  //data member 
  int x,y,z;
public:
  //member function
  demo(); //constructor are no arguments
  demo(int);//constructor with one integer arguments
  demo(int,int);//constructor with two integer arguments  
  void display(demo);//class function display
};
//simple constructor
demo::demo(){
  //assign default value
  x=0,y=0,z=0;
}
//one parameterized constructor
demo::demo(int data){
  //assign data value
  x=data;
  y=data;
  z=data;

}
//two intger parameterized constructor
demo::demo(int data1,int data2){
  //assign data1 and data2
  x=data1;
  y=data2;
  z=x+y;
}
void demo::display(demo obj){
  cout<<"x :"<<obj.x<<endl;
  cout<<"y :"<<obj.y<<endl;
  cout<<"z :"<<obj.z<<endl;
}
int main(){
  demo obj1;//call simple constructor (no arguments)
  demo obj2(10);//call one argument constructor
  demo obj3(20,30);//call two parameterized constructor

  //display data memeber value
  cout<<"Print obj1"<<endl;
  obj1.display(obj1);
  cout<<"Print obj2"<<endl;
  obj2.display(obj2);
  cout<<"Print obj3"<<endl;
  obj3.display(obj3);
}
Output
Print obj1
x :0
y :0
z :0
Print obj2
x :10
y :10
z :10
Print obj3
x :20
y :30
z :50

Note that we are declare only used three constructor of this class we can create any number of constructor using on requirement. and also possible to pass any data type value and objects.

Constructors With Default Argument

Interesting we can set default parameter of constructor. there is are similar of function default parameters. let as view an example to this situations.

/*
Example constructor With Default arguments
*/
#include<iostream>
using namespace std;
//create class computer
class computer{

  //data member 
  int amount,discount;
  public:
  //member function
  computer();
  computer(int,int);//constructor with two integer arguments  
  void price(computer);//class function price
};

computer::computer(){
   discount=0;
   amount=22000;
}
/*
 Two integer parameterized constructor.
 Default value for discount is 10.
 If we are pass two parameter then default value are replace.
*/
computer::computer(int amount,int discount=10){
  //Assign data member value
  this->amount=amount;
  this->discount=discount;
}
void computer::price(computer obj){
  //display computer price discount etc.
  cout<<"Actual Price :"<<obj.amount<<endl;
  cout<<"Discount :"<<obj.discount<<" % "<<endl;
  cout<<"Amount :"<<obj.amount-((obj.amount*obj.discount)/100)<<endl;
}
int main(){
  cout<<"Display obj1 Values"<<endl;
  computer obj1;
  obj1.price(obj1);

    cout<<"\nDisplay obj2 Values"<<endl;
  computer obj2(50000); //two parameterized constructor execute
  obj2.price(obj2);

    cout<<"\nDisplay obj3 Values"<<endl;
  computer obj3(70000,15);//two parameterized constructor execute
  obj3.price(obj3);
}
Output
Display obj1 Values
Actual Price :22000
Discount :0 %
Amount :22000

Display obj2 Values
Actual Price :50000
Discount :10 %
Amount :45000

Display obj3 Values
Actual Price :70000
Discount :15 %
Amount :59500

In this example we are use three object of class computer. obj1 are have default value that is assigned by parameter less constructor. and another two object are used to parameter constructors. In this example set one parameter default value is 10. that means second argument are not compulsory. obj2 are satisfied this condition. and if we can also provide both arguments. see obj3.

Observe that if parameter and data member name are same then we are used (this) pointer to data member.

Copy Constructor

We now about the constructor. they are initialize data member value. similarly copy constructor are used to declared and assign primary value to objects.

/*
Example of copy constructor
*/
#include<iostream>
using namespace std;
//create class dollar
class dollar{

  //data member 
  int amount;
  public:
  //member function
  dollar(int);//constructor
  
  dollar(dollar &);//declared copy constructor  
  void info();//class function 
};

dollar::dollar(int value){
  amount=value;
}
//Defined the copy constructor
dollar::dollar(dollar & obj){
  amount=obj.amount;
}
void dollar::info(){
  //display amount value
  cout<<"Amount :"<<amount<<endl;
}
int main(){
  dollar rupees1(75);
  //copy constructor are invoked
  dollar rupees2(rupees1);
  
  cout<<" rupees1 "<<endl;
  rupees1.info();

    cout<<" rupees2 "<<endl;
  rupees2.info();
}
Output
 rupees1
Amount :75
 rupees2
Amount :75





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment