C++ Virtual Base Class Program and Explanation
C++ Virtual Base Class Program and Explanation
The term virtual base class in C++ is used to overcome the Diamond
Problem in Multiple Inheritance. This issue arises because a derived class
is developed from two classes that are themselves evolved from a single
mother class. It can cause uncertainty to determine which of the
versions of the base class the derived class has to use or inherit.
A virtual base class makes certain that only a single edition of the base
class exists in the derived classes hierarchy, hence the elimination of
ambiguity.
Let us consider the above image, Class A is the parent class, and Classes
B and C are the derived classes from Class A. Thus Class B and Class C
have all the properties of Class A.
Next, Class D inherits Class B and Class C. With our knowledge, Class D
will get all the properties from Class B and C, which also has Class A
properties. What do you think will happen when we try to access Class
A’s properties through D? There will be an error because Class D will get
Class A’s properties twice, and the compiler can’t decide what to
output. You’ll see the term “ambiguous” when such a situation occurs.
Syntax
If Class A is considered as the base class and Class B and Class C are
considered as the derived classes of A.
Note: The word “virtual” can be written before or after the word
“public”.
// statement 1
};
// statement 2
};
Simple Program for Virtual Base Class Using C++ Programming
Step 4: Create the derived class test virtually derived from the base
class student.
Step 6: Create the derived class sports virtually derived from the base
class student.
Step 8: Create the derived class result derived from the class test and
sports.
#include<conio.h>
class student {
int rno;
public:
void getnumber() {
cin>>rno;
void putnumber() {
};
public:
void getmarks() {
cin>>part1;
cin>>part2;
void putmarks() {
};
public:
int score;
void getscore() {
cin>>score;
}
void putscore() {
};
int total;
public:
void display() {
putnumber();
putmarks();
putscore();
};
void main() {
result obj;
clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
Explanation
This C++ code defines a program that involves multiple classes using
inheritance, allowing for the organization and management of
student-related data, including roll numbers, marks, and sports
scores. Here's a breakdown of the code:
1. Header Files
#include<iostream.h>
#include<conio.h>
class student {
int rno; // Roll number of the student
public:
void getnumber(); // Function to input roll number
void putnumber(); // Function to display roll number
};
3. Main Function
void main() {
result obj; // Create an object of 'result' class
clrscr(); // Clear the console
obj.getnumber(); // Get roll number
obj.getmarks(); // Get marks
obj.getscore(); // Get sports score
obj.display(); // Display all information
getch(); // Wait for a key press
}
Functionality:
An object obj of class result is created, which inherits from test and
sports.
The program clears the console, collects input for roll number,
marks, and sports scores, and finally displays all the collected
information including the total score.
Summary:
This code demonstrates how to use classes and inheritance in C++ to
create a structured program for managing student information. The
use of virtual inheritance allows for efficient memory management
and avoids potential duplication of data related to the student class,
making it easy to extend functionality with multiple derived classes.
Output:
Enter Roll No: 200
Enter Marks
Part1: 90
Part2: 80
Enter Sports Score: 80
Step 8: Call the functions display() and show() using the base class object
and pointer.
Step 9: Create the derived class object and call the functions display()
and show() using the derived class object and pointer.
Program
#include<iostream.h>
#include<conio.h>
class base {
public:
void display() {
}
};
public:
void display() {
void show() {
};
void main() {
clrscr();
base obj1;
base *p;
p = &obj1;
p->display();
p->show();
drive obj2;
p = &obj2;
p->display();
p->show();
getch();
Output
P points to Base
P points to Drive