Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance,

Similar presentations


Presentation on theme: "Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance,"— Presentation transcript:

1 Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance, they are too general to create objects from. –Abstract classes provide an generic base class that can be used by concrete classes to provide an interface and/or implementation.

2 Abstract Classes An abstract class in C++ is defined as any class that contains at least one pure virtual function.

3 Virtual Functions A virtual function is essentially an incomplete function definition. –The function includes the return type, no parameters, and possibly an initial return value. virtual double functionName() const { return 0.0; }

4 Pure Virtual Functions A pure virtual function associates an initializer with the virtual function declaration. virtual returnType functionName () const = 0;

5 Virtual Functions The derived classes may define an implementation for the virtual function. –If the derived class does not include an implementation for the pure virtual function, the derived class is an abstract class.

6 Virtual Functions Defining pure virtual functions in the abstract base class: –Abstract.h virtual returnType functionName() const = 0; –Abstract.C There is no implementation in the *.C file.

7 Virtual Functions Implementing virtual functions in the concrete derived class: –Concrete.h virtual returnType functionName() const; –Concrete.C returnType Concrete::functionName() const { … }

8 Virtual Functions Animal Dog

9 Virtual Functions Animal.h virtual void speak() = 0; Animal.C –No implementation Dog.h virtual void speak(); Dog.C virtual void speak() { cout << “Arf!”; }

10 Polymorphism Polymorphism occurs when multiple objects from different classes are related by inheritance from the same abstract class. –Each object provides it’s own implementation of the virtual functions and therefore, responds differently to the same message.

11 Polymorphism Polymorphism is most useful when there are a series of related classes that require the “same” behaviors. –The individual classes typically define their own specific behavior implementation.

12 Polymorphism Examples: –A company where there is an abstract class Employee. Every employee must earn money but the way earnings are calculated may be different based upon the type of employee (manager, commission worker, hourly worker).

13 Polymorphism Example: –Another example includes working with shapes. Most shapes have an area and a volume. The specific shapes then define the particular implementation of these two functions.

14 Polymorphism Animal Example

15 Constructors By definition a constructor function can not be defined as a virtual function.

16 Virtual Destructors A class that contains virtual functions should also contain a virtual destructor.

17 What is happening? Any time the C++ compiler encounters a class definition containing a virtual function, the compiler creates a virtual function table (vtable) for the class. –The vtable is used in the program to determine which function implementation is to be executed when the function is called.

18 What is happening? –When an object of a class containing virtual functions is instantiated, the compiler attaches a pointer to the class’ vtable to the front of the object.

19 What is happening? Area Volume PrintName Print Area Volume PrintName Print 0.0 0 0 “Point” [x, y] Shape vtable Point vtable X = 8 Y = 10 Point point

20 Final Example Shapes


Download ppt "Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance,"

Similar presentations


Ads by Google