ÝNHERÝTANCE clarification is needed

Started by
6 comments, last by tonymontana 20 years, 3 months ago
#include <iostream> using namespace std; class employee { protected: int salary; int age; public: employee():salary(2000),age(20) { } employee(int sal1,int age1):salary(sal1),age(age1) { } void Show() const { cout<<"\nSalary:"<ublic employee { private: int basketballdues; public: laborer():employee(3000,22),basketballdues(500) { } void Show() const { employee::Show(); cout<<"basketball dues"<ublic employee { private: int tennisdues; public: manager():employee(5000,25),tennisdues(700) { } void Show() const { employee::Show(); cout<<"Tennis dues:"<rivate employee { private: int golfdues; public: scientist():employee(8000,35),golfdues(1000) { } scientist(int sal1,int ag1,int golfd1) // this function okey.Ýt is working this way { salary=sal1; age=ag1; golfdues=golfd1; } /* MY question is why this is not working?????? i want to use that instead of the previous one scientist(int sal1,int ag1,int golfd1):salary(sal1),age(ag1),golfdues(golfd1) { } */ void Show()const { employee::Show(); cout<<"Golf dues:"< i know them.But sometimes i am confused. for examplue in Show() memeber function of scientist how it is reaching the Show() member function of employee it shouldn''t ( don''t tell me we used employee::Show() i know what a scope resolution is ) because the class scientist is derived from employee in private way. where i am confused is when we use scope resolution operator in a private class? Can we call a public ,protected,private member function from the base class.Ýf this is so inheritence is a little nonsense.Because a private derived class may access it''s mother.. as you see i am confused. please Enlight me !!!! */ // somebody should teach me how to write embed codes here too..
Essegin Ziki:) bunu bu sitede imza hesabına yazsam kimse anlamaz!!!:)
Advertisement
A class can access all its members (and base classes), public, protected or public.
A derived class can access protected and public members (and base classes) of its base classes
Anything else can only access public members (and base classes).

Scientist has Employee as a private base class. It can use it, but no other class can access (e.g. cast) Scientist as an Employee (private). If the inheritance was protected, classes derived from Scientist would be able to access it as an Employee.

public inheritance: a Labourer is an Employee.
private inheritance: a Scientist is implemented using Employee.

A using declaration will make private base class member function again accessible to other classes. For example:

class Scientist: private Employee{public:   using Employee::Show;}; 


Use [source‍][/source‍] or [code‍][/code‍] blocks. It''s in the forum FAQ, and clicking edit on any post using them would have revealed the ''secret''.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
See the magical source tags at work:

#include <iostream>using namespace std;class Employee{   protected:   int salary;   int age;      public:   Employee() : salary(2000), age(20)   {   }      Employee(int sal1,int age1) : salary(sal1), age(age1)   {   }      void Show() const   {      cout << "Salary: " << salary << "Age: " << age << endl;   }      Employee operator +(int k)    {      age+=k;      return Employee(salary,age);   }      Employee operator +=(int k1)   {      age+=k1;      return Employee(salary,age);   }};class Laborer : public Employee{   private:   int basketballdues;      public:   Laborer():Employee(3000,22),basketballdues(500)   {   }      void Show() const   {      Employee::Show();      cout << "basketball dues";    }};class Manager : public Employee{   private:   int tennisdues;      public:   Manager() : Employee(5000,25), tennisdues(700)   {   }      void Show() const   {      Employee::Show();      cout << "Tennis dues:";    }};class Scientist : private Employee{   private:   int golfdues;      public:   Scientist() : Employee(8000,35), golfdues(1000)   {   }      Scientist(int sal1, int ag1, int golfd1) // this function okey.Ýt is working this way   {      salary=sal1;      age=ag1;      golfdues=golfd1;   }      /* MY question is why this is not working?????? i want to use that instead of the previous one   Scientist(int sal1,int ag1,int golfd1):salary(sal1),age(ag1),golfdues(golfd1)   {   }   */      void Show()const   {      Employee::Show();      cout << "Golf dues:";    }      Scientist operator +(int k)   {      Employee::operator +(k);      return Scientist(salary, age, golfdues);   }};int main(){   Laborer l1;   Manager m1;   const Scientist s1;;   l1.Show();   m1.Show();   s1.Show();   return 0;}

Now, Fruny already pointed out what public, protected and private do.
You asked
/* MY question is why this is not working?????? i want to use that instead of the previous one*/Scientist(int sal1,int ag1,int golfd1) : salary(sal1),age(ag1),golfdues(golfd1){}  

Scientist is a subclass of Employee and therefore it must call an Employee constructor upon initialization. Like you do in the first Scientist constructor. In the second Scientist constructor you can assign the Employee members because the Employee part of the object is already constructed at that point.

Oh, the + operators look very weird? They make absolutely no sense. Why does + increase the age and not the salary? And why return a copy of an Employee after increasing it's age? It is much clearer if you use plane member functions like setAge(int age) and setSalary(int salary).

[note]I altered the code style a bit to make it more readable (for me)[/note]
[edit]replaced tabs with spaces[/edit]

[edited by - Rule on January 7, 2004 3:12:57 PM]
If you want a subclass to be able to override a function in a parent class, then in the parent class, you _must_ declare that function using the virtual keyword before it''s declaration.
thanks fruny & rule..
i have just realized my mistake.in the first one(the one which is working )a default constructor of employee is called to construct the scientist.but in second i neither give a chance for using it''s default constructor to compiler nor i called a constructor myself..So i tried to assing a value that is already did''nt created.i am right.am not i?
Essegin Ziki:) bunu bu sitede imza hesabına yazsam kimse anlamaz!!!:)
why are your I == Ý?? French keyboard???

Edit: had a little i


It's Maxd Gaming, put in an underscore and I will beat you with a rubber ducky!
{ Check out my Forum } { My First Space Art (Ever) }{ My Second Space Art (Ever) }{ My upcoming space mod for Battlefield: 1942. }

[edited by - Maxd Gaming on January 7, 2004 6:52:43 PM]
The Untitled RPG - |||||||||| 40%Free Music for your gamesOriginal post by capn_midnight 23yrold, is your ass burning from all the kissing it is recieving?
quote:Original post by maxd gaming
why are your I == Ý?? French keyboard???


I''d say a Turkish keyboard. I do not have ''Ý'' on mine. I do have § , µ, £, ° and ² (and a few accented letters) though
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
as you guessed because of turkish keyboard
ihave also ğ ç ş
Essegin Ziki:) bunu bu sitede imza hesabına yazsam kimse anlamaz!!!:)

This topic is closed to new replies.

Advertisement