Presentation is loading. Please wait.

Presentation is loading. Please wait.

 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.

Similar presentations


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

1

2

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

4 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.

5  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 ( ) { }

6  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.

7  The 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.

8  There are 5 Forms of INHERITANCE:  Single Inheritance  Multiple Inheritance  Hierarchical Inheritance  Multilevel Inheritance  Hybrid Inheritance

9 Multiple Inheritance Hybrid Inheritance Multi Level Inheritance Hierarchical InheritanceSingle Inheritance

10

11

12 Class derived: public base { Public: derived() Cout<<“Constructing derived”; } ~derived() { Cout<<“Destructing derived”; } };

13 Void main() { derived D; Return(0); }

14

15 #include //header file #include class base//declaraction of class { protected: int i;//members of class public: base(int X)//parametrised conctructor

16 { i=X; Cout<<“\n\nConstructing base”; } ~base()//destructor { Cout<<“\n\n Destructing base”; } };//termination of class class derived: public base//derived from base class {

17 int j; public: //passed along to base //passing parameters to base class derived(int X, int Y):base(Y) { j=X; Cout<<“Constructing derived\n\n”; } ~derived() { Cout<<“\n\n Destructing derived”; }

18 void show() { cout<<“i= \n “<<i<<“j= \n “<<j<<endl; } };//termination of class void main() { derived ob(3,4);//object is created clrscr(); ob.show();//function call getch(); }

19

20


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

Similar presentations


Ads by Google