You are on page 1of 23

CONSTRUCTORS

• Constructor is a special member function, it is used to initialize object data members and allocate the necessary resources to them.

Rules to be followed to construct constructor:

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

Syntax class classname { ………… ………… public: classname() { //constructor body } };

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.

• For example object declaration statement is: Test obj1;
• In the parameterized constructor declare the object by using the first method( ie. Calling constructor explicitly) is, Test obj1=Test( 50,100); //explicit call • By calling the constructor implicitly then the object declaration is Test obj1(50, 100); //implicit call

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 data‟s 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‟

• Another form of this statement is Integer obj2=obj1;
• The process of initializing through a copy constructor is known as copy initialization. • “A copy constructor takes a reference to an object of the same class as itself as an argument.”

#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(); }

Output: Id of A object is:100 Id of B object is:100 Id of C object is:100 Id of D object is:100

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 }

• Rules for defining the destructor
– The destructor name is same as the class name but prefixed by a tilde(~).

– 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.

• Operator overloading is an method to achieve POLYMORPHISM concept
• Defining operator overloading:

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

Unary operator overloading
• The operator which takes only one operand for performing the job is called unary operator. • Consider the unary minus ( - ) operator : Normally this operator can be used change the sign of an operand when applied to a basic data item: – The normal unary operator can be overloaded by using the special member function called operator op ( ). – The overloaded unary operator can be applied to an object. • The unary „minus‟ when applied to an object should change the sign of each data members

#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 over : : display( ) { cout<< x<<endl<<y<<endl<<z; }

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

BINARY OPERATOR OVERLOADING
• • The operator takes two operand to perform its operation is called binary operator. Consider the example of binary operator „+‟, which will be used for adding two data member normally. Now using overloading process we can overload the „+ „operator by operator op special member function. Then the overloaded binary operator can be used for adding the “object”( user defined types) Rules for overloading binary operator: – The binary overloaded operator function takes the first object as implicit operand and the second operand must be passed explicitly. – The data members of the first objects are accessed without using the dot operator. – The second argument members can be accessed using the dot operator is the argument is an object.

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

The operators which cannot be overloaded:
1. 2. 3. 4. size of operator (sizeof) scope resolution operator ( : : ) conditional opearetor ( ? :) member access operator ( . )