Example:2
Declare a class named student with attributes as roll and name. Declare another class internalmarks with attributes as m1 and m2. Declare another class externalmarks with attributes as m3 and m4. Classes internalmarks and externalmarks are derived classes of the class student. Declare another class name result. Class result is the derived class of the classes internalmarks and externalmarks.
Take input for the details are display them.
//example of virtual base class #include<iostream> #include<conio.h> using namespace std; class student { private: int roll; char name[20]; public: void readstddata(); void showstddata(); }; class internalmarks:public virtual student { private: float m1,m2; public: void readmarks(); void showmarks(); float calinternalmarks(); }; class externalmarks:public virtual student { private: float m3,m4; public: void readmarks(); void showmarks(); float calexternalmarks(); }; class result:public internalmarks,public externalmarks { public: float caltotalmarks(); }; void student::readstddata() { cout<<"Enter roll and name "; cin>>roll>>name; } void student::showstddata() { cout<<roll<<" "<<name<<endl; } void internalmarks::readmarks() { cout<<"Enter marks of m1 and m2 "; cin>>m1>>m2; } void internalmarks::showmarks() { cout<<m1<<" "<<m2<<endl; } float internalmarks::calinternalmarks() { return(m1+m2); } void externalmarks::readmarks() { cout<<"Enter marks of m3 and m4 "; cin>>m3>>m4; } void externalmarks::showmarks() { cout<<m3<<" "<<m4<<endl; } float externalmarks::calexternalmarks() { return(m3+m4); } float result::caltotalmarks() { return(calinternalmarks()+calexternalmarks()); } int main() { result r; r.readstddata(); r.internalmarks::readmarks(); r.externalmarks::readmarks(); r.showstddata(); r.internalmarks::showmarks(); r.externalmarks::showmarks(); float t; t=r.caltotalmarks(); cout<<"Total marks "<<t<<endl; return(0); }
Output:
Enter roll and name 101
Sumit
Enter marks of m1 and m2 98
97
Enter marks of m3 and m4 95
96
101 Sumit
98 97
95 96
Total marks 386