You are on page 1of 23

CONSTRUCTORS

Presented by: Er. Simarpreet Kaur


Subject: Programming in C++
Definition
• Constructors are special class functions which performs
initialization of every object. The Compiler calls the
Constructor whenever an object is created. Constructors
initialize values to object members after storage is
allocated to the object.
• Constructors can be defined either inside the class
definition or outside class definition using class name
and scope resolution :: operator.
Cont..
• The constructor is invoked whenever an object
of its associated class is created.
• It is called constructor because it constructs the
values of data members of the class.
• There is no need to write any statement to
invoke the constructor function.
• If a ‘normal’ member function is defined for
zero initialization, we would need to invoke
this function for each of the objects separately.
Syntax and Example
• The syntax generally is as given below:
<class name> {arguments};
• The default constructor for a class X has the
form X::X( )
• Example1:
class A
{
int x;
public:
Example 2
class add  When a class contains a
{ constructor, it is guaranteed that
int m, n ; an object created by the class will
public : be initialized automatically.
add (void) ; add a ;
------
 Not only creates the object a of
};
add :: add (void) type add but also initializes its data
members m and n to zero.
{
m = 0; n = 0;
}
Characteristics of Constructors
• They should be declared in the public section.
• They are invoked automatically when the objects are created.
• They do not have return types, not even void and they cannot
return values.
• They cannot be inherited, though a derived class can call the
base class constructor.
• Like other C++ functions, Constructors can have default
arguments.
• Constructors can not be virtual.
• We can not refer to their addresses.
• An object with a constructor (or destructor) can not be used as a
member of a union.
• They make ‘implicit calls’ to the operators new and delete when
memory allocation is required.
Types of Constructor
• Constructors are of three types :
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
Default Constructor
• This constructor has no arguments in it.
• Default Constructor is also called as no argument
constructor.
• Example: class creature
{
private:
int yearofBirth;
public:
creature( )
{
cout<<obj;
Parameterized Constructors
• It may be necessary to initialize the various
data elements of different objects with
different values when they are created.
• This is achieved by passing arguments to the
constructor function when the objects are
created.
• The constructors that can take arguments are
called parameterized constructors.
Example 1
• class Creature
{
private:
int yearOfBirth;
public:
// …
Creature(int year) { //Parameterized
Constructor yearOfBirth = year;
}
Example2

class add  When a constructor is


{ parameterized, we must pass
int m, n ; the initial values as arguments
public : to the constructor function
add (int, int) ; when an object is declared.
------  Two ways Calling:
}; o Explicit
add : : add (int x, int y)  add sum = add(2,3);
{ o Implicit
m = x; n = y;  add sum(2,3)
}  Shorthand method
Copy Constructor
•A copy constructor is used to declare and
initialize an object from another object.
integer (integer & i) ;
integer I 2 ( I 1 ) ; or integer I 2 = I 1 ;
The process of initializing through a copy
constructor is known as copy initialization.
•The statement
I 2 = I 1;
will not invoke the copy constructor.
Cont..
• A reference variable has been used as an
argument to the copy constructor.
• We cannot pass the argument by value to a
copy constructor.
Example
• class abc
{
int a, b;
public:
abc(int x, int y)
{
a = x; b = y;
}
abc::abc(abc &p)
{
a = p.a;
Cont..
void showdata( )
{
cout << a << " " << b << endl;
}
};
int main( )
{
abc c1(10, 20);
abc c2(c1);
c1.showdata( );
c2.showdata( );
Constructors with Default
Arguments
• It is possible to define constructors with default
arguments.
• Consider complex (float real, float imag = 0);
– The default value of the argument imag is zero.
– complex C1 (5.0) assigns the value 5.0 to the real
variable and 0.0 to imag.
– complex C2(2.0,3.0) assigns the value 2.0 to real and
3.0 to imag.
• A::A()  Default constructor
• A : : A (int = 0)  Default argument
constructor

• The default argument constructor can be called


Constructor Overloading
• You can have more than one constructor in a
class, as long as each has a different list of
arguments.
class rectangle {
private:
float height;
float width;
int xpos;
int ypos;
public:
rectangle(float, float); // constructor
rectangle(); // another constructor
void draw(); // draw member function
void posn(int, int); // position member function
void move(int, int); // move member function
};
Cont..
rectangle::rectangle()
{
height = 10;
width = 10;
xpos = 0;
ypos = 0;
}

void main()
{
rectangle rc1(3.0, 2.0);
rectangle rc2();

rc1.draw();
rc2.draw();
}
Defining Copy Constructor is
important
• In the absence of a copy constructor, the C++
compiler builds a default copy constructor for
each class which is doing a member wise copy
between objects.
• Default copy constructors work fine unless the
class contains pointer data members ...
why???
example
#include <iostream.h>
#include <string.h>
class string {
private:
char *s;
int size;
public:
string(char *); // constructor
~string(); // destructor
void print();
void copy(char *);
};
void string::print()
{
Cont..
void string::copy(char *c)
{
strcpy(s, c);
}

void main()
{
string str1("George");
string str2 = str1; // default copy constructor

str1.print(); // what is printed ?


str2.print();

str2.copy("Mary");

str1.print(); // what is printed now ?


Defining Copy Constructor for
above example
class string {
private:
char *s;
int size;
public:
string(char *); // constructor
~string(); // destructor
string(const string&); // copy constructor
void print();
void copy(char *);
Cont...
string::string(const string& old_str)
{
size = old_str.size;
s = new char[size+1];
strcpy(s,old_str.s);
}

void main()
{
string str1("George");
string str2 = str1;
str1.print(); // what is printed ?
str2.print();
str2.copy("Mary");
str1.print(); // what is printed now ?
str2.print();
}

You might also like