C++ Virtual Functions | Find output programs | Set 1

This section contains the C++ find output programs with their explanations on C++ Virtual Functions (set 1).
Submitted by Nidhi, on July 23, 2020

Program 1:

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    virtual float PI = 3.14F;
    virtual float R = 5.2F;
    float AREA = 0;

    AREA = PI * R * R;

    cout << AREA << endl;

    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:7:5: error: ‘virtual’ outside class declaration
     virtual float PI = 3.14F;
     ^~~~~~~
main.cpp:8:5: error: ‘virtual’ outside class declaration
     virtual float R = 5.2F;
     ^~~~~~~

Explanation:

It will generate an error because the virtual keyword cannot be used outside the class.

Program 2:

#include <iostream>
#include <string.h>
using namespace std;

class A {
    int val;

public:
    A()
        : val(10)
    {
    }

    void print()
    {
        cout << val << endl;
    }
};

class B {
    virtual A ob;

public:
    void fun()
    {
        ob.print();
    }
};

int main()
{
    B ob;

    ob.fun();
    return 0;
}

Output:

main.cpp:21:15: error: ‘ob’ declared as a ‘virtual’ field
     virtual A ob;
               ^~

Explanation:

It will generate an error. Here, We created a class A that contains a data member val and a member function print(). Then we created a class B, and we declared an object of class A as a virtual data member. We cannot use a virtual keyword with data members.

Program 3:

#include <iostream>
#include <string.h>
using namespace std;

class A {
public:
    virtual void fun()
    {
        cout << "A::fun() called" << endl;
    }
};

class B {
public:
    void fun()
    {
        cout << "B::fun() called" << endl;
    }
};

int main()
{
    A ob1, *p;
    B ob2;

    p = &ob1;
    p->fun();

    p = &ob2;
    p->fun();

    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:29:10: error: cannot convert ‘B*’ to ‘A*’ in assignment
     p = &ob2;
          ^~~

Explanation:

It will generate an error.

Here, we created two classes A and B, both contain member function fun(). Class A contain fun() function as a virtual.

The main cause of the error, here we did not inherit class A into class B that's why we cannot assign the address of B class object into a pointer of class A.

Program 4:

#include <iostream>
#include <string.h>
using namespace std;

class A {
public:
    virtual void fun()
    {
        cout << "A::fun() called" << endl;
    }
};

class B : public A {
public:
    void fun()
    {
        cout << "B::fun() called" << endl;
    }
};

int main()
{
    A ob1, *p;
    B ob2;

    p = &ob1;
    p->fun();

    p = &ob2;
    p->fun();

    return 0;
}

Output:

A::fun() called
B::fun() called

Explanation:

Here, we created two classes A and B. Both contain the function with the same name that is fun(). Class A inherited in class B.

Class A contains the virtual function. It will use dynamic binding for function calls.

In the main() function, we created object ob1 and a pointer p of class A. And, we created object ob2 of class B.

First, we assigned the address of ob1 to pointer p then it will call function fun() of class A, then we assigned the address of ob2 to the pointer p then it will call function fun() of class B.

Program 5:

#include <iostream>
#include <string.h>
using namespace std;

class A {
public:
    void fun()
    {
        cout << "A::fun() called" << endl;
    }
};

class B : public A {
public:
    void fun()
    {
        cout << "B::fun() called" << endl;
    }
};

int main()
{
    A ob1, *p;
    B ob2;

    p = &ob1;
    p->fun();

    p = &ob2;
    p->fun();

    return 0;
}

Output:

A::fun() called
A::fun() called

Explanation:

Here, we created two classes A and B. Both contain the function with the same name that is fun(). Class A inherited in class B.

In the main() function, we created object ob1 and a pointer p of class A. And, we created object ob2 of class B.

First, we assigned the address of ob1 to pointer p then it will call function fun() of class A, then we assigned the address of ob2 to the pointer p then it will again call function fun() of class A. Because, Here we did not create any virtual function then it will use static binding for function call.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.