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

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

Program 1:

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

class A {
public:
    virtual void fun() = 0;
};

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

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

int main()
{
    A* p;
    B ob2;
    C ob3;

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

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

    return 0;
}

Output:

B::fun() called
C::fun() called

Explanation:

Let's understand the above program. Here we created classes A, B, and C.

Class A contains the pure virtual function fun() and we inherit class A into B and C. Both B and C also contain a function with the same name. 

We used a virtual keyword with the fun() function of class B and C, but there is no need of a virtual keyword with the fun() function of class B and C, we are overriding the function fun() in B and C class. 

Now look to the main() function, here we created object a pointer "p" of class A. and we created object ob2 of class B and object ob3 of class C.

First, we assigned the address of "ob2" to pointer "p" then it will call function fun() of class B, then we assigned the address of "ob3" to the pointer "p" then it will call function fun() of class C. because Here we created a pure virtual function in the parent class A then it will perform the dynamic binding.

Program 2:

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

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

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

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

class D : public B, public C {
public:
    virtual void funD()
    {
        cout << "funD() called" << endl;
    }
};

int main()
{
    D obj;

    obj.funA();

    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:41:9: error: request for member ‘funA’ is ambiguous
     obj.funA();
         ^~~~
main.cpp:7:18: note: candidates are: virtual void A::funA()
     virtual void funA()
                  ^~~~
main.cpp:7:18: note:                 virtual void A::funA()

Explanation:

The above code will generate a syntax error. Because here we inherit class A from into class B and C. and then class D inherits B and C, then function funA() inherits from through B and C. then we call function funcA() from the object of class D. then it will be an ambiguous function call, then we need to use the concept of virtual base class to resolve the problem.

Program 3:

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

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

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

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

class D : public B, public C {
public:
    void funD()
    {
        cout << "funD() called" << endl;
    }
};

int main()
{
    D obj;

    obj.funA();

    return 0;
}

Output:

funA() called

Explanation:

Let's understand the above program, in the above program, we created 4 classes A, B, C, and D.

Class A is inherited by B and C, and B and C classes are inherited by class D. then to resolve ambiguous function class of funA(), we used the concept of the virtual base class using "virtual" keyword during inheritance, which given below:

class B: virtual public A
class C: virtual public A

Using virtual base class only a single copy of funA() is inherited in class D, then we can call class funA() using the object of class D.

Program 4:

#include <iostream>
using namespace std;

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

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

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

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

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

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

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

    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:31:7: error: cannot declare variable ‘ob1’ to be of abstract type ‘A’
     A ob1, *p;
       ^~~
main.cpp:4:7: note:   because the following virtual functions are pure within ‘A’:
 class A {
       ^
main.cpp:6:18: note: 	virtual void A::fun()
     virtual void fun() = 0;
                  ^~~

Explanation:

The above program will generate a compilation error. Because we cannot create the object of a class that contains a pure virtual function. Here we created object ob1 of class A. 

A class that contains pure virtual function is known as abstract class.

Program 5:

#include <iostream>
using namespace std;

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

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

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

int main()
{
    A* p;
    B ob2;
    C ob3;

    p = new A();
    p->fun1();

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

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

    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:35:15: error: invalid new-expression of abstract class type ‘A’
     p = new A();
               ^
main.cpp:4:7: note:   because the following virtual functions are pure within ‘A’:
 class A {
       ^
main.cpp:6:18: note: 	virtual void A::fun()
     virtual void fun() = 0;
                  ^~~

Explanation:

The above program will generate a compilation error. Because we cannot create the object of a class that contains a pure virtual function. A class that contains pure virtual function is known as abstract class. Here we initialized pointer p using "new" operator, which is also not allowed with an abstract class.





Comments and Discussions!

Load comments ↻





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