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

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

Program 1:

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

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

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

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

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

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

    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:26:9: error: invalid conversion from ‘A*’ to ‘B*’ [-fpermissive]
     p = &ob1;
         ^~~~

Explanation:

The above code will generate an error, because if we want to implement the concept of dynamic binding then we need to create the virtual function in the base class. and also we can call functions using the base class pointer.

Program 2:

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

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

class B : public A {
public:
    virtual 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:

Let's understand the above program. In the above program, we created two classes A and B. Both contain the function with the same name that is fun(). Class A inherited in class B.

Now look to the main() function, here 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 created the virtual function in the child class then it will useless and perform the static binding.

Program 3:

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

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

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 = &ob2;
    p->fun();

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

    return 0;
}

Output:

main.cpp:7:5: error: ‘pure’ does not name a type
     pure virtual void fun();
     ^~~~
main.cpp: In function ‘int main()’:
main.cpp:33:8: error: ‘class A’ has no member named ‘fun’
     p->fun();
        ^~~
main.cpp:36:8: error: ‘class A’ has no member named ‘fun’
     p->fun();
        ^~~

Explanation:

The above program will generate syntax error because we used the wrong syntax to declare the pure virtual function, the "pure" is an invalid word, it is not a keyword; the declaration of a pure virtual function is given below:

virtual void fun()=0;

Program 4:

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

class A {
public:
    virtual void fun() = 0
    {
        cout << "A::fun() 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 = &ob2;
    p->fun();

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

    return 0;
}

Output:

main.cpp:7:24: error: pure-specifier on function-definition
     virtual void fun() = 0
                        ^

Explanation:

The above program will generate a compilation error, we tried to create a pure virtual function, we cannot define the pure virtual function, we can only declare the pure virtual function and it can be overridden in child class. 

We can declare pure virtual function like:

virtual void fun()=0;

Program 5:

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

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

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 = &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.

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 the pure virtual function in the parent class A then it will perform the dynamic binding.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.