-
base and derived class
Hi everyone,
I am trrying to figure out the difference between a base class and a derived class.
If a class includes another class, does that make it the derived class?
And the included class the base?
Thnx.
Kim
#ifndef HOURLY_H
#define HOURLY_H
#include "employ.h"
class HourlyWorker : public Employee {
public:
HourlyWorker( const char*, const char*, double, double );
double getPay() const;
void print() const;
private:
double wage;
double hours;
};
#endif
#ifndef EMPLOY_H
#define EMPLOY_H
class Employee {
public:
Employee( const char *, const char * ); // constructor
void print() const;
~Employee();
private:
char *firstName;
char *lastName;
};
#endif
-
In the example you give Employee is the base, and HourlyWorker the derived class. While you could say that HourlyWorker includes Employee, that terminology might confuse inheritance and composition (where a class has an object of another class imbedded in it). It's probably clearer to say that a derived class extends a base class.
-
data members
What are the data members in the hourlyWorker object?
Are these it? I am use to seeing int and bool.
double wage;
double hours;
double getPay() constI want to rewrite the code in composition instead of inheritance. If someone could just get me started I think I could finish.
Thanks a million,
Kim
class HourlyWorker : public Employee {
public:
HourlyWorker( const char*, const char*, double, double );
double getPay() const; // calculate and return salary
void print() const; // overridden base-class print
private:
double wage; // wage per hour
double hours; // hours worked for week
};
#endif