8/13/2019 C++ virtual functions and abstract class
1/11
C++ VirtualFunctions,Abstract
Class,Templates,OverloaMember Function
Pi19404
November 26, 2013
8/13/2019 C++ virtual functions and abstract class
2/11
Contents
Contents
0.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
0.2 Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8/13/2019 C++ virtual functions and abstract class
3/11
C++ Inheritance
C++ Inheritance
0.1 Introduction
This article describes basic concepts of C++ Virtual functions andabstract class
0.2 Virtual Functions
By default, C++ matches a function call with the correct func-tion definition at compile time.This is called static binding
You can specify that the compiler match a function call with thecorrect function definition at run time; this is called dynamicbinding
You declare a function with the keyword virtual if you want thecompiler to use dynamic binding for that specific functions
8/13/2019 C++ virtual functions and abstract class
4/11
C++ Inheritance
The virtual keyword indicates to the compiler that it shouldchoose the appropriate definition of f() not by the type ofreference, but by the type of object that the reference refers
to.
a virtual function is a member function you may redefine forother derived classes, and can ensure that the compiler will call
the redefined virtual function for an object of the correspond-ing derived class, even if you call that function with a pointer orreference to a base class of the object.
A class that declares or inherits a virtual function is called apolymorphic class.
8/13/2019 C++ virtual functions and abstract class
5/11
C++ Inheritance
You redefine a virtual member function, like any member func-tion, in any derived class. Any member function declared as vir-tual with same name in derived class is also considered as vir-tual,irrespective of the parameter type
A virtual function cannot be global or static because, by defini-tion, a virtual function is a member function of a base class andrelies on a specific object to determine which implementationof the function is called. You can declare a virtual function tobe a friend of another class.
If a function is declared virtual in its base class, you can stillaccess it directly using the scope resolution (::) operator. In thiscase, the virtual function call mechanism is suppressed and the
function implementation defined in the base class is used.
8/13/2019 C++ virtual functions and abstract class
6/11
C++ Inheritance
In addition, if you do not override a virtual member function ina derived class, a call to that function uses the function imple-mentation defined in the base classes
The return type of an overriding virtual function may differfrom the return type of the overridden virtual function. The
8/13/2019 C++ virtual functions and abstract class
7/11
C++ Inheritance
derived class return a pointer or reference to a class whichis a direct or indirect base class of type returned by the Baseclass.
You cannot override one virtual function with two or moreambiguous virtual functions. This can happen in a derived classthat inherits from two nonvirtual bases that are derived froma virtual base class.
8/13/2019 C++ virtual functions and abstract class
8/11
C++ Inheritance
As long as ambiguous function is not called,the compiler will not
give error
8/13/2019 C++ virtual functions and abstract class
9/11
C++ Inheritance
The access for a virtual function is specified when it is declared.The access rules for a virtual function are not affected by the
access rules for the function that later overrides the virtualfunctions
If a virtual function is called with a pointer or reference to aclass object, the type of the class object is not used to deter-mine the access of the virtual function. Instead, the type of thepointer or reference to the class object is used.
An abstract class is a class that is designed to be specifically used
8/13/2019 C++ virtual functions and abstract class
10/11
C++ Inheritance
as a base class. . An abstract class contains at least one purevirtual function
You declare a pure virtual function by using a pure specifier (=
0) in the declaration of a virtual member function in the classdeclaration.
You cannot use an abstract class as a parameter type, a functionreturn type, or the type of an explicit conversion, nor can you
declare an object of an abstract class. You can, however, declare
pointers and references to an abstract classes
A function declaration cannot have both a pure specifier and adefinition.A pure declaration forces a definition in the derivedclass.
Virtual member functions are inherited. A class derived from anabstract base class will also be abstract unless you override eachpure virtual function in the derived class.
Note that you can derive an abstract class from a nonabstractclass, and you can override a non-pure virtual function with apure virtual function.
You can call member functions from a constructor or destruc-tor of an abstract class.
However, the results of calling (directly or indirectly) a purevirtual function from its constructor are undefined.
8/13/2019 C++ virtual functions and abstract class
11/11
C++ Inheritance
If you declare a base class destructor as virtual, a derived classdestructor will override that base class destructor, even thoughdestructors are not inherited.