Ad Code

Responsive Advertisement

Virtual class in inheritance in c++.








programs of Virtual class in inheritance:

// virtual class to remove ambiguous in inheritance 
#include<iostream>
using namespace std;

class A{
 int a;
 public:

 void set_a(int a1){
  a=a1;
 }
 int get_a(){
  return a;
 } 
};
class B:virtual public A{
 int b;
 public:

 void set_b(int b1){
  b=b1;
 } 
 void sub(){ 
  cout<<"\n\n substration of class A and class B  : "<<get_a()-b;  
 }
};
class C:virtual public A{
 int c;
 public:

 void set_c(int c1){
  c=c1;
 }
 void addition(){
  cout<<"\n\naddition only class A and class C "<<get_a()+c;
 }
}; 
class D: public B,public C{

}; 
int main(){
 D obj;
 obj.set_a(50);
 obj.set_b(10);
 obj.sub();

 obj.set_a(20);
 obj.set_c(10);
 obj.addition();
 
}



out put:
 substration of class A and class B  : 40
addition only class A and class C : 30


If you like code with vibhu  and would like to contribute, you can also write an article using   This link :

https://docs.google.com/forms/d/e/1FAIpQLScAmvlPvNUz35R-G0nc_zpRVP3o8xlhtFgC3aKPyLetX_RyXg/viewform?pli=1


OR  mail your article to bibhutiswain990@gmail.com.  See your article appearing on the code with vibhu main page and help other students. 
❮ Previous                                                      Next ❯

Post a Comment

0 Comments