Download presentation
Presentation is loading. Please wait.
Published byChandan R Modified over 2 years ago
1
SUB: OOPS USING C++ [18CS42] TOPIC: Virtual and Pure Virtual Fucntions in C++
2
What is virtual function? Virtual functionVirtual function is a member function that is declared within the base class and can be redefined by the derived class. Its name in the base class and in the derived classes remains the same Its definition in these classes may be different. The virtual function in the derived class is executed through the pointer of the public base class. A virtual function in c++ is declared by writing the word virtual before function declaration in the base class The functions with the same name are declared in the derived classes. The use of the word virtual before function declaration in derived classes is optional.
3
Once a function is declared virtual in a base class, it becomes virtual in all derived classes; even when the keyword virtual is not written in its definition in the derived classes. The derived classes may have different versions of a virtual function, A virtual function may be redefined in the derived classes.
4
#include using namespace std; class base { public: void show() { std::cout << "Base class" << std::endl; } }; class derived1 : public base { public: void show() { std::cout <<"Derived class 1" << std::endl; } }; class derived2 : public base { public: void show() {
5
std::cout << "Derived class 1" << std::endl; } }; class derived2 : public base { public: void show() { std::cout << "Derived class 2" << std::endl; } }; int main() { base *b; derived1 d1; derived2 d2; b=&d1; b->show(); b=&d2; b->show(); return 0;
6
In the above code, we have not used the virtual method. We have created a base class that contains the show() function. The two classes are also created named as 'derived1' and 'derived2' that are inheriting the properties of the base class. Both 'derived1' and 'derived2' classes have redefined the show() function. Inside the main() method, pointer variable 'b' of class base is declared. The objects of classes derived1 and derived2 are d1 and d2 respectively. Although the 'b' contains the addresses of d1 and d2, but when calling the show() method; it always calls the show() method of the base class rather than calling the functions the derived1 and derived2 class.
7
To overcome the above problem, we need to make the method as virtual in the base class. Here, virtual means that the method exists in appearance but not in reality. We can make the method as virtual by simply adding the virtual keyword preceeding to the function. In the above program, we need to add the virtual keyword that precedes to the show() function in the base class shown as below: 1.virtual void show() 2.{ 3. std::cout << "Base class" << std::endl; 4.} 5.;
8
Rules of Virtual Function Virtual functions must be members of some class. Virtual functions cannot be static members. They are accessed through object pointers. They can be a friend of another class. A virtual function must be defined in the base class, even though it is not used. The prototypes of a virtual function of the base class and all the derived classes must be identical. If the two functions with the same name but different prototypes, C++ will consider them as the overloaded functions. We cannot have a virtual constructor, but we can have a virtual destructor Consider the situation when we don't use the virtual keyword
9
Virtual function Example: #include { public: virtual void display() { cout << "Base class is invoked"<<endl; } }; class B:public A { public: void display() { cout << "Derived Class is invoked"<<endl; } };
10
int main() { A* a; //pointer of base class B b; //object of derived class a = &b; a->display(); //Late Binding occurs }
11
Pure Virtual Functions in C++: A pure virtual function is a virtual function in C++ for which we need not to write any function definition and only we have to declare it. It is declared by assigning 0 in the declaration. Pure Virtual functions can be given a small definition in the Abstract class, which you want all the derived classes to have. Still you cannot create object of Abstract class. Also, the Pure Virtual function must be defined outside the class definition. If you will define it inside the class definition, complier will give an error. Inline pure virtual definition is Illegal
12
Characteristics of a pure virtual function A pure virtual function is a "do nothing" function. Here "do nothing" means that it just provides the template, and derived class implements the function. It can be considered as an empty function means that the pure virtual function does not have any definition relative to the base class. Programmers need to redefine the pure virtual function in the derived class as it has no definition in the base class. A class having pure virtual function cannot be used to create direct objects of its own. It means that the class is containing any pure virtual function then we cannot create the object of that class. This type of class is known as an abstract class.
13
Syntax There are two ways of creating a virtual function: virtual void display() = 0; virtual void display() {} Let's take an example, Suppose, we have derived Car, Bike and Cycle classes from the Vehicle class, and we want to calculate the Speed of all these Vehicles. In this case, we can create a pure virtual function named calculateSpeed() in the Vehicle. Since it's a pure virtual function, all derived classes Car, Bike and Cycle must include the calculate Speed() function with implementation. A pure virtual function doesn't have the function body and it must end with =0. For example, class Vehicle { public: // creating a pure virtual function virtual void calculateSpeed() = 0; };
14
ABSTRACT CLASSES: // An abstract class class Test { // Data members of class public: // Pure Virtual Function virtual void show() = 0; /* Other members */}; A pure virtual function is implemented by classes which are derived from a Abstract class. Following is a simple example to demonstrate the same
15
#include using namespace std; class Base { int x; public: virtual void fun() = 0; int getX() { return x; } }; // This class inherits from Base and implements fun() class Derived: public Base { int y; public: void fun() { cout << "fun() called"; } }; int main(void) { Derived d; d.fun(); return 0; }
16
PURE VIRTUAL FUNCTION EXAMPLE: #include using namespace std; // Abstract class class Shape { public: virtual float calculateArea() = 0; // pure virtual function. }; class Square : public Shape { float a; public: Square(float l) { a = l; } float calculateArea() { return a*a; } };
17
class Circle : public Shape { float r; public: Circle(float x) { r = x; } float calculateArea() { return 3.14*r*r ; } }; class Rectangle : public Shape { float l; float b; public: Rectangle(float x, float y) { l=x; b=y; }
18
float calculateArea() { return l*b; } }; int main() { Shape *shape; Square s(3.4); Rectangle r(5,6); Circle c(7.8); shape =&s; int a1 =shape->calculateArea(); shape = &r; int a2 = shape->calculateArea(); shape = &c; int a3 = shape->calculateArea(); std::cout << "Area of the square is " <<a1<< std::endl; std::cout << "Area of the rectangle is " <<a2<< std::endl; std::cout << "Area of the circle is " <<a3<< std::endl; return 0; }
19
THANK YOU
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.