Inheritance Juan Marquez 12_inheritance.ppt

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Operator overloading redefine the operations of operators
Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Inheritance Reserved word protected Reserved word super
CIS162AD Inheritance 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Constructors & Inheritance.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Inheritance.
CS102--Object Oriented Programming Lecture 8: – More about Inheritance When to use inheritance Relationship between classes Rules to follow Copyright ©
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
VB Classes ISYS 512. Adding a Class to a Project Project/Add Class –*** MyClass is a VB keyword. Steps: –Adding properties Declare Public variables in.
CS102--Object Oriented Programming Lecture 10: – Abstract Classes Copyright © 2008 Xiaoyan Li.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Chapter 12: Adding Functionality to Your Classes.
Chapter 15 Inheritance. Slide Overview 15.1 Inheritance Basics 15.2 Inheritance Details 15.3 Polymorphism.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Object Oriented Programming in C++ Chapter 6 Inheritance.
CMSC 202 Inheritance I Class Reuse with Inheritance.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
CIS162AD Inheritance Part 3 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Abstract Classes and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Polymorphism 2nd Lecture
Chapter 15 Inheritance. Chapter 15 Inheritance.
Inheritance and Polymorphism
COMP 2710 Software Construction Inheritance
Inheritance 2nd Lecture
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Inheritance Basics Programming with Inheritance
Comp 249 Programming Methodology
Inheritance I Class Reuse with Inheritance
Learning Objectives Inheritance Virtual Function.
Inheritance 2nd Lecture
CMSC 202 Inheritance.
Class Reuse with Inheritance
Computer Programming with JAVA
Inheritance 2nd Lecture
Overview of C++ Polymorphism
C++ Programming CLASS This pointer Static Class Friend Class
CMSC 202 Inheritance II.
Chapter 7 Inheritance.
Presentation transcript:

Inheritance Juan Marquez 12_inheritance.ppt 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 CIS162AB

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

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

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

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

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

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

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

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

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

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

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

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

Functions Employee Employee::Employee() cout << “Enter name:” { 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Employee emp2 (“Joe Cool”, “123-45-6789”); SalariedEmployee empSalary; 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

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

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

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