CONSTRUCTORS AND DESTRUCTORS Constructors: A constructor is a member function whose name is same as class name and is used to initialize data members and allocate memory dynamically. A constructor is automatically executed whenever objects are created. Class Class-Name Function-Name ( ); //Variable and Function Declaration //Constructor (Class name and Function should be as same) Class integer integer( ); //constructor Characteristics of Constructor: 1. Constructors should be declared in public section. 2. They are involved automatically when the objects are created. 3. They do not have return types. 4. They cannot be inherited. TYPES OF CONSTRUCTORS 1. Parameterized Constructors 2. Constructor with default Arguments 3. Copy Constructors 4. Dynamic Constructors M.EZHILVENDAN [AP/IT] [Jawahar Engineering College - Chennai] Page 1
1. Parameterized Constructors: The constructor with arguments is called parameterized constructor. Example : Class integer //Class Class-Name int m,n; //Variable Declaration integer(int x,int y) //Function Definition with some parameter m=x; n=y; ; 1.1. By calling the constructor explicitly: Class-Name Object-Name = Function-Name (Arguments); integer int1=integer(0,100); 1.2. By calling the constructor implicitly: Class-Name Object-Name(Arguments); integer int1(0,100); 2. Constructor with default Arguments The constructor with no arguments is called default constructor Class integer int m,n; Public: Integer( );. ; // Class Class-Name // Variable Declaration; //Function Declaration (Function not having arguments) M.EZHILVENDAN [AP/IT] [Jawahar Engineering College - Chennai] Page 2
integer::integer( ) //default constructor No parameter or Arguments Pass //Function Definition outside the Class m=0; n=0; 3. Copy Constructors A copy constructor is used to declare and initialize an object from another object. It takes a reference to an object of the same class as an argument. class code //Class Class-Name int id; //Variable Declaration code(); //Default Constructor code(int a)id=a; //Parameter Constructor code(code &x) //Copy Constructor Constructor Argument call as an another constructor id=x.id; ; 4. Dynamic Constructors It is used for allocation of memory to a variable at run time. M.EZHILVENDAN [AP/IT] [Jawahar Engineering College - Chennai] Page 3
Example Program For Constructor // Constructor Types #include <iostream.h> class Distance int feet, int inch; Distance() // Default constructor feet = 0; inch = 0; Distance (int n, int d = 0) // Parameterized constructor feet = n; inch = d; Distance (const Distance &a) // Copy constructor feet = a.feet; inch = a.inch; void print() cout << feet: <<feet << " ' "<< /t << inch: <<inch << " "<< \n"; ; int main() Distance d1, d2(4), d3(22,7); Distance d4(d2); d1.print(); d3.print(); d4.print(); Output: Feet: 0 inch: 0 Feet: 22 inch: 7 Feet: 4 inch: 0 M.EZHILVENDAN [AP/IT] [Jawahar Engineering College - Chennai] Page 4
Destructors A destructor is used to destroy objects when they go out of scope. Like constructor, destructor has the same name as class, but proceeded by a tilde ~. A destructor never takes any arguments nor does it return. There can be only one destructor for a class. Any memory allocated dynamically using constructor should be released using delete in the destructor. ~mystring(); // Constructor and Destructor #include<iostream.h> #include<conio.h> class add int a,b,c; add() cout<<"enter the value of A: "; cin>>a; cout<<"enter the value of B: "; cin>>b; c=a+b; ; ~add() cout<<"the value of A is: "<<a<<endl; cout<<"the value of B is: "<<b<<endl; cout<<"the sum of A and B is: "<<c<<endl; M.EZHILVENDAN [AP/IT] [Jawahar Engineering College - Chennai] Page 5
void main() clrscr(); add a1; getch(); Output Enter the value of A: 12 Enter the value of B: 15 The value of A is: 12 The value of B is: 15 The sum of A and B is: 27 M.EZHILVENDAN [AP/IT] [Jawahar Engineering College - Chennai] Page 6