Polymorphism Encapsulation Inheritance Polymorphism from the Greek meaning

  • Slides: 9
Download presentation
Polymorphism • Encapsulation • Inheritance • Polymorphism (from the Greek meaning "having multiple forms")

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

Examples • Pledge: “I, (state your name), do solemnly swear…. ” – UPE (Upsilon

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

Virtual Methods • Different derived classes respond to the same request in different ways.

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;

Run-time Binding • Virtual methods override by a method in a derived class virtual

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)

Which function to call? Example: Class. B derives from Class. A, and so on…

Which function to call? Example: Class. B derives from Class. A, and so on… Object Class. A has a method f(); Class. B, Class. C, Class. D all implement own f(); Class. A Class. B Class. A^ a=gcnew Class. B(); a->f(); Class. C C++: virtual method Class. D

 • Abstract ref class – An incomplete definition of a ref class that

• 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)

C++ Example How to check the earnings of different employee types? Object Employee Boss

C++ Example How to check the earnings of different employee types? Object Employee Boss salary, Earnings, To. String Commission Worker first, last, Earnings, To. String Piece. Worker Hourly. Worker wage. Per. Piece, quantity Earnings, To. String

Applications

Applications

Summary • Although virtual functions of a base class are called within a framework,

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