Q:
C++ Virtual Functions | Find output programs | Set 3
belongs to collection: C++ find output programs
C++ find output programs
- C++ Operators | Find output programs | Set 1
- C++ Operators | Find output programs | Set 2
- C++ const Keyword | Find output programs | Set 1
- C++ const Keyword | Find output programs | Set 2
- C++ Manipulators | Find output programs | Set 1
- C++ Manipulators | Find output programs | Set 2
- C++ Reference Variable | Find output programs | Set 1
- C++ Reference Variable | Find output programs | Set 2
- C++ Conditional Statements | Find output programs | Set 1
- C++ Conditional Statements | Find output programs | Set 2
- C++ Switch Statements | Find output programs | Set 1
- C++ Switch Statements | Find output programs | Set 2
- C++ goto Statement | Find output programs | Set 1
- C++ goto Statement | Find output programs | Set 2
- C++ goto Statement | Find output programs | Set 2
- C++ Looping | Find output programs | Set 1
- C++ Looping | Find output programs | Set 2
- C++ Looping | Find output programs | Set 3
- C++ Looping | Find output programs | Set 4
- C++ Looping | Find output programs | Set 5
- C++ Default Argument | Find output programs | Set 1
- C++ Default Argument | Find output programs | Set 2
- C++ Arrays | Find output programs | Set 1
- C++ Arrays | Find output programs | Set 2
- C++ Arrays | Find output programs | Set 3
- C++ Arrays | Find output programs | Set 4
- C++ Arrays | Find output programs | Set 5
- C++ Strings | Find output programs | Set 1
- C++ Strings | Find output programs | Set 2
- C++ Strings | Find output programs | Set 3
- C++ Strings | Find output programs | Set 4
- C++ Strings | Find output programs | Set 5
- C++ Class and Objects | Find output programs | Set 1
- C++ Class and Objects | Find output programs | Set 2
- C++ Class and Objects | Find output programs | Set 3
- C++ Class and Objects | Find output programs | Set 4
- C++ Class and Objects | Find output programs | Set 5
- C++ Constructor and Destructor | Find output programs | Set 1
- C++ Constructor and Destructor | Find output programs | Set 2
- C++ Constructor and Destructor | Find output programs | Set 3
- C++ Constructor and Destructor | Find output programs | Set 4
- C++ Constructor and Destructor | Find output programs | Set 5
- C++ this Pointer | Find output programs | Set 1
- C++ this Pointer | Find output programs | Set 2
- C++ this Pointer | Find output programs | Set 3
- C++ Structures | Find output programs | Set 1
- C++ Structures | Find output programs | Set 2
- C++ Structures | Find output programs | Set 3
- C++ Structures | Find output programs | Set 4
- C++ Structures | Find output programs | Set 5
- C++ Friend Function | Find output programs | Set 1
- C++ Friend Function | Find output programs | Set 2
- C++ Function Overloading | Find output programs | Set 1
- C++ Function Overloading | Find output programs | Set 2
- C++ Function Overloading | Find output programs | Set 2
- C++ Function Overloading | Find output programs | Set 3
- C++ Static Variables and Functions | Find output programs | Set 1
- C++ Static Variables and Functions | Find output programs | Set 2
- C++ Static Variables and Functions | Find output programs | Set 3
- C++ Static Variables and Functions | Find output programs | Set 4
- C++ Static Variables and Functions | Find output programs | Set 5
- C++ Operator Overloading | Find output programs | Set 1
- C++ Operator Overloading | Find output programs | Set 2
- C++ Operator Overloading | Find output programs | Set 3
- C++ Operator Overloading | Find output programs | Set 4
- C++ Operator Overloading | Find output programs | Set 5
- C++ Enumeration | Find output programs
- C++ Inheritance | Find output programs | Set 1
- C++ Inheritance | Find output programs | Set 2
- C++ Inheritance | Find output programs | Set 3
- C++ Inheritance | Find output programs | Set 4
- C++ Inheritance | Find output programs | Set 5
- C++ Exceptional Handling | Find output programs | Set 1
- C++ Exceptional Handling | Find output programs | Set 2
- C++ Exceptional Handling | Find output programs | Set 3
- C++ Virtual Functions | Find output programs | Set 1
- C++ Virtual Functions | Find output programs | Set 2
- C++ Virtual Functions | Find output programs | Set 3
- C++ Templates | Find output programs | Set 1
- C++ Templates | Find output programs | Set 2
- C++ Preprocessor | Find output programs | Set 1
- C++ Preprocessor | Find output programs | Set 2
- C++ Preprocessor | Find output programs | Set 3
- C++ Signal Handling | Find output programs | Set 1
- C++ Namespace | Find output programs | Set 1
- C++ Namespace | Find output programs | Set 2
- C++ Dynamic Memory Allocation | Find output programs | Set 1
- C++ Dynamic Memory Allocation | Find output programs | Set 2
- C++ Dynamic Memory Allocation | Find output programs | Set 3
Answer Program 1:
Output:
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.
Answer Program 2:
Output:
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.
Answer Program 3:
Output:
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.
Answer Program 4:
Output:
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.
Answer Program 5:
Output:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer