Advertisement

Inheritance in C++

Started by June 03, 2001 07:09 PM
2 comments, last by Xtreme 23 years, 8 months ago
hi All, Can some1 give me a hand in how inheritance works in C++ as well as which methods or variables I can access? For example: say you have this code
  
class Employee
{
   private:
      char name[80];
   public:
      void outputname()
      { 
         cout << name; 
      }
      void inputname()
      {
         cin >> name;
      }
};

class Engineer : private Employee
{
   private:
      char id[30];
}

class Manager : public Employee
{
}

class Supervisor : public Employee
{
   Manager::outputname();
}


/*////////////////////////////
main()///////////////////////
///////////////////////////*/
Employee emp; Manager man; Supervisor sup; Engineer eng; main() { cout << "Enter employee details:\n "; emp.inputname(); cout << "Enter Supervisor details:\n"; sup.inputname(); emp.outputname(); sup.outputname(); return 0; }
Now, I don''t understand how Supervisor class needs the explicit call to Manager::outputname() for it to work. Can someone help me? Also, when can you access members of the upper class? Is multiple inheritance possible in C++ and can you use the functions of derived classes and add some of your own? I''m kinda getting confused with inheritance & polymorphism...
Yes! There are kangaroos in Australia but I haven't seen them...yet
Derived classes can access the public and protected methods and variables from the base class. I think you ment for Manager to have the function "outputname".
Supervisor will need to explicitally call "outputname()" if there is more than one class with the "outputname" function.



base class
class Employee
{
private:
char name[80];
public:
virtual void outputname(){cout << date;}
derived classes
class Manager : public Employee
{
void output(){cout << name;}
}



class Supervisor : public Employee
{
void outputname(){cout << nick_name;}
}

class Engineer : public Employee
{
void outputname(){cout << middle_name;}
void salary(){cout << paycheck;
}
Manager is derived from the class Engineer also so it can call the function salary();

class Manager : public Employee , public Employee
{
void outputname(){cout << madien_name;
}
int main()
{
Employee Boss;
Boss.outputname();//will not get expected output
Boss.Manager::outputname();//very clear what to print.

return 0;
}



because when you call "outputname" the program does not know which one to call. If you want a derived class to access data from another derived class you have to derive that class from that derived class also. i.e class Supervisor : public Employee, public Engineer . Now Suprvisor can access Engineer's methods and variableMultiple inheritance is possible but should be used with caution.


-----------------------------
"There are ones that say they can and there are those who actually do."

"...u can not learn programming in a class, you have to learn it on your own."



Edited by - cMADsc on June 3, 2001 11:23:44 PM

Edited by - cMADsc on June 3, 2001 11:31:27 PM

Edited by - cMADsc on June 3, 2001 11:35:37 PM
-----------------------------"There are ones that say they can and there are those who actually do.""...u can not learn programming in a class, you have to learn it on your own."
Advertisement
Howework?

the keywords public, protected, & private are used for two different things.

Inside a class they determine what other objects can access the methods & properties. Public items are accessible to everything, protected items are accessible to sub-classes (classes that derive or inherit from the given class), private items are accessible only to that class.

When you define a new class that inherits from a class the keywords public/protected/private determine the placement of the inherited class within the given sub-class. It sort of clips the protection status of the parent object. i.e. If you inherit as public, everything stays the same, inherit by protected, then everything that was public in the parent class is now protected in the sub-class. Inherit by private and everything in the parent class is now private in the sub-clas.

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
quote:
When you define a new class that inherits from a class the keywords public/protected/private determine the placement of the inherited class within the given sub-class. It sort of clips the protection status of the parent object. i.e. If you inherit as public, everything stays the same, inherit by protected, then everything that was public in the parent class is now protected in the sub-class. Inherit by private and everything in the parent class is now private in the sub-clas.



Magmai Kai Holmlor, can you expand on that please?

To date, I have made classes on their own, ie, with no inheritance. I do know what private & public is when defined under functions & variables but I have no idea how private, protexted, & private classes work for inheritance!
Yes! There are kangaroos in Australia but I haven't seen them...yet

This topic is closed to new replies.

Advertisement