You are on page 1of 11

C++ Virtual Functions,Abstract Class,Templates,Overload Member Function

Pi19404
November 26, 2013

Contents

Contents
C++ Inheritance
0.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 0.2 Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3 3

2 | 11

C++ Inheritance

C++ Inheritance
0.1 Introduction
This article describes basic concepts of C++ Virtual functions and abstract class

0.2 Virtual Functions


 By default, C++ matches a function call with the correct function definition at compile time.This is called static binding  You can specify that the compiler match a function call with the correct function definition at run time; this is called dynamic binding  You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific functions

using namespace std; class Parent{ public: int a; void disp(){cout << "This is the parent" << endl;} }; class Base:public Parent{ using Parent::a; public: void disp(){cout << "This is the Derived" << endl;} }; int main() { Base d; Parent *p; p=&d;

3 | 11

C++ Inheritance

//p.disp() static binding,it does not know //p is considerd as object of parent class, //though it is pointer to object of Derived class p->disp();//will print "This is the parent"
 The virtual keyword indicates to the compiler that it should choose the appropriate definition of f() not by the type of reference, but by the type of object that the reference refers to.

#include <iostream> using namespace std; class Parent{ public: int a; virtual void disp(){cout << "This is the parent" << endl;} }; class Base:public Parent{ using Parent::a; public: void disp(){cout << "This is the Derived" << endl;} }; int main() { Base d; Parent *p; p=&d; //dynamic bindind for disp function //the fuction is derived class is called p->disp();//will print "This is the Derived" }
 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.  A class that declares or inherits a virtual function is called a polymorphic class.

4 | 11

C++ Inheritance
 You redefine a virtual member function, like any member function, in any derived class. Any member function declared as virtual with same name in derived class is also considered as virtual,irrespective of the parameter type

class Parent{ public: int a; virtual void disp(){cout << "This is the parent" << endl;} }; class Base:public Parent{ public: //this is not virtual function,it hides Parent::disp void disp(int x){cout << "This is the Base" << endl;} }; class Derived:public Parent { public: //this function is not virtual it hides Parent::disp void disp(){cout << "This is the Derived" << endl;} }; int main() { Derived d; Base b; Parent *p; p=&d;p->disp(); p=&b;p->disp(); b.disp(); // this line will give error }
 A virtual function cannot be global or static because, by definition, a virtual function is a member function of a base class and relies on a specific object to determine which implementation of the function is called. You can declare a virtual function to be a friend of another class.  If a function is declared virtual in its base class, you can still access it directly using the scope resolution (::) operator. In this case, the virtual function call mechanism is suppressed and the function implementation defined in the base class is used.

5 | 11

C++ Inheritance

class Parent{ public: int a; virtual void disp(){cout << "This is the parent" << endl;} }; class Base:public Parent{ public: //this is a virtual function,it overrides Parent::disp void disp(){cout << "This is the Base" << endl;} }; int main() { Base b; Parent *p; p=&b;p->disp(); b.Parent::disp(); // this refer to the parent }
 In addition, if you do not override a virtual member function in a derived class, a call to that function uses the function implementation defined in the base classes

class Parent{ public: int a; virtual void disp(){cout << "This is the parent" << endl;} }; class Base:public Parent{ public: }; int main() { Base b; Parent *p; p=&b;p->disp(); b.Parent::disp(); // this refer to the parent }
 The return type of an overriding virtual function may differ from the return type of the overridden virtual function. The

6 | 11

C++ Inheritance derived class return a pointer or reference to a class which is a direct or indirect base class of type returned by the Base class.

class Parent{ public: int a; virtual Parent * ret(){return this;} virtual void disp(){cout << "Parent" << endl;} }; class Base:public Parent{ public: Base * ret(){return this;} //returning type is different void disp(){cout << "Base" << endl;} }; int main() { Base b; Parent *p; p=&b; p=p->ret(); p->disp(); }
 You cannot override one virtual function with two or more ambiguous virtual functions. This can happen in a derived class that inherits from two nonvirtual bases that are derived from a virtual base class.

class Parent{ public: int a; virtual void disp(){cout << "Parent" << endl;} }; class Base:public Parent{ public: //Base::disp tries to override Parent::disp void disp(){cout << "Base" << endl;}

7 | 11

C++ Inheritance

}; class Base1:public Parent{ public: //Base1::disp tries to override Parent::disp void disp(){cout << "Base" << endl;} }; class Derived:public Base1,public Base { //Both Base::1 and Base::disp try to override Parent::disp }
 As long as ambiguous function is not called,the compiler will not give error

class Parent{ public: int a; virtual void disp(){cout << "Parent" << endl;} }; class Base:public Parent{ public: //Base::disp tries to override Parent::disp void disp(){cout << "Base" << endl;} }; class Base1:public Parent{ public: //Base1::disp tries to override Parent::disp void disp(){cout << "Base" << endl;} }; class Derived:public Base1,public Base { //Both Base::1 and Base::disp try to override Parent::disp }; int main() { Derived d; Base1 *b=&d; //Base1

8 | 11

C++ Inheritance

b->disp(); //no ambiguties since Base::disp is called }


 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 virtual functions  If a virtual function is called with a pointer or reference to a class object, the type of the class object is not used to determine the access of the virtual function. Instead, the type of the pointer or reference to the class object is used.

class Parent{ public: int a; virtual void disp(){cout << "Parent" << endl;} private: virtual void disp1(){cout << "Parent" << endl;} }; class Base:public Parent{ private: //Base::disp tries to override Parent::disp void disp(){cout << "Base" << endl;} public: void disp1(){cout << "Base" << endl;} }; int main() { Parent d; Base b1; Parent *b=&b1; //this does not give error since disp is public in parent //though private in the base class b->disp(); //this gives error since disp1 is private in parent //though public in the base class b->disp1(); }
 An abstract class is a class that is designed to be specifically used

9 | 11

C++ Inheritance as a base class. . An abstract class contains at least one pure virtual function
 You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.

class parent { virtual void f()=0; }


 You cannot use an abstract class as a parameter type, a function return 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

class Parent{ public: virtual void f()=0; }; int main() { Parent a; //this line will give error Parent *a1;//this is allowed }
 A function declaration cannot have both a pure specifier and a definition.A pure declaration forces a definition in the derived class.  Virtual member functions are inherited. A class derived from an abstract base class will also be abstract unless you override each pure virtual function in the derived class.  Note that you can derive an abstract class from a nonabstract class, and you can override a non-pure virtual function with a pure virtual function.  You can call member functions from a constructor or destructor of an abstract class.  However, the results of calling (directly or indirectly) a pure virtual function from its constructor are undefined.

10 | 11

C++ Inheritance

class Parent{ public: virtual void f()=0; Parent() { f();//this will issue a warning } };
 If you declare a base class destructor as virtual, a derived class destructor will override that base class destructor, even though destructors are not inherited.

11 | 11