Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.

Similar presentations


Presentation on theme: "Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually."— Presentation transcript:

1 Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually pointed to Static binding is done at compile time

2 Late Binding / Dynamic Binding
technique of waiting until runtime to determine which version of a member function is appropriate Can look at the actual class of the object pointed to and choose the most specific version of the function Dynamic binding is used to bind virtual functions

3 Polymorphism Polymorphism is another word for late binding. Enables us to write programs that process objects of classes that are part of the same class hierarchy as if they were all objects of the hierarchy’s base class.

4 Virtual Functions (C++ only)
By default, C++ matches a function call with the correct function definition at compile time -- static binding. Can specify that the compiler match a function call with the correct function definition at run time -- dynamic binding. Declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function.

5 Virtual Functions (C++ only)
The virtual function specification tells the compiler to create a pointer to a function f(), but to not fill in the value of the pointer until the function is actually called. Declaring a function virtual will make the compiler check the type of each object to see if it defines a more specific version of the virtual function;

6 Virtual Functions (C++ only)
the compiler chooses the appropriate definition of f(), not by the type of reference, but by the type of object that the reference refers to. Therefore, a virtual function is a member function you may redefine for other derived classes, and can ensure that the compiler will call the redefined virtual function for an object of the corresponding derived class, even if you call that function with a pointer or reference to a base class of the object.

7 Virtual Functions (C++ only)
To create a virtual function, place the keyword virtual before the function's return type in the declaration section. Once a function is declared as virtual, it remains virtual for the next derived class with or without a virtual declaration in the derived class. The virtual declaration in the derived class is not needed, but is usually included both for clarity and to ensure that any subsequently derived classes correctly inherit the function.

8 Virtual Functions (C++ only)
A class that declares or inherits a virtual function is called a polymorphic class. You redefine a virtual member function, like any member function, in any derived class.

9 Polymorphism and Virtual Member Functions
Polymorphic code: Code that behaves differently when it acts on objects of different types Virtual Member Function: The C++ mechanism for achieving polymorphism

10 Polymorphism Consider the Vehicle, Car, Truck hierarchy where each class has its own version of the member function printType( ) Vehicle Car Truck 18-Wheeler

11 Polymorphism class Vehicle { public: Vehicle(); Vehicle(…); void printType(); private: string type; };

12 Polymorphism class Car : public Vehicle { public: Car(); Car(…) void printType(); … };

13 Polymorphism class Truck : public Vehicle { public: Truck(); Truck(…) void printType(); … };

14 void Vehicle::printType() { cout << "vehicle"; }
Polymorphism void Vehicle::printType() { cout << "vehicle"; }

15 void Car::printType() { cout << "car"; }
Polymorphism void Car::printType() { cout << "car"; }

16 void Truck::printType() { cout << "truck"; }
Polymorphism void Truck::printType() { cout << "truck"; }

17 Polymorphism Consider the collection of different Vehicle objects
Vehicle *vehCollection[] = {new Vehicle, new Car,new Truck}; and accompanying code for(int k=0; k<3; k++) vehCollection[k]->printType(); Prints: vehicle vehicle vehicle ignoring the more specific versions of printType() in Car and Truck See inheritance4.h and pr15-02.cpp

18 Polymorphism The preceding code is not polymorphic: it behaves the same way even though Vehicle, Car, and Truck have different types and different printType() member functions. Static binding is used. In the expression vehCollection[k]->printType(); the compiler sees only the type of the pointer vehCollection[k], which is pointer to Vehicle; it does not see the type of the actual object pointed to, which may be Vehicle, or Car, or Truck

19 Polymorphism Polymorphic code would have printed vehicle car truck instead of vehicle vehicle vehicle

20 Virtual Functions If the member functions printType()are declared virtual, then the code Vehicle *vehCollection[] = {new Vehicle, new Car,new Truck}; for(int k=0; k<3; k++) vehCollection[k]->id(); will print vehicle car truck See inheritance5.h and pr15-03.cpp

21 Virtual Functions Declaring printType() as a virtual function class Vehicle { public: … virtual void printType(); }

22 Virtual Functions Declaring printType() as a virtual function: class Car : public Vehicle { … virtual void printType(); }

23 Virtual Functions Declaring printType() as a virtual function: class Truck : public Vehicle { … virtual void printType(); }

24 Virtual functions Now that printType() is declared virtual, vehCollection[k]->printType() will print vehicle car truck dynamic binding is used and the compiler chooses the appropriate definition of printType() by the type of object that the reference refers to.

25 Abstract Base Classes and Pure Virtual Functions
An abstract class is a class that contains no objects that are not members of subclasses (derived classes) For example, in real life, Animal is an abstract class: there are no animals that are not dogs, or cats, or lions …

26 Abstract Base Classes and Pure Virtual Functions
Abstract classes are an organizational tool. They are useful in organizing inheritance hierarchies Abstract classes can be used to specify an interface that must be implemented by all subclasses

27 Abstract Functions The member functions specified in an abstract class do not have to be implemented The implementation is left to the subclasses In C++, an abstract class is a class with at least one abstract member function

28 Pure Virtual Functions
In C++, a member function of a class is declared to be an abstract function by making it virtual and replacing its body with = 0; class Animal{ public: virtual void id()=0; }; A virtual function with its body omitted and replaced with =0 is called a pure virtual function, or an abstract function See pr15-04.cpp

29 Abstract Classes An abstract class can not be instantiated An abstract class can only be inherited from; that is, you can derive classes from it Classes derived from abstract classes must override all pure virtual functions with concrete member functions before they can be instantiated.


Download ppt "Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually."

Similar presentations


Ads by Google