CH 4
CH 4
Parameterized Constructors
• C++ permits us to achieve this objective by passing arguments to the constructor function when the objects are
created.
• The constructors that can take arguments are called parameterized constructors.
e.g.
class integer
{
int m, n;
public:
integer(int x, int y); // parameterized constructor
…………..
…………..
};
integer :: integer(int x, int y)
{
m=x;
n=y;
}
• when a constructor has been parametrized, the object declaration statement such as –
integer int1;
may not work.
• We must pass the initial values as arguments to the constructor function when an object is declared.
• This can be done in two ways:
1. By calling the constructor explicitly
2. By calling the constructor implicitly
The declaration
integer I1;
Would automatically invoke the first constructor and set both m and n of I1 to zero.
integer I2(20,40);
Would call the second constructor which will initialize the data members m and n of I2 to 20 and 40 resp.
integer I3(I2);
would invoke the third constructor which copies the values of I2 into I3. Such constructor are called as copy constructor.
Note: when more than one constructor function is defined in a class, it is called as constructor is overloaded.
Cont..
Program:
#include <iostream>
using namespace std;
class ABC
{
private:
int x,y;
public:
ABC () //constructor 1 with no arguments
{
x = y = 0;
}
ABC(int a) //constructor 2 with one argument
{
x = y = a;
}
ABC(int a, int b) //constructor 3 with two argument
{
x = a;
y = b;
}
void display()
{
cout << "x = " << x << " and " << "y = " << y << endl;
Contd..
}
};
int main()
{
ABC cc1; //constructor 1
ABC cc2(10); //constructor 2
ABC cc3(10,20); //constructor 3
cc1.display();
cc2.display();
cc3.display();
return 0;
}
Output:
x = 0 and y = 0
x = 10 and y = 10
x = 10 and y = 20
// C++ program to illustrate the use of copy constructor
#include <iostream>
#include <string.h>
using namespace std;
// Class definition for 'student'
class student
{ int rno;
string name;
double fee;
public:
// Parameterized constructor
student(int, string, double);
// Copy constructor
student(student& t)
{
rno = t.rno;
name = t.name;
fee = t.fee;
cout << "Copy Constructor Called" << endl;
}
// Implementation of the parameterized constructor
// Function to display student details
void display();
};
rno = no;
name = n;
fee = f;
cout << rno << "\t" << name << "\t" << fee << endl;
}
int main()
s.display();
// Create another student object using the copy // constructor
student s1(s);
s1.display();
return 0;
}
Constructor with default arguments
• e.g.
complex( float real, float imag=0);
The default value for the imag is zero. Then the statement
complex c(5.0);
assigns the value 5.0 to the real variable and 0.0 to imag.
• However, the statement
complex c(2.0,3.0);
assign 2.0 to real and 3.0 to imag.
Cont…
Program:
#include<iostream>
using namespace std;
class sample
{
int n;
float avg;
public:
sample(int p,float q)
{
n=p;
avg=q;
}
void disp()
{
cout<<"\n Roll number:- " <<n;
cout<<"\nAverage :- "<<avg;
}
};
Cont…
int main()
{
int a ;
float b;
cout<<"\nEnter the Roll Number=>";
cin>>a;
cout<<"\nEnter the Average=>";
cin>>b;
Output:
Roll numbe:- 35
Average :- 567
Dynamic Constructors
• The constructor can be used to allocate memory while creating objects.
• This will enable the system to allocate the right amount of memory for each object when the objects are not of the same
size, thus resulting in the saving of memory.
• Allocate of memory to objects at the time of their construction is known as dynamic constructors of objects. The
memory is allocated with the help of new operator.
Destructors
• A destructor is used to destroy the objects that have been created by a constructor.
• Like, a constructor, the destructor is member function whose name is the same as the class name but is preceded by a
tilde.
e.g.
~ integer( )
{
}
• A destructor never takes any arguments nor does it return any value.
• It will be invoked implicitly by the complier upon exit from the program to clean up storage that is no longer accessible.
• It is good practice to declare destructors in a program since it releases memory space for future use.
Program
#include<iostream>
using namespace std;
int count=0;
class test1
{
public:
test1()
{
count++;
cout<<"\n\n Constrctor msg: Object number"<<count<<" created";
}
~test1()
{
cout<<"\n\n Disconstrctor msg: Object number"<<count<<" destroyed";
count--;
}
};
int main()
{
cout<<"Inside the main block...";
cout<<"\n\n Creating first object T1...";
Contd…
test1 T1;
cout<<"\n\n Inside block 1....";
cout<<"\n\n Creating two more objects T2 and T3...";
test1 T2,T3;
Output:
Inside the main block...
Leaving Block1...