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

Post on 16-Dec-2015

217 views 2 download

Tags:

transcript

CIS162AB - C++

Inheritance Juan Marquez

12_inheritance.ppt

CIS162AB 2

Overview of Topics

• Inheritance

• Base and Derived Classes

• Constructor Overloading

• Base Initialization List

• Constructors in Derived Class

• Function Redefinition vs Overloading

• Function Signatures

CIS162AB 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.

CIS162AB 4

Inheritance Terminology

• Base Class• Parent Class• Super Class

• Employee

Derived Class

Child Class

Sub Class

SalariedEmployee

HourlyEmployee

CIS162AB 5

Labels

class Employee

{

private:

protected: \\use protected instead of private

\\ in base class

public:

};

CIS162AB 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.

CIS162AB 7

Base Class - Employeeclass Employee{protected:

string name, ssn;double gross;

public:Employee();Employee(string newName, string newSSN);void displayGross();

void calcPay();};

CIS162AB 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.

CIS162AB 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”);

CIS162AB 10

Derived Class - SalariedEmployee

class SalariedEmployee : public Employee

{

private:

double salary;

public:

SalariedEmployee();

void calcPay();

};

CIS162AB 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.

CIS162AB 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.

CIS162AB 13

Derived Class - HourlyEmployeeclass HourlyEmployee : public Employee{private:

double rate;int hours;

public:HourlyEmployee();void calcPay();

};

CIS162AB 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.

CIS162AB 15

Functions EmployeeEmployee::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;

}

CIS162AB 16

void Employee::displayGross();{

cout << “Gross = “ << gross;}void Employee::calcPay(){

cout << “Error: calcPay() called for an “ << “undifferentiated employee”

}

CIS162AB 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.

CIS162AB 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;}

CIS162AB 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}

CIS162AB 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()

CIS162AB 21

Functions SalariedEmployeeSalariedEmployee::SalariedEmployee() : Employee(){

cout << “Enter salary”;cin >> salary;

}

void SalariedEmployee::calcPay(){

gross = salary;}

CIS162AB 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.

CIS162AB 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.

CIS162AB 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.

CIS162AB 25

Functions HourlyEmployeeHourlyEmployee::HourlyEmployee() : Employee(){

cout << “Enter pay rate”;

cin >> rate;

cout << “Enter hours worked”;

cin >> hours;}

void HourlyEmployee:: calcPay(){

gross = rate * hours;}

CIS162AB 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.

CIS162AB 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.

CIS162AB 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.

CIS162AB 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()?

CIS162AB 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();

}

CIS162AB 31

Objects as Function Arguments

• An object of a derived class is also an object of the base class.

base: Employeederived: SalariedEmployee,

HourlyEmployee

• Object types of SalariedEmployee and HourlyEmployee can be passed through a parameter type of Employee.

CIS162AB 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).

CIS162AB 33

Summary

• Inheritance

• Base and Derived Classes

• Constructor Overloading

• Base Initialization List

• Constructors in Derived Class

• Function Redefinition vs Overloading

• Function Signatures