Consider a situation where multilevel multiple and hierarchical all the three kinds are inherited.
In the above figure the base class is inherited by both Derived1 and Derived2. Derived 3 directly inherits both Derived and Derived2.
All the public and protected member of base are inherited into Derived3 twice through both Derived 1 and Derived2.
Therefore Derived3 would have duplicate sets of members inherited. This causes ambiguity when member of base is used by Derived.
To Resolve this ambiguity, cpp includes a mechanism by which only one copy of base will be included in derived3. And this feature is called as virtual base class.
When a class is made virtual, cpp takes necessary care to inherit only only one copy of the class.
The keyword virtual precedes the base class access specifiers when it is inherited by derived class.
Ex:
Class Base
{
};
Class Derived1 : virtual public Base
{
};
Class Derived2 : virtual public Base
{
};
Class Derived3 : public Derived1, public Derived2
{
};
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollno;
public:
void getrollno(int n)
{
rollno=n;
}
void display1( )
{
cout<<"\n\n\tRoll no is--->"<<rollno;
}
};
class test:virtual public student
{
protected:
int cpp;
int rdbms;
public:
void getmarks(int c, int r)
{
cpp=c;
rdbms=r;
}
void display2( )
{
cout<<"\n\n\tMarks of cpp is--->"<<cpp;
cout<<"\n\n\tMarks of rdbms is--->"<<rdbms;
}
};
class spots:virtual public student
{
protected:
int sportwt;
public:
void getsportmark(int s)
{
sportwt=s;
}
void display3( )
{
cout<<"\n\n\tSport Marks is--->"<<sportwt;
}
};
class rest:public test,public sport
{
protected:
int total;
public:
void calresult( )
{
total=cpp+rdbms+sportwt;
}
void display( )
{
display1( );
display2( );
display3( );
cout<<"\n\n\tTotal Result is--->"<<total;
}
};
void main( )
{
result R;
clrscr( );
R.getrollno(101);
R.getmarks(78,87)
R.getsportmark(10);
R.calresult( );
R.display( );
getch( );
}
Output:-
Roll no is--->101
Marks of cpp is--->78
Marks of rdbms is--->87
Sport Marks is--->10
Total Result is--->175
In the above figure the base class is inherited by both Derived1 and Derived2. Derived 3 directly inherits both Derived and Derived2.
All the public and protected member of base are inherited into Derived3 twice through both Derived 1 and Derived2.
Therefore Derived3 would have duplicate sets of members inherited. This causes ambiguity when member of base is used by Derived.
To Resolve this ambiguity, cpp includes a mechanism by which only one copy of base will be included in derived3. And this feature is called as virtual base class.
When a class is made virtual, cpp takes necessary care to inherit only only one copy of the class.
The keyword virtual precedes the base class access specifiers when it is inherited by derived class.
Ex:
Class Base
{
};
Class Derived1 : virtual public Base
{
};
Class Derived2 : virtual public Base
{
};
Class Derived3 : public Derived1, public Derived2
{
};
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollno;
public:
void getrollno(int n)
{
rollno=n;
}
void display1( )
{
cout<<"\n\n\tRoll no is--->"<<rollno;
}
};
class test:virtual public student
{
protected:
int cpp;
int rdbms;
public:
void getmarks(int c, int r)
{
cpp=c;
rdbms=r;
}
void display2( )
{
cout<<"\n\n\tMarks of cpp is--->"<<cpp;
cout<<"\n\n\tMarks of rdbms is--->"<<rdbms;
}
};
class spots:virtual public student
{
protected:
int sportwt;
public:
void getsportmark(int s)
{
sportwt=s;
}
void display3( )
{
cout<<"\n\n\tSport Marks is--->"<<sportwt;
}
};
class rest:public test,public sport
{
protected:
int total;
public:
void calresult( )
{
total=cpp+rdbms+sportwt;
}
void display( )
{
display1( );
display2( );
display3( );
cout<<"\n\n\tTotal Result is--->"<<total;
}
};
void main( )
{
result R;
clrscr( );
R.getrollno(101);
R.getmarks(78,87)
R.getsportmark(10);
R.calresult( );
R.display( );
getch( );
}
Output:-
Roll no is--->101
Marks of cpp is--->78
Marks of rdbms is--->87
Sport Marks is--->10
Total Result is--->175
0 Comments:
Post a Comment