 A constructor is a “special” member function whose
task to initialize the objects of its class.
 Its special because Its name is the same as the class
name.
 The constructor is invoked whenever an object of its
associated class is created.
 It is called constructor.
 //class with a constructor
Class integer
{
int m,n;
Public:
Integer(void); //constructor declared
……………..
…………..
};
Integer::integer(void) //constructor defined
{
m=0;n=0;
}
#include<iostream.h>
Class line
{
Public:
Void setlength(void);
Void getlength(void);
Line();
Private:
Double length;
};
Line::line(void)
{
Cout<<“object is being created”;
}
Void line::setlength(void)
{
Double l=getlength();
Cout<<“length”<<l;
}
Double line::getlength(void)
{
Cout<<“enter length”;
Cin>>length;
Return length;
}
int main()
{
Line ln;
Ln.setlength();
Return 0;
}
 A constructor that accepts no parameters is called
default constructor.
 The default constructor for class A is A::A().
 If no such constructor is defined then the compiler
supplies a default constructor.
 They should be declared in the public section
 They are invoked automatically when the object are
created
 They do not have return types ,not even void and there
for and they cannot return value
 We cannot refer to the addresses.
 The constructor integer (),defined above initializes the
data members of all the objects to zero
 To initialize the various data elements of different
object and different values are created
 The constructors that can take arguments are called
parameterized constructors.
 Class integer
{
Int m,n;
Public:
Integer (int x,int y);
……….
};
Integer::integer(int x,int y);
{
M=x;n=y;
}
#include<iostream.h>
Class line
{
Public:
Void setlength(void);
Double getlength(void);
Line(double len)
Private:
Double length;
};
Line::line(double len)
{
Cout<<“object is being created,length=“<<len;
}
Void line::setlength(void)
{
Double l;
L=grtlength();
Cout<<“length:”<<l;
}
Double line::getlength(void)
{
Cout<<“enter length:”;
Cin>>length;
Return length;
}
Int main()
{
Line line(10.0);
Line .setlength(6.0);
}
 Two ways:
 By calling the constructor explicitly
Integer int1=integer(0,100);
The statement create on integer object int 1 and passes
the values 0 and 100
by calling the constructor implicitly
integer int 1 (0,100);
 A reference variable has been used as an argument to
the copy constructor. we cannot argument by value to a
copy constructor Integer (integer & i);
 A copy constructor is used to declare and initialize an
object from another object
Integer i2(i1);
the object i2 and the same time initialize to the value of
i1
Integer i2 =i1;
#include<iostream.h>
Class code
{
Int id;
Public:
Code()
{}
Code(int a)
{
id=a;
}
Code(code &x)
{Id=x.id;}
Void display(void)
{
Cout<<id;
}
};
Int main()
{
Code A(100);
Code B(A);
Code C=A;
Code D;
D=A;
cout<<“n id of A:”;
A.display();
cout<<“n id of B:”;
B.display();
cout<<“n id of C:”;
C.display();
cout<<“n id of D:”;
D.display();
Return 0;
}
 The constructor can also be used to allocate memory
while creating object
 To allocate the right amount of memory each object ,
the object are not of the same size in the saving
memory
 Allocation of memory to object the construction is use
known as dynamic constructor
 The program uses two constructors the first is on
empty constructor to declare on array of string
 The second constructor initialize the length of the
string .space allocated to hold the end of string
character”o”.
THANK YOU

constructors

  • 2.
     A constructoris a “special” member function whose task to initialize the objects of its class.  Its special because Its name is the same as the class name.  The constructor is invoked whenever an object of its associated class is created.  It is called constructor.
  • 3.
     //class witha constructor Class integer { int m,n; Public: Integer(void); //constructor declared …………….. ………….. }; Integer::integer(void) //constructor defined { m=0;n=0; }
  • 4.
    #include<iostream.h> Class line { Public: Void setlength(void); Voidgetlength(void); Line(); Private: Double length; };
  • 5.
    Line::line(void) { Cout<<“object is beingcreated”; } Void line::setlength(void) { Double l=getlength(); Cout<<“length”<<l; }
  • 6.
    Double line::getlength(void) { Cout<<“enter length”; Cin>>length; Returnlength; } int main() { Line ln; Ln.setlength(); Return 0; }
  • 8.
     A constructorthat accepts no parameters is called default constructor.  The default constructor for class A is A::A().  If no such constructor is defined then the compiler supplies a default constructor.
  • 9.
     They shouldbe declared in the public section  They are invoked automatically when the object are created  They do not have return types ,not even void and there for and they cannot return value  We cannot refer to the addresses.
  • 10.
     The constructorinteger (),defined above initializes the data members of all the objects to zero  To initialize the various data elements of different object and different values are created  The constructors that can take arguments are called parameterized constructors.
  • 11.
     Class integer { Intm,n; Public: Integer (int x,int y); ………. }; Integer::integer(int x,int y); { M=x;n=y; }
  • 12.
    #include<iostream.h> Class line { Public: Void setlength(void); Doublegetlength(void); Line(double len) Private: Double length; };
  • 13.
    Line::line(double len) { Cout<<“object isbeing created,length=“<<len; } Void line::setlength(void) { Double l; L=grtlength(); Cout<<“length:”<<l; }
  • 14.
    Double line::getlength(void) { Cout<<“enter length:”; Cin>>length; Returnlength; } Int main() { Line line(10.0); Line .setlength(6.0); }
  • 15.
     Two ways: By calling the constructor explicitly Integer int1=integer(0,100); The statement create on integer object int 1 and passes the values 0 and 100 by calling the constructor implicitly integer int 1 (0,100);
  • 16.
     A referencevariable has been used as an argument to the copy constructor. we cannot argument by value to a copy constructor Integer (integer & i);  A copy constructor is used to declare and initialize an object from another object Integer i2(i1); the object i2 and the same time initialize to the value of i1 Integer i2 =i1;
  • 17.
  • 18.
    Void display(void) { Cout<<id; } }; Int main() { CodeA(100); Code B(A); Code C=A; Code D;
  • 19.
    D=A; cout<<“n id ofA:”; A.display(); cout<<“n id of B:”; B.display(); cout<<“n id of C:”; C.display(); cout<<“n id of D:”; D.display(); Return 0; }
  • 20.
     The constructorcan also be used to allocate memory while creating object  To allocate the right amount of memory each object , the object are not of the same size in the saving memory  Allocation of memory to object the construction is use known as dynamic constructor
  • 21.
     The programuses two constructors the first is on empty constructor to declare on array of string  The second constructor initialize the length of the string .space allocated to hold the end of string character”o”.
  • 22.