Constructor Destructor CONSTRUCTOR v Constructors are special member
Constructor & Destructor
CONSTRUCTOR v Constructors are special member functions whose task i to initialize the object of its class. v It is special because its name is the same as the class name. v The Constructor is invoked whenever an object is create v It is called constructor because it constructs the values of data members of the class.
SPECIAL CHARACTERISTICS: • They should be declared in the public section • They are invoked automatically when the objects are created • They do not have return types • They can be inherited though derived class • Like C ++ functions, they can have default arguments • Constructor cannot be virtual • We cannot refer their addresses • An object with a constructor cannot be used as a member of union • They make ‘implicit calls’ to the operator new and delete when memory allocation is required
EXAMPLE: Class integer { int m, n; public: integer (void); //constructor declared …. …. }; integer : : integer(void)//constructor defined { m=0; n=0; }
TYPES OF CONSTRUCTORS Constructors are of three types : 1. Default Constructor 2. Parameterized Constructor 3. Copy Constructor Dynamic Constructor
DEFAULT CONSTRUCTOR Default constructor is the constructor which doesn't take any argument. It has no parameter. EXAMPLE: Class rectangle { Private: Float length, breadth; Public: rectangle() …… …… }; rectangle: : rectangle() { cout<<“I am in the constructor”; Length=10. 0; Breadth=20. 5; }
PARAMETERIZED CONSTRUCTOR C++ permit us to achieve this objective by passing arguments to the constructor function when the objects are created. The constructor that can take arguments are called parameterized constructors. Example: class integer { int m, n; public: integer(int x, int y); //parameterized constructor } Integer : : integer(int x , int y) { m=x ; n=y; } We can have any number of parameters in a constructor.
COPY CONSTRUCTOR These are special type of Constructors which takes an object as argument, and is used to copy values of data members of one object into other object. EXAMPLE: Class fun { Float x , y; Public: fun(float a, float b) { x=a; y=b; } fun(fun &f) { Cout<<“ncopy constructor at workn”; x=f. x; y=f. y; }
DYNAMIC CONSTRUCTOR: Dynamic constructor is used to allocate memory to the objects at run time. Memory is allocated at run time with help of ‘new’ operator. we can dynamically initialize the objects using dynamic constructor
Destructors Destructor is a special class function which destroys the object as soon as the scope of object ends. The syntax for destructor is same as that for the constructor, the class name is used for the name of destructor, with a tilde ~ sign as prefix to it. Example class A { public: ~A(); }; Destructors will never have any arguments.
Special characteristics: • They are invoked automatically when the objects are no longer required. • They cannot be inherited • No arguments can be provided to the destructor , neither does it return any value • They cannot be virtual • We cannot refer to their addresses • They cannot be static
Program to use concept of constructor and destructor #include<iostream. h> #include<conio. h> Class con_des { Public: con_des() { Cout <<”constructor”<<endl; } ~con_des() { Cout <<”destructor”<<endl; } }; Void main() { Clrscr(); Cout<<”nt constructor & Destructor using classes and objects”<<endl; con_des a; con_des b; getch(); }
OUTPUT: Constructor &destructor using classes and objects CONSTRUCTOR DESTRUCTOR
MULTIPLE CONSTRUCTOR • A single class have more than one constructor • The compiler chooses which constructor to invoke by the signature of the constructor (the number and type of arguments) • If no arguments are given, then the default constructor is used
MULTIPLE CONSTRUCTOR IN A CLASS Example: Class integer { int m, n; Public: integer() //constructor 1 { m=0; n=0; } integer(int a, int b) //constructor 2 { m=a; n=b; } Integer(integer & i) //constructor 3 { m=i. m; n=i. n; } };
- Slides: 15