0% found this document useful (0 votes)
2 views

C++ Virtual Base Class Program and Explanation

The document explains the concept of virtual base classes in C++, addressing the Diamond Problem in multiple inheritance, which can lead to ambiguity when a derived class inherits from two classes that share a common base class. It provides syntax for declaring virtual base classes and includes a simple C++ program that demonstrates how to manage student information using virtual inheritance to avoid data duplication. Additionally, it outlines a separate program for virtual functions, illustrating polymorphism in C++.

Uploaded by

rincejohn80
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C++ Virtual Base Class Program and Explanation

The document explains the concept of virtual base classes in C++, addressing the Diamond Problem in multiple inheritance, which can lead to ambiguity when a derived class inherits from two classes that share a common base class. It provides syntax for declaring virtual base classes and includes a simple C++ program that demonstrates how to manage student information using virtual inheritance to avoid data duplication. Additionally, it outlines a separate program for virtual functions, illustrating polymorphism in C++.

Uploaded by

rincejohn80
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Virtual Base Class

C++ is a language that is widely praised because of its flexibility and


power when used for object-oriented programming. Another of such less
obvious characteristics of C++ is the virtual base class. This concept is
about issues that occur in some kinds of inheritance taxonomic
structures.

Understanding Virtual Base Classes

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.

How to Declare Virtual Base Class in C++?

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”.

class B: virtual public A {

// statement 1

};

class C: public virtual A {

// statement 2

};
Simple Program for Virtual Base Class Using C++ Programming

To calculate the total mark of a student using the concept of virtual


base class.

Simple Program for Virtual Base Class Algorithm/Steps:

Step 1: Start the program.

Step 2: Declare the base class student.

Step 3: Declare and define the functions getnumber() and


putnumber().

Step 4: Create the derived class test virtually derived from the base
class student.

Step 5: Declare and define the function getmarks() and putmarks().

Step 6: Create the derived class sports virtually derived from the base
class student.

Step 7: Declare and define the function getscore() and putscore().

Step 8: Create the derived class result derived from the class test and
sports.

Step 9: Declare and define the function display() to calculate the


total.

Step 10: Create the derived class object obj.

Step 11: Call the function get number(),getmarks(),getscore() and


display().

Step 12: Stop the program.


#include<iostream.h>

#include<conio.h>

class student {

int rno;

public:

void getnumber() {

cout << "Enter Roll No:";

cin>>rno;

void putnumber() {

cout << "\n\n\tRoll No:" << rno << "\n";

};

class test : virtual public student {

public:

int part1, part2;

void getmarks() {

cout << "Enter Marks\n";


cout << "Part1:";

cin>>part1;

cout << "Part2:";

cin>>part2;

void putmarks() {

cout << "\tMarks Obtained\n";

cout << "\n\tPart1:" << part1;

cout << "\n\tPart2:" << part2;

};

class sports : public virtual student {

public:

int score;

void getscore() {

cout << "Enter Sports Score:";

cin>>score;

}
void putscore() {

cout << "\n\tSports Score is:" << score;

};

class result : public test, public sports {

int total;

public:

void display() {

total = part1 + part2 + score;

putnumber();

putmarks();

putscore();

cout << "\n\tTotal Score:" << total;

};

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>

<iostream.h>: This includes the standard input/output library. Note


that in modern C++, this should be replaced with <iostream> and
prefixed with std::.
<conio.h>: This header file is used for console input/output,
providing functions like clrscr() to clear the screen and getch() to wait
for a key press.
2. Class Definitions
2.1. student Class

class student {
int rno; // Roll number of the student
public:
void getnumber(); // Function to input roll number
void putnumber(); // Function to display roll number
};

Attributes: rno holds the roll number.


Methods:
getnumber(): Prompts the user to enter their roll number.
putnumber(): Displays the roll number.

2.2. test Class

class test : virtual public student {


public:
int part1, part2; // Marks in two parts

void getmarks(); // Function to input marks


void putmarks(); // Function to display marks
};

Attributes: part1 and part2 store marks for two assessments.


Methods:
getmarks(): Prompts the user to input the marks for both parts.
putmarks(): Displays the obtained marks.

2.3. sports Class

class sports : public virtual student {


public:
int score; // Sports score

void getscore(); // Function to input sports score


void putscore(); // Function to display sports score
};

Attributes: score holds the sports score.


Methods:
getscore(): Prompts the user to enter their sports score.
putscore(): Displays the sports score.

2.4. result Class

class result : public test, public sports {


int total; // Total score
public:
void display(); // Function to display all information
};
Attributes: total computes the total score.
Methods:
display(): Displays the roll number, marks, sports score, and total
score.

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

Roll No: 200


Marks Obtained
Part1: 90
Part2: 80
Sports Score is: 80
Total Score is: 250

Simple Program for Virtual Functions Using C++ Programming

Simple Program for Virtual Functions Algorithm/Steps:

Step 1: Start the program.

Step 2: Declare the base class base.

Step 3: Declare and define the virtual function show().

Step 4: Declare and define the function display().


Step 5: Create the derived class from the base class.

Step 6: Declare and define the functions display() and show().

Step 7: Create the base class object and pointer variable.

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.

Step 10: Stop the program.

Program

#include<iostream.h>

#include<conio.h>

class base {

public:

virtual void show() {

cout << "\n Base class show:";

void display() {

cout << "\n Base class display:";

}
};

class drive : public base {

public:

void display() {

cout << "\n Drive class display:";

void show() {

cout << "\n Drive class show:";

};

void main() {

clrscr();

base obj1;

base *p;

cout << "\n\t P points to base:\n";

p = &obj1;

p->display();
p->show();

cout << "\n\n\t P points to drive:\n";

drive obj2;

p = &obj2;

p->display();

p->show();

getch();

Output

P points to Base

Base class display


Base class show

P points to Drive

Base class Display


Drive class Show

You might also like