Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Constructor

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

3 3 constructor Syntax: 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 4 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

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

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

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

8 8 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;} }; A class does not allow to contain both default constructor and default argument constructor, it leads to an ambiguity error

9 9 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: pp( int x,int y) {a=x;b=y;} };

10 10 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 Here 2 bytes of memory is allocated and address is return to the pointer variable p

11 11 constructor memory 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

12 12 constructor memory management operator delete Delete operator is used to destroy or release the memory space allocated by new operator

13 13 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]; } };

14 14 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; b=ob.b;} };

15 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 16 Destructor Destructor does not have return type and argument, they are used to destroy the allocated memory of an object

17 17 constructor


Download ppt "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."

Similar presentations


Ads by Google