Write a C++ program to show virtual base class.
AIM:
Write
a C++ program to show virtual base class.
THEORY:
- An ambiguity can
arise when several paths exist to a class from the same base class. This means
that a child class could have duplicate sets of members inherited from a single
base class.
- C++ solves this issue by introducing a virtual base class. When a class is made virtual, necessary care is taken so that the duplication is avoided regardless of the number of paths that exist to the child class.
- C++ solves this issue by introducing a virtual base class. When a class is made virtual, necessary care is taken so that the duplication is avoided regardless of the number of paths that exist to the child class.
- When two or more objects are derived from a common base class, we can
prevent multiple copies of the base class being present in an object derived
from those objects by declaring the base class as virtual when it is being
inherited. Such a base class is known as virtual base class. This can be
achieved by preceding the base class’ name with the word virtual.
SOURCE
CODE:
#include<iostream>
using namespace std;
class A1{
protected:
int
a1;
};
class A2: public virtual A1{
protected:
int
a2;
};
class A3:public virtual A1{
protected:
int
a3;
};
class A4:public A2,A3{
int
a4;
public:
void
get(){
cout<<endl<<"\n
enter values for a1,a2,a3,a4:"<<endl;
cin>>a1>>a2>>a3>>a4;
}
void
put(){
cout<<"a1=
"<<a1<<" "<<"a2=
"<<a2<<" "<<"a3=
"<<a3<<" "<<"a4=
"<<a4<<" ";
}
};
int main(){
A4
a;
a.get();
a.put();
}
Labels: oop through c++ lab
1 Comments:
شركة مكافحة حشرات بالمدينة المنورة
شركة تنظيف بالمدينة المنورة
Post a Comment
Subscribe to Post Comments [Atom]
<< Home