Presentation is loading. Please wait.

Presentation is loading. Please wait.

Polymorphism Encapsulation Inheritance

Similar presentations


Presentation on theme: "Polymorphism Encapsulation Inheritance"— Presentation transcript:

1 Polymorphism Encapsulation Inheritance
Polymorphism (from the Greek meaning "having multiple forms") The ability for different objects derived from a common base object to respond to the same stimuli in a completely different ways.

2 Examples Pledge: “I, (state your name), do solemnly swear….”
UPE (Upsilon Pi Epsilon) pledge: "I ( state your name ), … having heard and understood ... the purposes and tenets of Upsilon Pi Epsilon ... do solemnly pledge, ... always to respect and promote ... the aims and ideals of this Society.” Programing examples

3 Virtual Methods Different derived classes respond to the same request in different ways. A class provides virtual method, which allows any class derived from it to respond differently to the same request : via override the virtual method in the derived class, and then call the method in the base class Virtual methods virtual void speak() {}; Pure virtual method virtual void speak() = 0;

4 Run-time Binding Virtual methods override by a method in a derived class virtual void speak() override {}; Compare to general method override Both require identical signature Static binding (binding to the type that the method is called) in general method override vs, run-time binding in the virtual method case (derived class type’s method overrides at the time of instantiate)

5 Which function to call? ClassA^ a=gcnew ClassB(); a->f();
Example: ClassB derives from ClassA, and so on… ClassA has a method f(); ClassB, ClassC, ClassD all implement own f(); Object ClassA ClassA^ a=gcnew ClassB(); a->f(); ClassB ClassC C++: virtual method ClassD

6 Abstract ref class An incomplete definition of a ref class that has at least one pure virtual member method; Cannot instantiate an object of it; A derived class of it must implement the pure virtual function, (or, it becomes abstract)

7 C++ Example How to check the earnings of different employee types?
Object first, last, Earnings, ToString Employee Boss CommissionWorker PieceWorker HourlyWorker salary, Earnings, ToString wagePerPiece, quantity Earnings, ToString

8 Applications

9 Summary Although virtual functions of a base class are called within a framework, the actual functions to be called at run time are that of the actual class derived from the base class. Only base classes are needed to define a framework. Dynamic Binding versus Static Binding


Download ppt "Polymorphism Encapsulation Inheritance"

Similar presentations


Ads by Google