1 chapter -3 constructor and destructors constructor : constructor is a special member function...

12
1 Chapter -3 CONSTRUCTOR AND DESTRUCTORS Constructor : Constructor is a special member function which enables an object to initialize itself when it is created. Important characteristics of constructor are It should be defined in public section only. It has same name as the name of the class. It is invoked automatically when an object of its associated class is created. Constructor does not return any value. We cannot refer to their address. An object with a constructor ( or destructor) cannot be used member of a union. Like other c++ functions, they can have default arguments.

Upload: naomi-wood

Post on 18-Jan-2016

237 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Chapter -3 CONSTRUCTOR AND DESTRUCTORS Constructor : Constructor is a special member function which enables an object to initialize itself when it is

1

Chapter -3CONSTRUCTOR AND DESTRUCTORS

Constructor :  Constructor is a special member function which enables an object to initialize itself when it is created. 

Important characteristics of constructor are It should be defined in public section only. It has same name as the name of the class. It is invoked automatically when an object of its associated class is created. Constructor does not return any value. We cannot refer to their address. An object with a constructor ( or destructor) cannot be used member of a union. Like other c++ functions, they can have default arguments.

 

Page 2: 1 Chapter -3 CONSTRUCTOR AND DESTRUCTORS Constructor : Constructor is a special member function which enables an object to initialize itself when it is

2

Syntax for declaration of constructor

class_name{

public:class_name( parameter list){

function body //constructor function

}~ class_name( parameter list)

{ function body // destructor function

}};

Types of constructors  The three basic types of constructor available in c++ are :

Default constructor. Parameterized constructor. Copy constructor.

Page 3: 1 Chapter -3 CONSTRUCTOR AND DESTRUCTORS Constructor : Constructor is a special member function which enables an object to initialize itself when it is

3

•Default constructor :-

A constructor that accepts no parameters is called default constructor.

Example :-

Class integer{ int m,n;Public:

integer() //default constructor{ m=10; n=10;}

};

When a class contains a constructor like the one defined above, it is clear that an object created by the class will be initialized automatically. The declaration

Integer int1; // object int1 is

created

Note only creates the object int1 but also initialized its data member m and n to 10.

Page 4: 1 Chapter -3 CONSTRUCTOR AND DESTRUCTORS Constructor : Constructor is a special member function which enables an object to initialize itself when it is

4

•Parameterized constructor :-

In default constructor every objects are initialized with the same values but in many cases, it may be necessary to initialize the various data elements of different object with different values when they are permits us to achieve this objective by passing argument to the construct 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 {

m=x;n=y;

} };

When a constructor has been parameterized, the object of declaration statement as

integer int1;

integer int1=integer(0,100);

This statement creates an integer objects int1 & passes the values 0 and 100 to it.

integer int1(0,100);

Page 5: 1 Chapter -3 CONSTRUCTOR AND DESTRUCTORS Constructor : Constructor is a special member function which enables an object to initialize itself when it is

5

•Constructor with default arguments :-

Constructor with default argument means an argument value is specified in constructor declaration which is used if the corresponding actual parameter value is omitted when constructor is invoked.

While declaring the function the arguments without default values are placed first and those with default values are placed later as the first or middle arguments can not be omitted.

The default arguments are specified in function prototype if function is defined later else those are given in function definition.

Syntax for default argument :

return_type function_name(data_type parameter_name = constant_value);

Example :

int test(int a, int b = 5); // function test is having argument b with default value.

Page 6: 1 Chapter -3 CONSTRUCTOR AND DESTRUCTORS Constructor : Constructor is a special member function which enables an object to initialize itself when it is

6

Page 7: 1 Chapter -3 CONSTRUCTOR AND DESTRUCTORS Constructor : Constructor is a special member function which enables an object to initialize itself when it is

7

•Copy constructor :-A constructor can accept a reference to its own class as a

parameter which is called as a copy constructor.Therefore the following constructor is valid

Class Time{……Public:Time(Time&); //copy constructor};

A copy constructor can be used to declare and initialized an object of another object.

For example, the statement

Time T2(T1);Would define the object T2 and at the same time initialize it to

the values of T1.The process of initializing through a copy constructor is known as

copy initialization. But the statementT2=T1;

Will not invoke the copy constructor. However , if T1 & T2 are

objects, this statement is legal and simply assigns the values of T1 to T2 member by member.

Page 8: 1 Chapter -3 CONSTRUCTOR AND DESTRUCTORS Constructor : Constructor is a special member function which enables an object to initialize itself when it is

8

//Example of copy constructor

#include<iostream.h>#include<conio.h>class sample{

int number;public:

sample(int x){number=x;}sample(sample &m) // copy constructor{number=m.number;}void display(){cout << "\n a = " << number;}

};main(){

clrscr();sample a(40);sample b(a);sample c=a;cout << "\n The data for a object \n";a.display();cout << "\n The data for b object \n";b.display();cout << "\n The data for c object \n";c.display();getch();

}

Page 9: 1 Chapter -3 CONSTRUCTOR AND DESTRUCTORS Constructor : Constructor is a special member function which enables an object to initialize itself when it is

9

•Destructor function :-

It is a special function just opposite constructor. The important characteristics of destructor are :-

1) Destructor function also has the same name as the class but preceded by a tilde (~).

2) When an object is out of scope destructor function is called automatically.

3) The destructor has no arguments and also does not return any value.

Syntax for declaration of destructor

class class_name{ public:

class_name( parameter list){

function body //constructor function }

~ class_name( parameter list) {

function body // destructor function }};

Page 10: 1 Chapter -3 CONSTRUCTOR AND DESTRUCTORS Constructor : Constructor is a special member function which enables an object to initialize itself when it is

10

Example of Destructor

#include<iostream.h>#include<conio.h>class sample{int a,b;public:sample(){ a=100; b=200; }~sample() { cout << "\n OBJECT IS DESTROYED "; }void display(){

cout << "\n a = " << a << "\n b = " << b; } };main(){

clrscr();sample x;x.display();sample y;y.display();cout << "\n The y object is destroyed";sample z;z.display();cout << "\n the z object is destroyed ";getch();

}

Page 11: 1 Chapter -3 CONSTRUCTOR AND DESTRUCTORS Constructor : Constructor is a special member function which enables an object to initialize itself when it is

11

•Operator overloading :-

Overloading unary and binary operators. :

It allows to extend functionality of an existing operator to operate on user defined data types also. The operation of operator when the operands are of user defined data types depends upon operator overloading done in program.

Unary operator overloading : Overloading of operators operating on single operand. There should be no arguments to the operator function.

Binary operator overloading : Overloading of operators operating on two

operands. There should be one parameter to the operator function.

Following operator can’t be overloaded :- 1) :: Scope resolution operator.2) Sizeof operator3) ?: conditional (ternary) operator4) * pointer to class member operator5) . dot operator

Page 12: 1 Chapter -3 CONSTRUCTOR AND DESTRUCTORS Constructor : Constructor is a special member function which enables an object to initialize itself when it is

12

•Syntax for declaration of operator function :-

For declaration of the operator overloading

return_ datatype operator operator_to_overload(arguments);

e.g.

int operator + (test t); // operator function to overload '+' operator

To declare a class distance to store distance in feet and inches. Inches will increment by 1 if ++ operator is used with the object i.e. ++ operator is overloaded.

• Rules for overloading operators :-

1. Only existing operator can be overloaded. new operator cannot be created.

2. The overloaded operator must have at least one operand that is of user define type.

3. We cannot change the basic meaning of an operator. that is cannot redefine the plus(+) operator to subtract one value from the other.

4. Overloaded operator follow the syntax rules of original operator.