1 / 33

CIS162AB - C++

CIS162AB - C++. Inheritance Juan Marquez 12_inheritance.ppt. Overview of Topics. Inheritance Base and Derived Classes Constructor Overloading Base Initialization List Constructors in Derived Class Function Redefinition vs Overloading Function Signatures. Inheritance.

johnna
Download Presentation

CIS162AB - C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS162AB - C++ Inheritance Juan Marquez 12_inheritance.ppt

  2. Overview of Topics • Inheritance • Base and Derived Classes • Constructor Overloading • Base Initialization List • Constructors in Derived Class • Function Redefinition vs Overloading • Function Signatures

  3. Inheritance Inheritance is the process by which a new class is created from another class, and the new class has additional member variables and/or functions.

  4. Base Class Parent Class Super Class Employee Derived Class Child Class Sub Class SalariedEmployee HourlyEmployee Inheritance Terminology

  5. Labels class Employee { private: protected: \\use protected instead of private \\ in base class public: };

  6. protected: • Use protected when you might expect private. • A protected member is the same as a private member to any other class except a class derived from the base class, or derived from a class derived from the base class. • In the derived class it is private.

  7. Base Class - Employee class Employee { protected: string name, ssn; double gross; public: Employee(); Employee(string newName, string newSSN); void displayGross(); void calcPay(); };

  8. Constructor Overloading • Overloading is the same function name, but different types or number of parameters. • Constructors can be overloaded as well. • Default constructor has no parameters by definition.

  9. Constructor with Parameters • Constructors with parameters is allowed. • Values to be assigned to members can be passed when an object is declared. Employee emp1; //no arguments = default Employee emp2 (“Joe Cool”, “123-45-6789”);

  10. Derived Class - SalariedEmployee class SalariedEmployee : public Employee { private: double salary; public: SalariedEmployee(); void calcPay(); };

  11. Access Specifier • The access specifier options on the inheritance directive are public, protected, private. • The access specifier, public, in front of the base class Employee means that the public, protected, and private members in the base class are inherited the same way in the derived class.

  12. Derived Class • All member variables and functions defined in Employee are included in the class SalariedEmployee. • New variable (salary) added. • calcPay() is listed again, so it will redefine the calcPay() in Employee.

  13. Derived Class - HourlyEmployee class HourlyEmployee : public Employee { private: double rate; int hours; public: HourlyEmployee(); void calcPay(); };

  14. Derived Class • All member variables and functions defined in Employee are included in the class HourlyEmployee. • calcPay() is listed again, so it will redefine the calcPay() in Employee. • New variables (rate, hours) added.

  15. Functions Employee Employee::Employee() { cout << “Enter name:” cin >> name; cout << “Enter SSN:” cin >> ssn; gross = 0; } Employee::Employee(string na, string sn) { name = na; ssn = sn; gross = 0; }

  16. void Employee::displayGross(); { cout << “Gross = “ << gross; } void Employee::calcPay() { cout << “Error: calcPay() called for an “ << “undifferentiated employee” }

  17. Base Initialization List • We need to initialize the inherited member variables of the derived class as well as the members defined directly in the derived class. • When defining a constructor you can initialize member variables in a Base Initialize List. This initialization section is part of the heading for the function.

  18. Base Initialization List Option Employee::Employee(string na, string sn): name(na), ssn(sn), gross(0) {//empty body } • This function definition would replace the Employee constructor with parameters previously defined as: Employee::Employee(string na, string sn) { name = na; ssn = sn; gross = 0; }

  19. Default Values • Initialize member variables using base initialization list. • Must be used for “const” members. class Employee { double TAX_RATE = .05; // illegal in class definition } Employee::Employee () : TAX_RATE(.05) { //empty body -- default constructor }

  20. Constructors in Derived Class • A constructor for a derived class should begin with an invocation of a constructor for the base class. • List constructor in base initialization list. SalariedEmployee::SalariedEmployee() : Employee()

  21. Functions SalariedEmployee SalariedEmployee::SalariedEmployee() : Employee() { cout << “Enter salary”; cin >> salary; } void SalariedEmployee::calcPay() { gross = salary; }

  22. Reusable Code • Inheritance allows you to reuse code. • Employee has the code to get name and SSN. • Employee has the code to displayGross(); • The same code is used by SalariedEmployee and HourlyEmployee.

  23. Protected Members • Protected members are the same as private members in the base and derived class. • Member functions defined in a derived class can reference or call protected members directly within the derived class. • For example SalariedEmployee::calcPay() can reference gross defined in Employee which is protected.

  24. Private Members • Private members in the base class cannot be referenced or called directly by members of the derived class. • Private members are still inherited, but must use accessors.

  25. Functions HourlyEmployee HourlyEmployee::HourlyEmployee() : Employee() { cout << “Enter pay rate”; cin >> rate; cout << “Enter hours worked”; cin >> hours; } void HourlyEmployee:: calcPay() { gross = rate * hours; }

  26. Redefinition • Do not list member functions from base unless you want to change the definition. • When a member function is redefined in a derived class you must list its prototype in the class definition, even though the prototype is the same as in the base class. • See calcPay() in each derived class.

  27. Redefinition vs Overloading • A function is redefined when the new function in the derived class has the same name, and the exact number and types of parameters. • Overloading occurs when it is the same name, but different types or number of parameters.

  28. Function Signatures • Signature includes function name and the sequence of types in the parameter list.Employee(string, string); • When redefined, the function in the base and derived class have the same signature, but the derived overrides the base. • When overloaded, they have different signatures.

  29. Using Derived Classes • In the following code, what happens… • when each object is declared? • after each call to calcPay()? • after each call to displayGross()?

  30. void main() { Employee emp; emp.calcPay(); emp.displayGross(); Employee emp2 (“Joe Cool”, “123-45-6789”); SalariedEmployee empSalary; empSalary.calcPay(); empSalary.displayGross(); HourlyEmployee empHourly; empHourly.calcPay(); empHourly.displayGross(); }

  31. Objects as Function Arguments • An object of a derived class is also an object of the base class. base: Employee derived: SalariedEmployee, HourlyEmployee • Object types of SalariedEmployee and HourlyEmployee can be passed through a parameter type of Employee.

  32. Objects as Arguments - Example • Just like an ofstream can be passed through an ostream (see P11 SalesPersonClass).void outputSalesInfo(ostream& target);//ostream can handle cout or fileOut (ofstream)void displayName(Employee& emp);displayName(empSalary);displayName(empHourly); • However, only the members defined in the base are available in the function (name, ssn, gross).

  33. Summary • Inheritance • Base and Derived Classes • Constructor Overloading • Base Initialization List • Constructors in Derived Class • Function Redefinition vs Overloading • Function Signatures

More Related