Virtual base class program in c++

      No Comments on Virtual base class program in c++

Virtual base class program

Virtual base class program in c++.Declaring base class as a virtual class takes necessary care to see that only one copy of that class is inherited.This program is also a example of hybrid inheritance. Here class scan1 is a base class.

Virtual base class program

Program

#include<iostream.h>
#include<conio.h>
class scan1
{
  public:
  float x,y;
  public:
  void get()
  {
   cout<<"Enter first number:- ";
   cin>>x;
   cout<<"Enter second number:- ";
   cin>>y;
  }
};
class scan2:virtual public scan1
{
 public:
  int a;
 public:
 void res2()
 {
  a=x+y;

  cout<<"Sum of two number is:- "<<a<<endl;
  cout<<"..........................."<<endl;
 }
};
class scan3: virtual public scan1
{
 public:
 int b;
 public:
 void res3()
 {
 b=x-y;
 cout<<"Subtraction of two numbers is:- "<<b<<endl;
   cout<<"..........................."<<endl;
 }
};
class scan4:public scan2,public scan3
{
 public:
 float c;
 public:
 void res4()
 {
  c=a*b;
  cout<<"Multiplication :- "<<c<<endl;

 }

};
void main()
{
 clrscr();
 scan4 s4;
 cout<<".....Numbers for addition..... "<<endl;
 s4.get();
 s4.res2();
 cout<<".....Number for subtraction..... "<<endl;
 s4.get();
 s4.res3();
 cout<<".....Multiplication of above two answer..... "<<endl;
 s4.res4();
 getch();
}

OUTPUT

Virtual base class program


Comment below if you are facing any problem.


Also read

C++ program of multilevel inheritance
Hierarchical inheritance program in c++
Multiple inheritance program in c++

Summary
Virtual base class program in c++
Article Name
Virtual base class program in c++
Description
Declaring base class as a virtual class takes necessary care to see that only one copy of that class is inherited.
Author
Publisher Name
Scanfcode
Publisher Logo

Leave a Reply

Your email address will not be published. Required fields are marked *