Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CIS162AB - C++ Inheritance Juan Marquez 12_inheritance.ppt."— Presentation transcript:

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

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

3 CIS162AB3 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 CIS162AB4 Inheritance Terminology Base Class Parent Class Super Class Employee Derived Class Child Class Sub Class SalariedEmployee HourlyEmployee

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

6 CIS162AB6 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 CIS162AB7 Base Class - Employee class Employee { protected: string name, ssn; double gross; public: Employee(); Employee(string newName, string newSSN); void displayGross(); void calcPay(); };

8 CIS162AB8 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 CIS162AB9 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”, “ ”);

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

11 CIS162AB11 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 CIS162AB12 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 CIS162AB13 Derived Class - HourlyEmployee class HourlyEmployee : public Employee { private: double rate; int hours; public: HourlyEmployee(); void calcPay(); };

14 CIS162AB14 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 CIS162AB15 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 CIS162AB16 void Employee::displayGross(); { cout << “Gross = “ << gross; } void Employee::calcPay() { cout << “Error: calcPay() called for an “ << “undifferentiated employee” }

17 CIS162AB17 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 CIS162AB18 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 CIS162AB19 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 CIS162AB20 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 CIS162AB21 Functions SalariedEmployee SalariedEmployee::SalariedEmployee() : Employee() { cout << “Enter salary”; cin >> salary; } void SalariedEmployee::calcPay() { gross = salary; }

22 CIS162AB22 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 CIS162AB23 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 CIS162AB24 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 CIS162AB25 Functions HourlyEmployee HourlyEmployee::HourlyEmployee() : Employee() { cout << “Enter pay rate”; cin >> rate; cout << “Enter hours worked”; cin >> hours; } void HourlyEmployee:: calcPay() { gross = rate * hours; }

26 CIS162AB26 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 CIS162AB27 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 CIS162AB28 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 CIS162AB29 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 CIS162AB30 void main() { Employee emp; emp.calcPay(); emp.displayGross(); Employee emp2 (“Joe Cool”, “ ”); SalariedEmployee empSalary; empSalary.calcPay(); empSalary.displayGross(); HourlyEmployee empHourly; empHourly.calcPay(); empHourly.displayGross(); }

31 CIS162AB31 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 CIS162AB32 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 CIS162AB33 Summary Inheritance Base and Derived Classes Constructor Overloading Base Initialization List Constructors in Derived Class Function Redefinition vs Overloading Function Signatures


Download ppt "CIS162AB - C++ Inheritance Juan Marquez 12_inheritance.ppt."

Similar presentations


Ads by Google