Total Answers and Comments: 3
Last Update: April 16, 2008 Asked by: Sunder123
Submitted by : chrisdeepthi virtual inheritanceAbove answer was rated as good by the following members: suri7g
April 14, 2008 16:41:26 #2
ap.genius
Member Since: April 2008 Contribution: 1
RE: Dangers of public unsigned, diamond-shaped inheritance Duplicate copies can be avoided using 'virtual' keyword while making ancestral class...
The example illustrates it better:
#include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> class emp {private: char n[25] sex id[10]; public: void get1() {cout<<"ENTER NAME EMPLOYEE ID AND SEX(M/F) OF EMPLOYEE "; cin>>n>>id>>sex; } void show1() { cout<<"nNAME:"<<n<<"nEMPLOYEE ID:"<<id<<"nSEX:"<<sex; } }; class sal:virtual public emp { public: float sal; void get2() {cout<<"nEnter Salary of employee :Rs "; cin>>sal; } void show2() {cout<<"nSalary :"<<sal; } }; class incent:virtual public emp { public: float in; void get3() {cout<<"nEnter incentives :Rs "; cin>>in; } void show3() {cout<<"nIncentives :Rs"<<in; } }; class tsal:public sal incent {private: float t; public: void get4() {get1(); get2(); get3(); } void show4() {show1(); show2(); show3(); t sal+in; cout<<"nTotal salary :Rs"<<t; } }; void main() {clrscr(); tsal t; t.get4(); t.show4(); getch(); }
Is this answer useful? Yes | No
Go To Top