Constructor constructor The main use of constructors is

  • Slides: 17
Download presentation
Constructor

Constructor

constructor The main use of constructors is to initialize objects. A constructor is a

constructor The 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 2

constructor Syntax: class name (argument list) { constructor body; } Eg: Class s 4

constructor Syntax: class name (argument list) { constructor body; } Eg: Class s 4 { int a, b; public: s 4 ()// constructor fun {a=b=0; } }; Main() { s 4 s; // constructor fun invoked automatically when an object created } 3

constructor Constructors are 7 type 1) Default constructor 2) Parameterized constructor 3) Multiple constructor

constructor Constructors are 7 type 1) Default constructor 2) Parameterized constructor 3) Multiple constructor 4) Default argument constructor 5) Dynamic initialization of object 6) Dynamic constructor 7) Copy constructor 4

constructor Default constructor The constructor fun does not having parameters Class p {int a,

constructor Default constructor The constructor fun does not having parameters Class p {int a, b; Public: P() {a=b=10; } }; p obj; Q: Fibonacci series 5

constructor Parameterized constructor The constructor fun which having parameters class pp { int a,

constructor Parameterized constructor The constructor fun which having parameters class pp { int a, b; Public: pp( int x, int y) {a=x; b=y; } }; pp obj(10, 20); 6

constructor Multiple constructor (constructor overloading) One class contains more than one constructor function Default

constructor Multiple constructor (constructor overloading) One class contains more than one constructor function Default constructor and parameterized constructor in one class pp { int a, b; pp obj(10, 20); Public: pp obj 2; pp() // default constructor {a=b=10; } pp( int x, int y)// parameterized constructor {a=x; b=y; } }; 7

constructor Default argument constructor Constructor fun with default arguments class pp { int a,

constructor Default argument constructor Constructor fun with default arguments class pp { int a, b; Public: pp( int x=10, int y=20)// default argument constructor {a=x; b=y; } pp obj(10, 20); pp obj 2; }; A class does not allow to contain both default constructor and default argument constructor, it leads to an ambiguity error 8

constructor Dynamic initialization of object Class object initialized dynamically ie initial values are provide

constructor Dynamic initialization of object Class object initialized dynamically ie initial values are provide during run time, by reading values from main prog class pp { int a, b; Public: Cout<<“Enter 2 nos”; pp( int x, int y) Cin>>a>>b; pp obj(a, b); {a=x; b=y; } }; 9

constructor Dynamic constructor The memory allocation of the object at run time using the

constructor Dynamic constructor The 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 Pointer-variable= new data type; int *p; p=new int; Here 2 bytes of memory is allocated and address is return to the pointer variable p 10

constructor memory management operator Pointer-variable= new data type(value); This syntax is used to allocate

constructor memory management operator Pointer-variable= new data type(value); This syntax is used to allocate memory space and assign initial value int *p; p=new int(10); Pointer-variable= new data type[size]; This syntax is used to allocate an array of memory size int *p; p=new int[5]; 11

constructor memory management operator delete Delete operator is used to destroy or release the

constructor memory management operator delete Delete 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; 12

constructor Dynamic constructor The memory space for each class variables are allocated using the

constructor Dynamic constructor The memory space for each class variables are allocated using the new operator class p { char *s; public: P()// dynamic constructor {s=new char[20]; } p obj; }; 13

constructor Copy constructor copy constructor is used to copy an object. The copy constructor

constructor Copy 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 object class 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; pp A(1, 2); // calling parameterized constructor b=ob. b; } pp B=A; // calling copy constructor }; 14

Destructor • Like Constructors, destructors are also special member functions in C++, whose name

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(); 15

Destructor • Destructor does not have return type and argument, they are used to

Destructor • Destructor does not have return type and argument, they are used to destroy the allocated memory of an object 16

constructor 17

constructor 17