MSc, Department of Bioinformatics, KSAWU, Vijayapura 2
Karnataka State Women's University
VIJAYAPUR
INTRODUCTION
Constructor is the special type of member function in
C++ class, which are automatically invoked when an
object is being created.
It is special because its name is same as the class name.
Class A
{
public:
int x;
public:
A(); //Constructor
};
Constructer can be defined either inside the class
definition or outside class definition using class name
and the scope of resolution :: operator
Class A
{
int number;
public:
A(); // Constructor declared
};
A::A() // Constructor definition
{
number=1;
}
1. Default Constructors or Zero argument constructor
2. Parameterized Constructors
3. Copy Constructor
“Default constructor is the constructor which doesn’t take
any argument.”
Or
“A constructer that accepts no parameter is known as default
constructer”
Syntax
Student
{
Student()
{
}
};
Example :
Class Cube
{
public :
int side ;
public :
Cube ()
{
side 10;
}
};
Int main ()
{
Cube c;
cout <<“nValue is : “ << c. side;
Output :
Value is : 10
…………………………………………...
Process excited after o.o148 seconds with
return value 0
Press any key to continue . . .
 “A constructer that receives arguments / parameters , is called
parameterized constructer “
 Construct with parameter is called parameterized constructer
 All the constructor executes at the time of object creation
 When we will create parameterized constructor then we have to pass
argument value at the time of object creation of that class
Syntax
Class Student
{
Student (int a , int b ) // parameterized constructor
{
// Statements
}
};
EXAMPLE
Class student
{
public :
int Roll ;
String Name;
float Marks;
puclic :
Student (int r, string nm,float m)
{
Roll = r;
Name = nm;
Marks = m;
}
Parameterized
Constructor
with three
parameters
Void Display ()
{
cout<<“n Roll is : “<<Roll;
cout<<“n Name is : “<<Name;
cout<<“n Marks is : “:Marks;
}
}; // end of class
Int main()
{
Student S( 2, “Justin”, 90);
S.Display(); //Displaying Student
Details
return 0;
}
OUTPUT
Roll is : 2
Name is : Justin
Marks is : 90
……………………………………………………….
Process excited after o.o1708 seconds with return value 0
Press any key to continue . . .
 These are special type of constructors which takes an object as
argument and is used to copy values of data member of one object
into other object
 Initialization of an object through another object is called copy
constructor .
 In other words, copying the value of one object into another object is
called copy constructor.
Syntax
Class Class_Name {
Constructor_name(class_name & obj_name)
{
}
};
Class Student
{
Int Roll;
string Name;
float Marks;
Public:
Student(int r, string nm, float m)
{
Roll = r;
Name = nm;
Marks = m;
}
Student (student & S)
{
Roll = S.Roll;
Name = S. Name;
Marks = S. Marks;
}
constructor 1 :
Parameterize
Constructor
Constructor 2:
Copy
Constructor
EXAMPLE
Void Display ()
{
cout << “n Roll:” <<Roll;
cout << “n Name:” << Name;
cout << “n Marks:”<<Marks;
}
};
Int main ()
{
Student S1(2, “Justin”,90);
Student S2 (S1); // Statement 1
cout <<“n Values in objects S1”;
S1. Display();
cout <<“n.......................”;
cout<<“n Values in object S2”;
S2.Display();
OUTPUT
Value in object S1
Roll : 2
Name : Justin
Marks : 90
……………………………………………….
Value in object S2
Roll : 2
Name : Justin
Marks : 90
……………………………………………….
Process excited after o.o4381 seconds with return value 0
Press any key to continue . . .
To initialize data member of class:
In the constructor member function
(which will be declared by the programmer) we can initialize the
default vales to the data members and they can be used further for
processing.
To allocate memory for data member:
Constructor can also be used to declare
run time memory (dynamic memory for the data members).
Constructor is used for:
@geeks for geeks
Object-Oriented Programming with ANSI and
Turbo C++ by Ashok Kamthane
Publisher: Pearson India Release Date: July
2006 ISBN: 9788131703830
REFERENCES :
C++Constructors

C++Constructors

  • 2.
    MSc, Department ofBioinformatics, KSAWU, Vijayapura 2 Karnataka State Women's University VIJAYAPUR
  • 4.
    INTRODUCTION Constructor is thespecial type of member function in C++ class, which are automatically invoked when an object is being created. It is special because its name is same as the class name. Class A { public: int x; public: A(); //Constructor };
  • 5.
    Constructer can bedefined either inside the class definition or outside class definition using class name and the scope of resolution :: operator Class A { int number; public: A(); // Constructor declared }; A::A() // Constructor definition { number=1; }
  • 7.
    1. Default Constructorsor Zero argument constructor 2. Parameterized Constructors 3. Copy Constructor
  • 8.
    “Default constructor isthe constructor which doesn’t take any argument.” Or “A constructer that accepts no parameter is known as default constructer” Syntax Student { Student() { } };
  • 9.
    Example : Class Cube { public: int side ; public : Cube () { side 10; } }; Int main () { Cube c; cout <<“nValue is : “ << c. side;
  • 10.
    Output : Value is: 10 …………………………………………... Process excited after o.o148 seconds with return value 0 Press any key to continue . . .
  • 11.
     “A constructerthat receives arguments / parameters , is called parameterized constructer “  Construct with parameter is called parameterized constructer  All the constructor executes at the time of object creation  When we will create parameterized constructor then we have to pass argument value at the time of object creation of that class Syntax Class Student { Student (int a , int b ) // parameterized constructor { // Statements } };
  • 12.
    EXAMPLE Class student { public : intRoll ; String Name; float Marks; puclic : Student (int r, string nm,float m) { Roll = r; Name = nm; Marks = m; } Parameterized Constructor with three parameters
  • 13.
    Void Display () { cout<<“nRoll is : “<<Roll; cout<<“n Name is : “<<Name; cout<<“n Marks is : “:Marks; } }; // end of class Int main() { Student S( 2, “Justin”, 90); S.Display(); //Displaying Student Details return 0; }
  • 14.
    OUTPUT Roll is :2 Name is : Justin Marks is : 90 ………………………………………………………. Process excited after o.o1708 seconds with return value 0 Press any key to continue . . .
  • 15.
     These arespecial type of constructors which takes an object as argument and is used to copy values of data member of one object into other object  Initialization of an object through another object is called copy constructor .  In other words, copying the value of one object into another object is called copy constructor. Syntax Class Class_Name { Constructor_name(class_name & obj_name) { } };
  • 16.
    Class Student { Int Roll; stringName; float Marks; Public: Student(int r, string nm, float m) { Roll = r; Name = nm; Marks = m; } Student (student & S) { Roll = S.Roll; Name = S. Name; Marks = S. Marks; } constructor 1 : Parameterize Constructor Constructor 2: Copy Constructor
  • 17.
    EXAMPLE Void Display () { cout<< “n Roll:” <<Roll; cout << “n Name:” << Name; cout << “n Marks:”<<Marks; } }; Int main () { Student S1(2, “Justin”,90); Student S2 (S1); // Statement 1 cout <<“n Values in objects S1”; S1. Display(); cout <<“n.......................”; cout<<“n Values in object S2”; S2.Display();
  • 18.
    OUTPUT Value in objectS1 Roll : 2 Name : Justin Marks : 90 ………………………………………………. Value in object S2 Roll : 2 Name : Justin Marks : 90 ………………………………………………. Process excited after o.o4381 seconds with return value 0 Press any key to continue . . .
  • 19.
    To initialize datamember of class: In the constructor member function (which will be declared by the programmer) we can initialize the default vales to the data members and they can be used further for processing. To allocate memory for data member: Constructor can also be used to declare run time memory (dynamic memory for the data members). Constructor is used for:
  • 20.
    @geeks for geeks Object-OrientedProgramming with ANSI and Turbo C++ by Ashok Kamthane Publisher: Pearson India Release Date: July 2006 ISBN: 9788131703830 REFERENCES :

Editor's Notes

  • #10 Side= 10 is intilized at the int side by the instance variable
  • #18 In this example, Statement 1 is creating an object S2 and passing another object S1 as parameter to the constructor 2. Constructor 2 will take the reference of object S1 passed by the statement 1 and copy all the values of object S1 to data members associated to the object S2.