Multiplication of 2 numbers using virtual function
In this, we are going see a basic program on Multiplication Of Two Numbers using Virtual function  in C++ Programming Language.



The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <conio.h>

// Multiplication of 2 numbers using virtual function


class base
{
    public:
    int n1, n2;
    virtual int numbers()
    {
        return n1 + n2;
    }
};

class derived : public base
{
    public:
    int numbers()
    {
        return n1 * n2;
    }
};

void main()
{
    clrscr();
    derived obj;
    base *obj2 = &obj;
    cout << "Enter two numbers: ";
    cin >> obj2->n1 >> obj2->n2;
    cout << "The Multiplication is : " << obj2->numbers();
    getch();
}

//.......Coded by SHREYA IDATE

The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

class base
{
    public:
    int n1, n2;
    virtual int numbers()
    {
        return n1 + n2;
    }
};

class derived : public base
{
    public:
    int numbers()
    {
        return n1 * n2;
    }
};

int main()
{
    derived obj;
    base *obj2 = &obj;
    cout << "Enter two numbers: ";
    cin >> obj2->n1 >> obj2->n2;
    cout << "The Multiplication is : " << obj2->numbers();
}

//.......Coded by SHREYA IDATE

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post