Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a...
-
Upload
ernesto-rowsell -
Category
Documents
-
view
237 -
download
3
Transcript of Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a...
Constructor
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
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}
4
constructorConstructors are 7 type1) Default constructor2) Parameterized constructor3) Multiple constructor4) Default argument constructor5) Dynamic initialization of object6) Dynamic constructor7) Copy constructor
5
constructorDefault constructorThe constructor fun does not having parametersClass p{int a,b;Public:P(){a=b=10;}};Q: Fibonacci series
p obj;
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);
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;
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;
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);
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;
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];
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;
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;
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
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();
16
Destructor• Destructor does not have return type and
argument, they are used to destroy the allocated memory of an object
17
constructor