POLYMORPHISM By Muhammad Waris Zargar Visit My Website And Subscribe My Channel www.wariszargar.wordpress.com Contact :: +923086529243 Gmail :: [email protected] Face Book :: [email protected] PolyMorphism ◦ It is combination of two word poly (many) and morphism (form). ◦ In object oriented programming polymorphism is the ability of object of different types to response to function of same name. ◦ The behavior of object can be implemented in run time. Pointer To Object Ptr -> member (data member or member function) New keyword ◦It is used to allocate memory dynamically ◦It is allocate memory according to the type of pointer ◦It returns the address to the pointer ◦Pointer than work and collect data ◦It is used to create simple variable , object , array Method: 1st Class name *ptr = new class name (parameter); 2nd Int *ptr ; Ptr = new int Simple Programme ◦ Class student{ ◦ Int a; ◦ Void in(){ cin >> a; } ◦ Void show() { cout << “age :: ” << age;} ◦ }; ◦ Int main(){ student *s = new student; ◦ S ->in(); ◦ S ->show(); ◦} Array Of Pointer ◦ Class student{int age; string name; ◦ Void in(){ cin >> name; ◦ } void show() { cin >> age; cout << “name ” << name ◦ }; ◦ Int main(){ char choice; int I =0; ◦ Student *waris[50]; ◦ Do(){ ◦ Waris[i] = new student; ◦ Waris->in(); i++; ◦ Cin >> choice; ◦ }while(choice == ‘y’); ◦ } waris->show(); } << age;} Pointer And Inheritance ◦If we use only derive class object then it is called derive class function every time. ◦Obj . input(); ◦Obj .show(); ◦ This called every time derive class function Pare nt Ptr obj3 Pointer And Inheritance ◦In this section if we use overriding method and make an pointer object of parent class and pass address of derive class is called parent class function every time. ◦Parent *obj[50] = & obj; ◦Obj -> input(); ◦Obj[i] ->show(); ◦ This called every time parent class function Ptr obj1 Ptr obj2 Ptr obj3 Virtual Function ◦ Virtual means that exist in effect but not in reality . ◦ Virtual function are used to implement polymorphism. ◦ In this way user execute completely different function by same function call. ◦ Keyword virtual used with function in class ◦ Derive *obj[50]; ◦ Obj -> input(); ◦ Obj[i] ->show(); ◦ It called that function whose class object pass. Ptr obj1 Ptr obj2 Ptr obj3 Early And Late Binding ◦ Early Binding: ◦ It is also called static binding. ◦ Before polymorphism we doing early binding ◦ The early binding occur when every thing required to call function is known at compile time. Late Binding: It is also called dynamic binding. Compiler does not knwn which function to be called. The use of virtual function to implement polymorphism is an example late bibnding. Example ◦ Class A{ void show( ){ cout << “I am in parent class function A”} ◦ }; ◦ Class B : public A{ void show( ){ cout << “I am in derive class function B”} }; ◦ Class C: public A{ void show( ){ cout << “I am in derive class function C”} }; ◦ Int main(){ ◦ A *waris[5]; ◦ Int choice; ◦ Cout << “1 for create obj of class A and 2 for B and 3 for C”; ◦ Cin >> choice; ◦ For(int i=0;i<5;i++){ if(choice == 1) ◦ If(choice == 2) { waris[i] = new B; } ◦ If(choice == 3) { waris[i] = new C; ◦ } ◦ } { waris[i] =new A; } } Pure Virtual Function ◦ A type of virtual function that has no body is called pure virtual function. How make a pure virtual function ? The virtual keyword at the start of function. The zero at the end of function declation. Class waris{ Virtual void show() = 0; } Abstract Class ◦ If any class has virtual function is called abstract class. ◦ Abstact class cannot use directly means that it has no object. ◦ But we make pointer of that class. ◦ However , a child class can inherit an abstract class and use it by overriding pure virtual function. ◦ If not use overriding method then next class is also make abstract class.’ Syntax: class waris{ Virtual void show()=0; } Virtual Base Class Or Diamond Problem Parent Child1 Child1 Baby Thanks Visit My Website And Subscribe My Channel www.wariszargar.wordpress.com Contact :: +923086529243 Gmail :: [email protected] Face Book :: [email protected]
© Copyright 2025 Paperzz