Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 270 – Survey of Programming Languages C++ Lecture 5 – Inheritance.

Similar presentations


Presentation on theme: "CSC 270 – Survey of Programming Languages C++ Lecture 5 – Inheritance."— Presentation transcript:

1 CSC 270 – Survey of Programming Languages C++ Lecture 5 – Inheritance

2 Employee.h #ifndefEMPLOYEE_H #defineEMPLOYEE_H #include using namespace std; class Employee{ public: Employee(); Employee(string theName, string theSsn); stringgetName() const; stringgetSsn() const; doublegetNetPay() const;

3 voidsetName(string newName); voidsetSsn(string newSsn); voidsetNetPay(double newNetPay); voidprintCheck() const; private: string name; string ssn; double netPay; }; #endif

4 Employee.cpp #include #include "Employee.h" using namespace std;

5 Employee::Employee() : name ("No name yet"), ssn ("No number yet"), netPay(0) { // Deliberately empty /* * equivalent to * name = "no name yet"; * ssn = "No number yet"; * netPay = 0; */ }

6 Employee::Employee(string theName, string theSsn) : name (theName), ssn (theSsn), netPay(0) { // Deliberately empty } stringEmployee::getName() const { return name; }

7 stringEmployee::getSsn() const { return ssn; } doubleEmployee::getNetPay() const { return netPay; } voidEmployee::setName(string newName) { name = newName; }

8 voidEmployee::setSsn(string newSsn) { ssn = newSsn; } voidEmployee::setNetPay(double newNetPay) { netPay = newNetPay; }

9 voidEmployee::printCheck() const { cout << "\nERROR: printCheck FUNCTION CALLED" << " FOR AN \n" << "UNDIFFERENTIATED EMPLOYEE. << " Aborting theis progam.\n" << "Check with the author of the" << " program about this bug." << endl; exit(1); }

10 HourlyEmployee.h #ifndefHOURLYEMPLOYEE_H #defineHOURLYEMPLOYEE_H #include #include "Employee.h"

11 class HourlyEmployee : public Employee { public: HourlyEmployee(void); HourlyEmployee(string theName, string theSsn, double theWageRate, double theHours); voidsetRate(double newWageRate); double getRate()const; voidsetHours(double hoursWorked); double getHours() const; voidprintCheck(); private: double wageRate; double hours; }; #endif//HOURLYEMPLOYEE_H

12 HourlyEmployee.cpp #include #include "HourlyEmployee.h" using namespace std; HourlyEmployee::HourlyEmployee(void): Employee( ), wageRate(0), hours(0) { // deliberately empty }

13 HourlyEmployee::HourlyEmployee(string theName, String theSsn,double theWageRate, double theHours) : Employee(theName, theSsn), wageRate(theWageRate), hours(theHours) { // deliberately empty } voidHourlyEmployee::setRate(double newWageRate) { wageRate = newWageRate; }

14 double HourlyEmployee::getRate()const { return wageRate; } voidHourlyEmployee::setHours(double hoursWorked) { hours = hoursWorked; } double HourlyEmployee::getHours() const { return hours; }

15 voidHourlyEmployee::printCheck() { setNetPay(hours * wageRate); cout << "\n----------------------------------" <<"---------------------------\n"; cout << "Pay to the order of " << getName() << endl; cout << "The sum of " << getNetPay() << " Dollars" << endl; cout << "\n----------------------------------" <<"---------------------------\n"; cout << "Check stub: NOT NEGOTIABLE" << endl; cout << "Employee Number: " << getSsn() << endl;

16 cout << " Hourly Employee.\nHours worked: " << hours << " Rate: " << wageRate << " Pay: " << getNetPay() << endl; cout << "\n----------------------------------" <<"---------------------------\n"; }

17 SalariedEmployee.h #ifndefSALARIEDEMPLOYEE_H #defineSALARIEDEMPLOYEE_H #include #include"Employee.h" using namespace std;

18 class SalariedEmployee : public Employee { public: SalariedEmployee(void); SalariedEmployee(string theName, string theSsn, double theWeeklySalary); doublegetSalary(void) const; voidsetSalary(double newSalary); voidprintCheck(void);//weekly private: doublesalary;// weekly }; #endifSALARIEDEMPLOYEE_H

19 SalariedEmployee.cpp // This is the file hoursalariedemployee.cpp // This is the implementation for the class // SalariedEmployee // The interface for the class SalariedEmployee is // in the header salariedemployee.h #include #include "SalariedEmployee.h" SalariedEmployee::SalariedEmployee(void) : Employee( ), salary(0) { // deliberately empty }

20 SalariedEmployee::SalariedEmployee(string theName, string theNumber,double theWeeklyPay) : Employee (theName, theNumber), salary(theWeeklyPay) { // deliberately empty } doubleSalariedEmployee::getSalary(void) const { return salary; } voidSalariedEmployee::setSalary(double newSalary) { salary = newSalary; }

21 voidSalariedEmployee::printCheck(void) { setNetPay(salary); cout << "\n----------------------------------" << "---------------------------\n"; cout << "Pay to the order of " << getName() << endl; cout << "The sum of " << getNetPay() << " Dollars" << endl; cout << "\n----------------------------------" << "---------------------------\n"; cout << "Check stub: NOT NEGOTIABLE" << endl; cout << "Employee Number: " << getSsn() << endl;

22 cout << " Salaried Employee. Regular Pay: " << salary << endl; cout << "\n----------------------------------" << "---------------------------\n";

23 Employee.h with protected Properties #ifndefEMPLOYEE_H #defineEMPLOYEE_H #include using namespace std; class Employee{ public: Employee(); Employee(string theName, string theSsn); stringgetName() const; stringgetSsn() const; doublegetNetPay() const; voidsetName(string newName);

24 voidsetSsn(string newSsn); voidsetNetPay(double newNetPay); voidprintCheck() const; protected: string name; string ssn; double netPay; }; #endif

25 Using protected Properties voidHourlyEmployee::printCheck() { setNetPay(hours * wageRate); cout << "\n----------------------------------" <<"---------------------------\n"; cout << "Pay to the order of " << name << endl; cout << "The sum of " << netPay << " Dollars" << endl; cout << "\n----------------------------------" <<"---------------------------\n"; cout << "Check stub: NOT NEGOTIABLE" << endl; cout << "Employee Number: " << ssn << endl;

26 cout << " Hourly Employee.\nHours worked: " << hours << " Rate: " << wageRate << " Pay: " << netPay << endl; cout << "\n----------------------------------" <<"---------------------------\n"; }


Download ppt "CSC 270 – Survey of Programming Languages C++ Lecture 5 – Inheritance."

Similar presentations


Ads by Google