GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 160 of 203    Print  
Dangers of public unsigned, diamond-shaped inheritance
What is the danger of using public unsigned integers within a class?
In a diamond-shaped inheritance hierarchy, how to ensure that only 1 copy of parent is created?



  
Total Answers and Comments: 3 Last Update: April 16, 2008     Asked by: Sunder123 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: chrisdeepthi
 
virtual inheritance

Above answer was rated as good by the following members:
suri7g
April 01, 2008 09:15:32   #1  
chrisdeepthi Member Since: March 2008   Contribution: 1    

RE: Dangers of public unsigned, diamond-shaped inheritance
virtual inheritance
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
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
April 16, 2008 09:53:03   #3  
redleader Member Since: April 2008   Contribution: 10    

RE: Dangers of public unsigned, diamond-shaped inheritance
There are at least 2 questions here. The latter is answered but not the former.

If the initial question is answered there would be a requirement to highlight what the dangers are for both public unsigned and multiple inheritance.


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape