virtual function

virtual function
Introduction:
virtual function is a member function of a base class that can be overridden by a derived class. It allows for polymorphism, where objects of different classes can be treated as objects of a common base class.

Key characteristics:

   - Declared using the `virtual` keyword
   - Can be overridden by derived classes
   - Resolved at runtime (dynamic dispatch)
   - Enables polymorphism
Key points virtual function:

1. Declared using `virtual` keyword
2. Can be overridden by derived classes
3. Resolved at runtime (dynamic dispatch)
4. Enables polymorphism
5. Must be defined in base class
6. Can be pure virtual (abstract)
Virtual function diagram:
Explanation 
The image you've provided illustrates the *working of virtual functions in C++* through the use of *vptr* (virtual pointer) and *vtable* (virtual table). Let me break it down for you:
Concepts to Understand:*
1. Virtual Function: A function in a base class that can be overridden by derived classes. It allows dynamic (runtime) polymorphism, meaning the method that is called is determined by the type of object being referred to, not the type of the pointer or reference.

2. vptr (Virtual Pointer): A pointer in each object that points to the *vtable* of the class to which the object belongs. The *vptr* helps to determine which version of the virtual function to call.

3. vtable (Virtual Table): A table of function pointers maintained for each class with virtual functions. It contains the addresses of the virtual functions that can be called for an object of that class.
Explanation of the Image:

1. Virtual Table (vtable):
   - For each class with virtual functions, the compiler creates a vtable.
   - The vtable stores the addresses of the virtual functions. Each class with virtual functions has its own vtable.

2. Virtual Pointer (vptr):
   - Every object of a class with virtual functions contains a virtual pointer (vptr), which points to the class's vtable.
 
Program virtual function:

#include <iostream>
using namespace std;

class Shape {
public:
    // Virtual function to be overridden by derived classes
    virtual void draw() {
        cout << "Drawing a shape" << endl;
    }
    
    // Virtual destructor to ensure proper cleanup of derived objects
    virtual ~Shape() {
        cout << "Shape Destructor" << endl;
    }
};

class Circle : public Shape {
public:
    // Override the draw method
    void draw() override {
        cout << "Drawing a circle" << endl;
    }
    
    // Destructor for Circle
    ~Circle() override {
        cout << "Circle Destructor" << endl;
    }
};

class Square : public Shape {
public:
    // Override the draw method
    void draw() override {
        cout << "Drawing a square" << endl;
    }
    
    // Destructor for Square
    ~Square() override {
        cout << "Square Destructor" << endl;
    }
};

int main() {
    // Using base class pointer to call derived class methods
    Shape* shape1 = new Circle();
    Shape* shape2 = new Square();
    
    // Call draw method (polymorphism in action)
    shape1->draw(); // Output: Drawing a circle
    shape2->draw(); // Output: Drawing a square
    
    // Clean up
    delete shape1;
    delete shape2;
    
    return 0;
}

Output:
        Drawing a circle
        Drawing a square
        Square Destructor
        Circle Destructor
        Shape Destructor
        Shape Destructor
PROGRAM EXPLAIN 

This C++ code demonstrates object-oriented programming (OOP) concepts, specifically:

Inheritance
The `Circle` and `Square` classes inherit from the `Shape` class using public inheritance.

Polymorphism
The `draw()` method is declared as virtual in the `Shape` class and overridden in the `Circle` and `Square` classes. This allows for polymorphic behavior when calling the `draw()` method through a base class pointer.

Virtual Destructor
The `Shape` class has a virtual destructor to ensure proper cleanup of derived class objects when deleted through a base class pointer.


This C++ code demonstrates object-oriented programming (OOP) concepts, specifically:

Inheritance
The `Circle` and `Square` classes inherit from the `Shape` class using public inheritance.

Polymorphism
The `draw()` method is declared as virtual in the `Shape` class and overridden in the `Circle` and `Square` classes. This allows for polymorphic behavior when calling the `draw()` method through a base class pointer.

Virtual Destructor
The `Shape` class has a virtual destructor to ensure proper cleanup of derived class objects when deleted through a base class pointer.

Code Breakdown
1. Base Class (`Shape`):
    - Declares a virtual `draw()` method.
    - Has a virtual destructor.

2. Derived Classes (`Circle` and `Square`):
    - Override the `draw()` method.
    - Have their own destructors.

3. Main Function:
    - Creates objects of `Circle` and `Square` using base class pointers (`Shape*`).
    - Calls the `draw()` method on these objects, demonstrating polymorphism.
    - Deletes the objects using the base class pointers, ensuring proper cleanup

Conclusion:

Virtual functions enable runtime polymorphism, allowing objects of different classes to be treated as objects of a common superclass. This promotes flexibility and generic code. By overriding virtual functions, subclasses can provide specialized implementations.
                                           Prepared by
                                            Vignesh.S
                                    Ranjith kumar.S
                                     1,st B SC computer science 
                                    Rama krishna mission vidyalaya.                                 Coimbatore Tamil Nadu...

Comments

Post a Comment

Popular posts from this blog

ETHICAL HACKING - WHITE HAT HACKER

Radio Waves

Semantic Technology