Constructor, Operator Overloading
Constructor, Operator Overloading
Constructor, Operator Overloading
Constructor is a special member function, it is used to initialize object data members and allocate the necessary resources to them.
1. 2. 3. 4.
Constructor should be declared in the public section They are invoked automatically when the objects are created. They do not have return value specification, not even void is allowed. It has the same name as that of the class to which it belongs
Types of constructors
1. Default constructors 2. Parameters constructors 3. Copy constructors
#include<iostream.h> class test { int m1,m2; public: test() { m1=50; m2=25; } test(int x,int y) { m1=x; m2=y; } void display() { cout<<m1<<m2; } }; void main() { test t1; //default constructor called cout<<Initialized data members; t1.display(); test t2(50,100); cout<<Initialized object 2 data members; t2.display(); } //constructor with args defined
Two ways of calling parameterized constructor as: By calling the constructor explicitly. By calling the constructor implicitly.
Multiple constructors in a class: Multiple kinds of constructor can be used in the program such as,
1. 2. No argument constructor ( eg: test() ) With argument constructor ( eg: test ( int, int) )
Constructor Overloading:
The class can have multiple constructors and all the constructors have the same name as the corresponding class And they differ only in terms of their number of arguments, or data types of their argument or both, is called constructor overloading.
class account { public: account(); account(int an); account(int acvalue,float bal); }; accout acc1; account acc2(45); account acc3(324,675.50);
COPY CONSTRUCTOR
A copy constructor is used to declare and initialize an object from another object (ie, all the datas should be copied from one object to another object)
For example:
integer obj2(obj1);
The above statement defines the object obj2 and at the same time initializes or copies the values of obj1
#include<iostream.h> class code { int id; public: code() { } code( int a) { id=a; } code( code &x) { id=x.id; } }; void main( ) { code A(100); code B(A); code C=A; code D; D=A;
//copy constructor
cout<< Id of A object is:; A.display(); cout<< Id of B object is:; B.display(); cout<< Id of C object is:; C.display(); cout<< Id of D object is:; D.display(); }
DESTRUCTOR
When an object is no longer needed it can be destroyed. A class can have another special member function called destructor, which is called when the object is destroyed. Destructor is a member function having the character ~(tilde) followed by the name of its class and brackets. It should be declared as follows, ~classname( ); The destructor can be called automatically when the object goes out of scope and is no longer needed.
General format class classname { .. // private members public: ~classname( ); // destructor prototype or declaration }; class name :: ~ classname( ) { // destructor body definition }
Unlike constructor, destructor does not take any arguments The destructor has no return type like the constructor, since it is invoked automatically whenever the object goes out of scope. There can be only one destructor in each class. The overloading is not possible. Destructor must be declared in the public section of a class, so that it is accessible to all its users.
#include< iostream.h> int count =0; class test { public: test( ); ~test ( ); }; test::test( ) { count ++; cout<< object << count<<constructor of class test is called: } test::~test( ) { cout<< object << count<< Destructor of class test is called: count--; } void main() { test x; // constructor called { test y; //constructor called } }
Output: object 1 constructor of class test is called object 2 constructor of class test is called object 2 destructor of class test is called object 1 destructor of class test is called
OPERATOR OVERLOADING
Operator overloading is the process of making an operator to perform different task. The operator overloading is a mechanism of giving special meaning to an operator. The operator overloading is one of the exciting feature for enhancing the capability of an operator.
To define an additional task to an operator, we must specify what it means in relation to the class to which the operator is applied. This is done with the help of a special function called operator function which describe the task.
General format of operator function: return type class name :: operator op (arg list) { // function body; }
In this argument list, the member function has no argument for unary operators and only one for binary operators. Types of operator overloading:
1. 2. Unary operator overloading Binary operator overloading
#include<iostream.h> class over { int x, y, z; public: void setdata( int a,int b, int c); { x= a; y= b; z= c; } void operator ( ); void display( ); }; void over : : operator ( ) { x = -x; y = -y; z = -z; } //defining operator member function
void main( ) { over u; u. setdata( 20,-50,30); cout<<before calling unary function; u. display( ); - u; cout<<after calling unary function; u.display( ); } Run: before calling unary function 20 -50 30 after calling unary function -20 50 -30
class complex { float x,y; public: void setdata( float r, float i) { x= r; y= i; } complex operator + ( complex c2) { complex temp; temp. x= x + c2. x; temp. y= y + c2. y; return(temp); } void display( ) { cout<<x << +i<<y; } };
void main( ) { complex c1,c2 , c3; c1. setdata( 10, 3.5); c2. setdata( 5.5, 2.5); c3= c1 + c2; cout<< Ist complex number:; c1. display( ); cout<< IInd complex number:; c2. display ( ); cout<< added complex number:; c3. display ( ); } RUN: Ist complex number 10 + i 3.5 IInd complex number: 5.5 + i 2.5 added complex number 15. 5 + i 6.0