a constructor is a special member function whose task is to initialize the objects of its class. it...
TRANSCRIPT
![Page 1: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/1.jpg)
PASSING PARAMETERS TO BASE CLASS USING
CONSTRUCTOR AND DESTRUCTOR.
![Page 2: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/2.jpg)
SHRADDHA.MRUNALI.AISHWARAYA.SONALI.POOJA.
PRESENTED BY:
![Page 3: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/3.jpg)
A constructor is a special member function whose task is to initialize the objects of its class.
It is special because its name is same as the class name.
The constructor is invoked whenever an object of its associated class is created.
It is called constructor because it constructs the values of data members of the class.
CONSTRUCTORS
![Page 4: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/4.jpg)
class add{ int m, n ; public : add (void) ; ------};add :: add (void){ m = 0; n = 0;}
When a class contains a constructor, it is guaranteed that an object created by the class will be initialized automatically.
add a ; Not only creates the
object a of type add but also initializes its data members m and n to zero.
CONSTRUCTOR - EXAMPLE
![Page 5: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/5.jpg)
A destructor is used to destroy the objects that have been created by a constructor.
Like constructor, the destructor is a member function whose name is the same as the class name but is preceded by a tilde.
eg: ~ integer ( ) { }
DESTRUCTORS
![Page 6: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/6.jpg)
It is a good practice to declare destructors in a program since it releases memory space for further use.
Whenever new is used to allocate memory in the constructor, we should use delete to free that memory.
DESTRUCTORS
![Page 7: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/7.jpg)
INHERITANCEThe C++ classes can be reused in many ways.Once a class has been tested, it can be adapted by other programs to their requirements.The Mechanism of deriving a new class from an old one is called INHERITANCE.The old class is referred as BASE class.The new class is referred as DERIVED class.
![Page 8: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/8.jpg)
TYPES OF INHERITANCEThere are 5 Forms of INHERITANCE:Single InheritanceMultiple InheritanceHierarchical InheritanceMultilevel InheritanceHybrid Inheritance
![Page 9: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/9.jpg)
Multiple Inheritance
Hybrid InheritanceMulti Level Inheritance
Hierarchical InheritanceSingle Inheritance
![Page 10: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/10.jpg)
CONSTRUCTOR AND DESTRUCTOR IN INHERITANCE
![Page 11: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/11.jpg)
PROGRAM:#include< iostream .h >#include<conio.h>class base{Public:base()Cout<<“Constructing base”;}~base(){Cout<<“Destructing base”;} };
![Page 12: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/12.jpg)
Class derived: public base{Public:derived()Cout<<“Constructing derived”;}~derived(){Cout<<“Destructing derived”;} };
![Page 13: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/13.jpg)
Void main(){derived D;Return(0);}
![Page 14: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/14.jpg)
OUTPUT:
Constructing baseConstructing derivedDestructing derivedDestructing base
![Page 15: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/15.jpg)
PROGRAM:#include<iostream.h> //header file#include<conio.h>class base //declaraction of class{protected:int i; //members of classpublic:base(int X) //parametrised conctructor
![Page 16: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/16.jpg)
{i=X;Cout<<“\n\nConstructing base”;}~base() //destructor{Cout<<“\n\n Destructing base”;}}; //termination of classclass derived: public base //derived from base class{
![Page 17: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/17.jpg)
int j;public://passed along to base//passing parameters to base classderived(int X, int Y):base(Y){j=X;Cout<<“Constructing derived\n\n”;}~derived(){Cout<<“\n\n Destructing derived”;}
![Page 18: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/18.jpg)
void show(){cout<<“i= \n “<<i<<“j= \n “<<j<<endl;}}; //termination of classvoid main(){derived ob(3,4); //object is createdclrscr();ob.show(); //function callgetch();}
![Page 19: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/19.jpg)
OUTPUT:
Constructing baseConstructing derivedi=4j=3Destructing derivedDestructing base
![Page 20: A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class](https://reader036.vdocuments.us/reader036/viewer/2022083011/5697bf7d1a28abf838c84738/html5/thumbnails/20.jpg)
THANK-YOU