Академический Документы
Профессиональный Документы
Культура Документы
Balagurusamy
Constructors
- A constructor is a special member function whose task is to initialize the objects of its
class.
- It is special because its name is same as the class name.
- 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.
- Like member function constructor can be defined within or outside of class.
- Example:-
class Integer{
int i;
public:
Integer( ){
i=0; Constructor definition with in class.
}
};
main( ){
Integer a; Invoking the constructor.
}
- A constructor that accepts no parameters are called the default constructor.
- If no default constructor is defined, then the compilers supplies default constructor.
- Special characteristics of Constructor are:
o These are called automatically when the objects are created.
o All objects of the class having a constructor are initialized before some use.
o These should be declared in the public section for availability to all the
functions.
o Return type (not even void) cannot be specified for constructors.
o These cannot be inherited, but a derived class can call the base class
constructor.
o These cannot be static.
o Default and copy constructors are generated by the compiler wherever
required. Generated constructors are public.
o These can have default arguments as other C++ functions.
o A constructor can call member functions of its class.
o A constructor can call member functions of its class.
o The make implicit calls to the memory allocation and deallocation operators
new and delete.
o These cannot be virtual.
Parameterized Constructor:
- The constructor that can take arguments are called parameterized constructor.
- Remember, when the constructor is parameterized, we must provide appropriate
arguments for the constructor.
- The parameters of a constructor can be any type except that the class to which it
belongs.
- However, a constructor can accept a reference to its own class as a parameter. In such
cases constructor is called copy constructor.
- Example:-
class Point{
int x,y;
public:
Point (int a, int b ){
x=a; Parameterized Constructor.
y=b;
}
}; Invoking the parameterized
main( ){ constructor implicitly.
Point a(10,20);
}
- When more than one constructor function is defined in a class, we say that constructor
is overloaded.
- Example:-
class Point{
int x,y;
public:
Point (int a, int b ){
x=a; Constructor Overloading .
y=b;
}
Point( ){
X=y=0;
}
}; Invoking constructors implicitly.
main( ){
Point b,a(10,20);
}
-
main(){
int td,tm,ty,bd,bm,by;
clrscr();
cout<<"Enter Todays Date\n" ;
cin>>td>>tm>>ty;
cout<<"Enter Date of birth\n";
cin>>bd>>bm>>by;
Date t(td,tm,ty),b(bd,bm,by),a;
a=t.age(t,b);
cout<<a.y<<"-years "<<a.m<<"-months "<<a.d<<" -days";
getch();
}
- Example:-
class Point{
int x,y;
public:
Point(int=0,int=0);
}; Constructor with default argument
Point::Point (int a, int b ){
x=a;
y=b;
}
main( ){
Point a;
Point(10); Invoking constructors implicitly.
Point(10,20);
}
main( ){
int a,b;
cout<< “Enter two points\n”;
cin>>a>>b;
Point p(a,b);
}
Copy Constructor
- A copy constructor is used to declare and initialize an object from another object.
- For Example, the statement
Integer i2(i1);
Would define the object i2 and at the same time initialize it to the value of i1.
- Another form of above statement is
Integer i2=i1;
- The process of initializing through a copy constructor is known as copy initialization.
- Remember the statement
i2=i1;
will not invoke the copy constructor.
- However, if i1 and i2 are objects, this statement (i2=i1) is legal and simply assigns the
values of i1 to i2. This is task of overloaded assignment operator.
class Integer{
int a;
public:
Integer( ){
a=100;
}
Integer(Integer &o){ Copy Constructor
a=o.a
}
void dispay( ){
cout<<a<<endl;
}
};
main( ){
Integer i1;
Integer i2=i1; Invoking Copy Constructor
i1.display( );
i2.display( );
}
Dynamic Constructors
- The constructors can be also used to allocate memory while creating objects.
- Allocation of memory to the objects at the time of their construction is called as
dynamic construction of objects.
- The memory is allocated with the help of new operator.
- Example:-
class Point{
int *x,*y;
public:
Point(int ,int);
void display( );
};
Point::Point (int a, int b ){
x=new int (a);
y=new int(b);
}
Point::display( ){
cout<<x<<y;
}
main( ){
int a,b;
Destructor.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Point{
int *x,*y;
char *nm;
public:
Point();
Point(int,int);
Point(Point &);
~Point();
void show();
};
Point::Point(){
x=new int(1);
y=new int(1);
nm=new char[6];
nm="NMIMS";
}
Point::Point(int a,int b){
x=new int (a);