Vous êtes sur la page 1sur 22

CONSTRUCTORS & DESTRUCTORS

Constructor
Special Member Function used for Initialization -- Same Name as the Class Name Does NOT Return a Value Cannot be virtual and static Implicitly Invoked When Objects are Created or Copied Can Have Arguments Cannot be Explicitly Called Multiple Constructors are Allowed Default Constructor -- No Arguments -- Explicit or Implicit Copy Constructor -- Explicit or Implicit

What is a constructor?

It is a member function which initializes a class. A constructor has: (i) the same name as the class itself (ii) no return type

Constructor

Constructorwhats this?

method used for initializing objects (of certain class) a recipe for creating object of a given class

Constructordo we need it?

do the class variables need initialization?

Constructoran unusual method

We cannot not specify the return value (even: no void). We cannot call it for the already constructed object. We cannot get its address. It is not even visible in the class scope and has no name (according to ANSI).

Constructor Example 1
/* A Simple Date Class With Constructors */ #define size 50 class date{ private: int day, month, year; char *string_date; public: date(); //default date(int ip_day, int ip_month, int ip_year); date(char *ip_date); };

Types of Constructors

Default Constructor : - A constructor that accepts no arguments is called the default constructor. eg: - x::x() If no such constructor is defined, then the compiler supplies a default constructor Parameterized Constructor : -The constructor that can take arguments are called parameterized constructor.

Example Default Constructor


class integer { int m,n; public: integer( ); // default constructor declared void getinfo( ); };

Simple Program
// default constructor #include<iostream> Using namespace std; class integer { int m,n; public: integer(void); // constructor declared void getinfo( ); }; integer :: integer void( ) // constructor defined { m= 0; n=0; }

Example: parameterized constructor


class ABC { int i; public: float j; char k; ABC(int a, float b, char c) //parameterized { i=a; j=b; k=c; } : };

Class integer { Int m, n; Public : Integer (int x, int y); }; Integer :: integer(int x,int y) { m= x; n=y; } By Calling the Constructor explicitly By Calling the consructor implicitly

We must pass the initial values as arguments to the constructor function when an object is declared. This can be done in two ways:
by calling the function explicitly eg: integer int1 = integer(0,100); by calling the function implicitly eg: integer int1(0,100);

(1) (2)

Copy Constructor

Used to declare & initialize object from another object. eg: integer L2(L1); would define the object L2 & at the same time initialize it to the values of L1.Another form of this statement is integer L2 = L1;

Copy Constructor
1. The syntax for a copy constructor declaration is class_name :: class_name (class_name &ptr) 2. The Copy Constructor may be used in the following format also using a const keyword. class_name :: class_name (const class_name & ptr) 3. Copy Constructor are always used when the compiler has to create a temporary object of a class object.The copy constructor are used in the following situations:4. The Initialization of an object by another object of the same class. 5. Return of object as a function value. 6. Stating the object as by value parameters of a function.

Copy Constructor Example with Program

//fibonacci series using copy constructor

#include<iostream>
Using namespaces std ; Class fibonacci { Private : Unsigned long int f0,f1,fib; Public : Fiboancci () // constructor { F0=0; F1=1; Fib=f0+f1; } Fibonacci (fibonacci &ptr) // copy construtor { F0=ptr.f0; F1=ptr.f1; Fib=prt.fib; }

Void increment ( ) { F0=f1; F1=fib; Fib=f0+f1; } Void display ( ) { Cout<<fib<</t; } }; Void main( ) { Fibonacci number ; For(int i=0;i<=15;++i) { Number.display(); Number.increment(); } }

Dynamic Constructor

Used to allocate memory while creating objects. This will enable the system to allocate the right amount of memory for each object when the objects are not of the same memory. Allocation of memory to object at the time of their construction is known as dynamic construction of objects. The memory is allocated with the help of the new operator.

Destructor

What is the destructor?

Easy to guess

When do we need it?

Even easier

Destructor
Special Member Function used for Clean-up -- Same Name as the Class Name with a ~ One Single Destructor Invoked When an Object Goes Out of Scope Can be Explicitly Called -- Unusual Cannot Accept Parameters and Does Not Return A Value Cannot be Declared static, const or volatile Can be virtual

Destructor Example 1
/* A Simple Date Class With a Destructor */ #define size 50 class date{ private: int day, month, year; char *string_date; public: date(char *ip_date); ~date(); //Destructor };

Destructor

as opposed to constructor it is in the class scope we may call it

destructor, if not defined by programmer, is generated by compiler

it has empty body, but

Order of calling constructors and destructors

Constructors
1.

2.
3.

base class (classes in order of declaration) class members in order of declaration constructors body

Destructors simply opposite order


1. 2.

3.

destructors body destructors of class members (order opposite to declaration) destructor of the base class (classes in order opposite to declaration)

Thank You!!!

Vous aimerez peut-être aussi