constructor. 2 constructor the main use of constructors is to initialize objects. a constructor is a...

17
Constructor

Upload: ernesto-rowsell

Post on 14-Dec-2015

243 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

Constructor

Page 2: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

2

constructorThe main use of constructors is to initialize objects. A constructor is a special member function, whose

name is same as class name.Constructor function cannot invoke using class object.They invoked automatically when an object is created.Constructor fun does not having any return type, they

are mainly used to initialized the class variables.Constructor funs are defined in the public section of the

class

Page 3: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

3

constructorSyntax:class name (argument list){

constructor body;}Eg:Class s4{

int a,b;public:s4 ()// constructor fun {a=b=0;}

};Main(){

s4 s;// constructor fun invoked automatically when an object created}

Page 4: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

4

constructorConstructors are 7 type1) Default constructor2) Parameterized constructor3) Multiple constructor4) Default argument constructor5) Dynamic initialization of object6) Dynamic constructor7) Copy constructor

Page 5: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

5

constructorDefault constructorThe constructor fun does not having parametersClass p{int a,b;Public:P(){a=b=10;}};Q: Fibonacci series

p obj;

Page 6: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

6

constructorParameterized constructorThe constructor fun which having parametersclass pp{ int a,b;Public:pp( int x,int y){a=x;b=y;}}; pp obj(10,20);

Page 7: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

7

constructorMultiple constructor (constructor overloading)One class contains more than one constructor functionDefault constructor and parameterized constructor in one

classclass pp{ int a,b;Public:pp() // default constructor{a=b=10;}pp( int x,int y)// parameterized constructor{a=x;b=y;}};

pp obj(10,20);pp obj2;

Page 8: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

8

constructorDefault argument constructorConstructor fun with default argumentsclass pp{ int a,b;Public:pp( int x=10,int y=20)// default argument constructor

{a=x;b=y;}};A class does not allow to contain both default

constructor and default argument constructor, it leads to an ambiguity error

pp obj(10,20);pp obj2;

Page 9: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

9

constructorDynamic initialization of objectClass object initialized dynamically ie initial values

are provide during run time, by reading values from main prog

class pp{ int a,b;Public:pp( int x,int y){a=x;b=y;}};

int a,b;Cout<<“Enter 2 nos”;Cin>>a>>b;pp obj(a,b);

Page 10: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

10

constructorDynamic constructorThe memory allocation of the object at run time

using the memory management operator new New operator is used to allocate memory space at

run time

Here 2 bytes of memory is allocated and address is return to the pointer variable p

Pointer-variable= new data type;

int *p; p=new int;

Page 11: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

11

constructormemory management operator

This syntax is used to allocate memory space and assign initial value

This syntax is used to allocate an array of memory size

Pointer-variable= new data type(value);

int *p; p=new int(10);

Pointer-variable= new data type[size];

int *p; p=new int[5];

Page 12: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

12

constructormemory management operator deleteDelete operator is used to destroy or release the

memory space allocated by new operator

Delete pointer variable;

int *p; p=new int[5];delete p;

Page 13: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

13

constructorDynamic constructorThe memory space for each class variables are

allocated using the new operatorclass p{ char *s;public:P()// dynamic constructor{s=new char[20]; }}; p obj;

Page 14: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

14

constructorCopy constructor copy constructor is used to copy an object. The copy constructor allows the programmer to create a new object

from an existing one by initialization.The argument of copy constructor is the address of class objectclass pp{ int a,b;Public:pp( int x=10,int y=20)// default argument constructor

{a=x;b=y;}pp( pp &ob) // copy constructor{a=ob.a;b=ob.b;}};

pp A(1,2);// calling parameterized constructor

pp B=A;// calling copy constructor

Page 15: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

15

Destructor• Like Constructors, destructors are also special

member functions in C++, whose name is same as class name, use to destroy or release allocated memory.

• Destructors are used to free memory, release resources and to perform other clean up.

• Destructors are invoked automatically like constructors, at end of a program or end of a block.

• General Syntax of destructors• ~ classname();

Page 16: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

16

Destructor• Destructor does not have return type and

argument, they are used to destroy the allocated memory of an object

Page 17: Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class

17

constructor