C++ Inheritance for Programming

C++ Inheritance for Programming
Slide Note
Embed
Share

Delve into the intricate world of C++ inheritance in Lecture 5 of the CSC.270 course on Programming Languages. Explore how inheritance fosters code reusability and flexibility, building upon existing classes to create new ones with enhanced functionalities. Learn the nuances of base and derived classes, polymorphism, and more to elevate your C++ programming skills.

  • C++
  • Inheritance
  • Programming Languages
  • CSC.270
  • Polymorphism

Uploaded on Mar 08, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


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

  2. Employee.h #ifndef #define #include EMPLOYEE_H EMPLOYEE_H <string> using namespace std; class Employee public: Employee(); Employee(string theName, string theSsn); string getName() const; string getSsn() const; double getNetPay() const; {

  3. void void void void setName(string newName); setSsn(string newSsn); setNetPay(double newNetPay); printCheck() const; private: string name; string ssn; double netPay; }; #endif

  4. Employee.cpp #include #include #include #include "Employee.h" <cstring> <cstdlib> <iostream> 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 } string { Employee::getName() const return name; }

  7. string { Employee::getSsn() const return ssn; } double { Employee::getNetPay() const return netPay; } void { Employee::setName(string newName) name = newName; }

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

  9. void { Employee::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 #ifndef #define #include HOURLYEMPLOYEE_H HOURLYEMPLOYEE_H <string> #include "Employee.h"

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

  12. HourlyEmployee.cpp #include #include #include "HourlyEmployee.h" <string> <iostream> 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 } void { HourlyEmployee::setRate(double newWageRate) wageRate = newWageRate; }

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

  15. void { HourlyEmployee::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 #ifndef #define #include #include SALARIEDEMPLOYEE_H SALARIEDEMPLOYEE_H <string> "Employee.h" using namespace std;

  18. class SalariedEmployee : public Employee { public: SalariedEmployee(void); SalariedEmployee(string theName, string theSsn, double theWeeklySalary); getSalary(void) const; setSalary(double newSalary); printCheck(void); //weekly double void void private: double salary; // weekly }; #endif SALARIEDEMPLOYEE_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" <iostream> SalariedEmployee::SalariedEmployee(void) : Employee( ), salary(0) { // deliberately empty }

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

  21. void { SalariedEmployee::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 #ifndef #define #include EMPLOYEE_H EMPLOYEE_H <string> using namespace std; class Employee public: Employee(); Employee(string theName, string theSsn); string getName() const; string getSsn() const; double getNetPay() const; void setName(string newName); {

  24. void void void setSsn(string newSsn); setNetPay(double newNetPay); printCheck() const; protected: string name; string ssn; double netPay; }; #endif

  25. Using protected Properties void { HourlyEmployee::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"; }

More Related Content