Presentation is loading. Please wait.

Presentation is loading. Please wait.

Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.

Similar presentations


Presentation on theme: "Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking."— Presentation transcript:

1 Polymorphism

2 Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking. Also known as compile time polymorphism, simply means that an object is bound to its function call at compile time

3

4 How do we use member function show() to print the values of objects of both classes A and B? – Prototype of show() is same in both cases. So static binding does not apply. – Can use class resolution operator(::) Runtime polymorphism – Selecting appropriate member function while the program is running – Virtual function for this purpose – a.k.a late binding or dynamic binding class A { int x; public: void show(){…} }; class B: public A { int y; public: void show(){…} };

5 Pointers to Objects Consider the statement: item x; What does it mean ? We can define a pointer of type item as item *itemptr; Object pointers are useful in creating objects at runtime We can also use an object pointer to access public members of an object

6 To declare item variable x and pointer to x: item x; item *ptr ptr= &x; //ptr initialized with address of x Member function of item can be referred to as: x.getdata(10, 20.5) x.show(); OR ptr->getdata()10,20.5); ptr->show(); OR (*ptr).show(); We can also create the objects using pointers and new operator: item *ptr=new item; This allocates enough memory for the data members in the object structure and assigns the address of the memory space to ptr. ptr can be used to refer the members as: ptr->show(); We can create array of objects using pointers: item *ptr = new item[10]; class item { int code; float prices; public: void getdata(int a, float b) { code=a; price=b; } void show(int a, int b) { cout<<“Code: “<<code<<“\n”; cout<<“Price: “<<price<<“\n\n”; } };

7 Code Src: pointer_to_obj.cpp

8 this Pointer Every object has a special pointer “this “ which points to the object itself This pointer is accessible to all the members of the class but not to any static members of the class Can be used to find the address of the object in which the function is a member

9 this Pointer We create many instances of a class in a program How do functions get to know which object has called them The member functions are passes the pointers of the calling object. qMyInt.Get() &qMyInt

10 this Pointer The pointer is passed with the name “this” “this” points to qMyInt Actually additional pointer parameter of the class type that the function belongs to memfun(Myclass* const this, arg1,…) which we write as memfun(arg1,..)

11 Pointers to derived classes Pointers can be used not only to base objects but also to objects of derived classes Pointers to objects of a base are type-compatible with pointers to objects of a derived class Therefore, a single pointer variable can be made to point to objects belonging to different classes But there is a problem in using pointer to access the public members of the derived class Only those members that are inherited from base class can be accessed Those members which originally belong to derived class cannot be accessed.

12 Pointers to derived classes In case a member of derived class has the same name as one of the members of base class, then any reference to that member by pointer will always access the base class member.

13 Virtual Functions Polymorphism refers to the property by which objects belonging to different classes are able to respond to the same message, but in different forms An essential requirement of polymorphism is therefore the ability to refer to objects without any regard to their classes. This necessitates the use of single pointer variable to refer to the objects of different classes When we use same function name in both base and derived classes, the function in base class is declared as virtual. When a function is made virtual, c++ determines which function to use at run time based on the type of object pointed by the base pointer rather than the type of the pointer

14 Virtual functions We must access virtual function through the use of pointer declared as a pointer to the base class. Why can’t we use the object name with dot operator the same way as any other member function to call the virtual function? Yes, we can but run time polymorphism is achieved only when a virtual function is accessed through a pointer to the base class.

15 Rules for Virtual Functions The virtual function must be member of some class, cannot be static member They are accessed by using object pointer A virtual function can be a friend of another class The prototypes of base class version of virtual function and all derived class version must be identical We cannot have virtual constructors but can have virtual destructors While a base class pointer can point to any type of derived object, the reverse is not true When a base pointer points to a derived class, incrementing or decrementing it will not make it point to the next object of derived class

16 Pure Virtual Function A virtual function is usually declared in base class and redefined in the derived classes. The function in base class only serves as place holders, also are “empty”. If we define such functions as: – virtual void display()=0; Such functions are called pure virtual functions. The derived classes should either define the function or re-declare it as pure virtual. The class containing pure virtual function cannot be used to declare any object of its own. Such classes are called abstract base classes

17 Early Binding and Late Binding http://www.learncpp.com/cpp-tutorial/124- early-binding-and-late-binding/


Download ppt "Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking."

Similar presentations


Ads by Google