a constructor is a special member function whose task is to initialize the objects of its class. it...

20
PASSING PARAMETERS TO BASE CLASS USING CONSTRUCTOR AND DESTRUCTOR.

Upload: myrtle-benson

Post on 29-Jan-2016

212 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

{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

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

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

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

THANK-YOU