Cpp-Constructors(copy,parameterised,dynamic)
Constructors
A constructor is a special member function which is used to initialize the object of its class. It is special because its name is same as the class name. The constructor is called when the object of related class is created. It is called constructor because it construct the values of data member of the class. A constructor is declared and defined as follows.
class integer
{
int x, y;
--------
--------
public:
integer();
---------
---------
};
integer::integer()
{
x=0;
y=0;
}
When we declared object of integer class.
For example.,
Integer int 1;
The constructor function is automatically called & the data members. X & Y are set to 0. A constructor that accept no parameters are called default constructor. Following are some characteristics of constructor function.
1) They should be declaring in the public section.
2) They are called automatically when the objects are created. They do not have return type not even void and they cannot return values.
3) They cannot be inherited though a derived class can call the base class constructor.
4) Like other C++ function they can have default arguments.
5) Constructors cannot be virtual.
6) We cannot refer to their addresses.
7) They make implicit calls to the operator new and delete when memory allocation is required.
1. Parameterized constructor –
The constructor that takes arguments are called parameterized constructor. We can call the parameterized constructor in two ways.
· By calling the function explicitly.
Ex., integer int 1=integer(10,20);
· By calling the constructor implicitly.
Ex., integer int 2=integer(50,100);
//program for parameterised constructor
#include<iostream>
class image
{
int m;
int n;
public:
integer(int,int);
void display()
{
cout<<"m="<<m;
cout<<"n="<<n;
}
};
integer::integer(int x, int y)
{
m=x;
n=y;
}
int main()
{
integer int1=integer(10,20);
int1.display();
integer int2(50,100);
int2.display();
return 0;
}
Constructors with default arguments.It is possible to define constructors with default arguments.
For example., complex(float real, float image=0);
The default value of the argument image is zero then the statement
Complex int 1(5.0);
It assigns the value 5.0 to the real variable and 0.0 to image variable. We can also assign new value for the default argument.
For example., complex int 2(5.0,3.5);
It assigns 5.0 to real and 3.5 to image.
2. Copy constructor –
A copy constructor is used to declare and initialize an object from another object. The process of initializing through a copy constructor is known as copy initialization.
For example., if I1 and I2 are the objects of class integer then the statement
int I2(I1);
Will define the object I2 and initialize it with the value of I1.
//program for copy constructor
#include<iostream>
class code
{
int id;
public:
code()
{
}
code(int a)
{
id=a;
}
code(code &x)
{
id=x.id;
}
void display()
{
cout<<id;
}
};
int main()
{
code A(100);
code B(A);
code C=A;
code D;
D=A;
cout<<"id of A=";
A.display();
cout<<"id of B=";
B.display();
cout<<"id of C=";
C.display();
cout<<"id of D=";
D.display();
return 0;
}
3. Dynamic constructor –
The constructors can also be used to allocate memory while creating objects due to this the right amount of memory space is allocated to each object. When the objects are not of same size dynamic constructor are used to save the memory. Allocation of memory to the objects at the time of their construction is known as dynamic construction of objects.
//program for daynamic constructor
#include<iostream>
#include<string.h>
class dynamic
{
char *name;
int length;
public:
dynamic()
{
length=0;
name=new char[length+1];
}
dynamic(char *s)
{
length=strlen(s);
name=new char[length+1];
strcpy(name,s);
}
void display()
{
cout<<name;
}
void join(dynamic &a, dynamic &b)
{
length=a.length+b.length;
delete name;
name=new char[length+1];
strcpy(name, a.name);
strcpy(name,b.name);
}
};
int main()
{
char *first="C++";
dynamic name1(first), name2("Programming"), n3("Language"),s1.s2;
s1.join(name1, name2);
s2.join(s1.name s);
name2.display();
name3.display();
s1.display();
s2.display();
return 0;
}
Comments
Post a Comment