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?

Questions by Sunder123

Showing Answers 1 - 3 of 3 Answers

ap.genius

  • Apr 14th, 2008
 

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();
}

  Was this answer useful?  Yes

redleader

  • Apr 16th, 2008
 

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.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions