Multiple
repetition of the data member can be corrected by changing the derived class
into virtual base class. Any base class which is declared using the keyword
virtual is called a virtual base class. Virtual base class is useful method to
avoid unnecessary repetition of the same data member in the multiple
inheritance hierarchies.
When
a class is made a virtual base class C++ takes necessary care to section that
only one copy of that class inherited, regardless of how many inheritance pass
exists between the virtual base class and derived class.
It is used when multiple path inheritance.
Example:
#include<iostream.h>
#include<conio.h>
class
student
{
int id;
char name[20];
public:
void get();
};
void
student::get()
{
cout<<”Enter Student Info =
”;
cin>>id>>name;
}
class
theory : virtual public student
{
Protected:
int english,
french, maths;
public:
void getdata();
};
void
theory :: getdata()
{
cout<<”Enter Marks = ”;
cin>>english>>french>>maths;
}
class sports : virtual public student
{
protected:
int
sm;
public:
void
takes();
};
void
sports :: takes()
{
cout<<”Enter Sports Marks = ”;
cin>>sm;
};
class
result : public theory, public sports
{
float
avg;
int total;
public:
void putdata();
};
void result :: putdata()
{
total
= english+french+maths+sm;
avg
= total/4;
cout<<”Student
Id = “;
cout<<id;
cout<<”Student
Name = “;
cout<<name;
cout<<”Theory
Marks of English, French, maths = ”;
cout<<english<<french<<maths;
cout<<”Sport
Marks = ”
cout<<sm;
cout<<”Total
= ”;
cout<<total;
cout<<”Average
= ”;
cout<<avg
};
void main()
{
clrscr();
result rs;
rs.get();
rs.getdata();
rs.takes();
rs.putdata();
getch();
}
c programming codes
ReplyDeletec code - Search An Element in Linked List