Tutorial Seven - PowerPoint PPT Presentation

1 / 70
About This Presentation
Title:

Tutorial Seven

Description:

void setPerson(const char i, const double sal, const int a); void showPerson(void) ... Employee::hourlySalary=sal; void Employee::outputData(void) ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 71
Provided by: jun6
Category:
Tags: sal | seven | tutorial

less

Transcript and Presenter's Notes

Title: Tutorial Seven


1
Tutorial Seven
  • Inheritance

2
Lesson A
Understanding Inheritance
3
Lesson A Objectives
  • To learn
  • What inheritance is
  • How to create a derived class
  • How to choose the class access specifier
  • How to override parent class functions

4
What Is Inheritance?
  • Inheritance
  • Principle that knowledge of a general category
    can be applied to more specific objects
  • Objects used in computer programs are easier to
    understand if you can place them within a
    hierarchy of inheritance

5
What Is Inheritance?
  • When a new class is inherited from a previous
    class, it is said to be derived from the previous
    class
  • The previous class is called a parent class, base
    class, or superclass
  • The new class is called a child class, derived
    class, or subclass

6
What Is Inheritance?
  • In many cases, a derived class may not possess
    all of its parents traits
  • Members of the new class may require a different
    display format than members of the previous class
  • A feature of inheritance is that the descendant
    class can override those attributes from the
    parent that are inappropriate

7
What Is Inheritance?
  • To be truly object-oriented, a programming
    language must allow inheritance
  • One major feature of object-oriented programming
    is the ability to create new classes from one
    that already exists

8
What Is Inheritance?
  • Programs in which you create classes that are
    derived from existing classes offer several
    advantages
  • Save time because much of the code needed for
    your class is already written
  • Save time because the existing code has already
    been testedthat is, it is reliable
  • Save time because you already understand how the
    base class works

9
What Is Inheritance?
  • Programs in which you create classes that are
    derived from existing classes offer several
    advantages (continued)
  • In a derived class, you can extend and revise a
    parent class without corrupting its existing
    class functions
  • The more situations in which the code has been
    used, the more likely that logical errors have
    already been found and fixed

10
What Is Inheritance?
  • In some object-oriented programming languages,
    every new class must inherit from an existing
    class
  • C is sometimes called a hybrid object-oriented
    programming language because you may create
    original base classes without deriving them from
    some other class

11
Creating a Derived Class
  • With C, you cannot inherit regular functions
    and variables, only classes
  • The specifier protected
  • Allows members to be used by class member
    functions and by derived classes

12
class NameAndNumber private int idNum
char lastName20 char firstName15
public NameAndNumber() NameAndNumber()
void inputData(const int num, const char
last, const char first) void
outputData(void)
13
class NameAndNumber protected int
idNum char lastName20 char
firstName15 public NameAndNumber()
NameAndNumber() void inputData(const int
num, const char last, const char
first) void outputData(void)
14
What is Inheritance?
  • To create a derived class, you include the
    following elements in the order listed
  • The keyword class
  • The derived class name
  • A colon
  • A class specifier, either public, private, or
    protected

15
What is Inheritance?
  • To create a derived class, you include the
    following elements in the order listed
    (continued)
  • An opening bracket
  • The class definition statements
  • A closing bracket
  • A semicolon

16
include "NameAndNumberNew.cpp" class Customer
public NameAndNumber private double
balanceDue public void inputBalanceDue(con
st double bal) void outputBalance(void)
17
What is Inheritance?
  • Access specifier matters in the following cases
  • example

class Customer public NameAndNumber
18
What is Inheritance?
  • Access specifier matters in the following cases
  • If "public" access specifier is used
  • based class that public remain public in the
    derived class
  • based class that protected remain protected in
    the derived class
  • based class that private are inaccessible in the
    derived class

19
What is Inheritance?
  • Access specifier matters in the following cases
  • If "protected" access specifier is used
  • based class that public remain protected in the
    derived class
  • based class that protected remain protected in
    the derived class
  • based class that private are inaccessible in the
    derived class

20
What is Inheritance?
  • Access specifier matters in the following cases
  • If "private" access specifier is used
  • based class that public remain private in the
    derived class
  • based class that protected remain private in the
    derived class
  • based class that private are inaccessible in the
    derived class

21
includeltiostream.hgt class Person private
double salary protected int age
public char initial void setPerson(const
char i, const double sal, const int a)
void showPerson(void)
22
void PersonshowPerson() coutltlt"The person
initial is "ltltinitialltltendl coutltlt"The person
age is "ltltageltltendl coutltlt"The person salary is
"ltltsalaryltltendl void PersonsetPerson(const
char i, const double sal, const int
a) initiali salarysal agea
23
include "Person.cpp" void main() Person
Carmen coutltltCarmen.initial
24
include "Person.cpp" class Introvert private
Person private int idNum public
void showIntrovert(void)
Introvert class has four data members salary,
age, initial, and idNum
25
showIntrovert() can use idNum. sowIntrovert can
also access initial and age, but can not use
salary, since it is private in Person class. The
way to access salary is througth
showPerson() function and built in side the
showIntrovert(). Other program such as main()
can not access the four data iterm directly.
26
include "Person.cpp" class ShomwhatShy
protected Person private int idNum
public void showShy(void)
27
It also has four data members. showShy() can
access idNum, and may also reference as
reference age and intial. When a somewhatShy()
object is instantiated in the main(), it may
use the showShy() fucntion to access idNum,
initial, or age. showShy() can not access salary
directly. It has to cal showPerson() to access
salary.
28
include "Person.cpp" class Extrovert public
Person private int idNum public
void showIntrovert(void)
29
includeltiostream.hgt include"Extrovert.cpp" void
main() Extrovert ex1 ex1.setPerson('J',
30000,40) / can not access id.Num, since it is
private is Extrovert class can not access
age, since it is protected can not access
salary, since it is priavte defined in base
class coutltltex1.idNumltltendl coutltltex1.ageltlten
dl coutltltex1.salaryltltendl /
30
ex1.initial'K' //it can access initial,
since it is public in Extrovert
class coutltltex1.initialltltendl ex1.showPerson()

31
When you use main() to create an object of
Extrovert class, showExtrovert() can display any
of the four data members. Main() function can
not access salary directly it may call
showPerson() to access salary.
32
What is Inheritance?
  • The following are never inherited
  • Constructor functions
  • Destructor functions
  • friend function
  • static data members

33
What is Inheritance?
  • The following are never inherited (continued)
  • static member functions
  • Overloaded new operators
  • Overloaded operators
  • If a derived class requires any of these items,
    they must be explicitly defined within the
    derived class definitions

34
Choosing the Class Access Specifier
  • When defining a derived class, insert one of
    three class access specifiers (public, private,
    or protected) just prior to the base class name
  • No matter which access specifier you use when
    creating a child class, access to parent members
    never becomes more lenient than originally coded

35
Choosing the Class Access Specifier
  • If a derived class uses the public access
    specifier, the following statements are true
  • Base class members that are public remain public
    in the derived class
  • Base class members that are protected remain
    protected in the derived class
  • Base class members that are private are
    inaccessible in the derived class

36
Choosing the Class Access Specifier
  • If a derived class uses the protected access
    specifier, the following statements are true
  • Base class members that are public become
    protected in the derived class
  • Base class members that are protected remain
    protected in the derived class
  • Base class members that are private are
    inaccessible in the derived class

37
Choosing the Class Access Specifier
  • If a derived class uses the private access
    specifier, the following statements are true
  • Base class members that are public become private
    in the derived class
  • Base class members that are protected become
    private in the derived class
  • Base class members that are private are
    inaccessible in the derived class

38
Choosing the Class Access Specifier
  • If a class has private data members, they can be
    used only by member functions of that class
  • If a class has protected data members, they can
    be used by member functions of that class and by
    member functions of described classes
  • If a class had public data members, they can be
    used by member functions of that class, by member
    function of derived classes, and by the main()
    program

39
class GrandParant public int
x class Parent class Child public
Parent
40
Overriding Parent Class Functions
  • When a new class is derived from an existing
    class, the derived class has access to nonprivate
    member functions in the base class
  • The new class may also have its own member
    functions
  • Those functions may have names that are identical
    to the function name in the base class

41
Overriding Parent Class Functions
  • When any class member function is called, the
    following steps take place
  • Compiler looks for a matching function name in
    the class of the object using the function name
  • If no match is found, compiler looks for a
    matching function name in the parent class
  • If no match is found, compiler continues up the
    inheritance hierarchy until the base class is
    reached
  • If no match is found in any class, an error
    message is issued

42
includeltiostream.hgt includeltstring.hgt class
NameAndNumber protected int idNum
char lastName20 char firstName15
public NameAndNumber() NameAndNumber()
void inputData(const int num, const char
last, const char first)
43
void outputData(void) NameAndNumberNameA
ndNumber() NameAndNumberidNum123 strcpy(Na
meAndNumberlastName," ") strcpy(NameAndNumber
firstName," ") NameAndNumberNameAndNumber(
)
44
void NameAndNumberinputData(const int
num, const char last, const char
first) NameAndNumberidNumnum strcpy(NameA
ndNumberlastName,last) strcpy(NameAndNumberf
irstName,first) void NameAndNumberoutputData(
void) coutltlt"ID "ltltNameAndNumberidNum cout
ltlt" "ltltNameAndNumberfirstNameltlt"
" ltltNameAndNumberlastNameltltendl
45
include "NameAndNumberB.cpp" class
Employeepublic NameAndNumber private
int dept double hourlySalary public
void inputData(const int id, const char last,
const char first, const int dept, const double
sal) void outputData(void)
46
void EmployeeinputData(const int id, const char
last, const char first, const int dep,
const double sal) NameAndNumberinputData(id,
last, first) Employeedeptdep Employeehour
lySalarysal void EmployeeoutputData(void)
coutltlt"Employee ID "ltltEmployeeidNumltltendl co
utltlt" Employee is "ltltEmployeelastNameltlt" ,
" coutltltEmployeefirstName0ltlt"."ltltendl cout
ltlt" Salary "ltltEmployeehourlySalary
ltlt" Department "ltltEmployeedeptltltendl
47
include "Employee.cpp" includeltiostream.hgt void
main() NameAndNumber person person.inputData
(123,"Jun","Ni") person.outputData() coutltltendllt
ltendl Employee worker worker.inputData(987,
"Dean", "Roger", 101, 35433.45) worker.outputData
()
48
Lab Exercises and Homework
  • Do Exercises 1 on page 282
  • Do Exercises 2 on page 282
  • Do Exercises 3 on page 282
  • Due date

49
Lesson B
Inheritance Techniques
50
Lesson B Objectives
  • To learn
  • How to use the constructor initialization lists
  • How to inherit from base classes with
    constructors
  • How to override inherited access
  • How to use multiple inheritance

51
Constructor Initialization Lists
  • Many constructor functions consist of a series of
    assignment statements
  • Constructor initialization list
  • Inserted after the argument list for the
    constructor function, preceded by a single colon

52
Constructor Initialization Lists
  • Understanding use of constructor initialization
    lists
  • Many C programmers prefer this method, so it is
    used in many programs
  • Technically, constructors should initialize
    values
  • Reference variable and constant class members
    cannot be assigned values
  • When creating a derived class and instantiating
    an object, a parent class object must be
    constructed first

53
class Inventory protected int itemNum
double itemPrice public Inventory(int n,
double p) itemNumn, itemPricep
54
class Inventory protected int itemNum
double itemPrice public Inventory(int n,
double p)itemNum(n), itemPrice(p)
55
Base Class Construction
  • When instantiating a derived class object
  • A constructor for its base class is called first,
    followed by the derived class constructor
  • If a base class constructor requires arguments,
    you must create a constructor for any derived
    class

56
Base Class Construction
  • If you fail to call a needed base class
    constructor in the initialization list for a
    derived class, you will receive an error message
  • Example Cannot find default constructor to
    initialize base class

57
Base Class Construction
  • Class object
  • May be initialized in a constructor
    initialization list
  • When constructing a derived class object, the
    base class constructor is called first
  • When a derived class object is destroyed, the
    child class destructor is called first and the
    base class destructor is called last

58
Base Class Construction
  • If a default base class constructor exists, no
    compiler error will arise if you omit the call to
    the base class constructor when deriving a class

59
Overriding Inherited Access
  • Nine inheritance access specifier combinations
    are possible
  • In addition, you may override the class access
    specifier for any specific class members
  • Note that you do not place parentheses after the
    function name in the derived class

60
class PetInven protected int stockNum
double price public PetInven(const int
stk, const double pr) stockNum(stk), price(pr)

61
class Animalpublic PetInven protected
int petAge public Animal(const int stk,
const double price, const int
age) AnimalAnimal(const int stk, const
double price, const int age)
PetInven(stk,price) AnimalpetAgeage
62
class PetInven //alternatively protected
int stockNum double price public
PetInven(const int stk, const double
pr) stockNum(stk), price(pr) class
Animalpublic PetInven protected int
petAge public Animal(const int stk, const
double price, const int age)
PetInven(stk,price), petAge(age)
63
Multiple Inheritance
  • A derived class may derive from more than one
    base class
  • You already use multiple inheritance each time
    you include iostream.h in a program
  • cin and cout objects are each derived from other
    classes

64
Multiple Inheritance
  • Some programmers are vehemently opposed to
    multiple inheritance
  • As proof that multiple inheritance is never
    required, consider that the object-oriented
    languages SmallTalk and Java allow only single
    inheritance

65
Virtual Base Classes
  • A base class may have many descendants through
    single inheritance
  • A class may inherit from two other classes
    through multiple inheritance
  • The keyword virtual indicates that the base class
    should be used only once

66
includeltiostream.hgt class Inventory
protected int itemNumber double itemPrice
public Inventory(void) Inventory(void)
void showData(void)
67
class Employee protected int empNumber
double empSalary public Employee(void)
Employee(void) void showData(void)
68
class Patentpublic Inventory, public
Employee private int patentNum public void
showAll(void) void PatentshowAll(void) c
outltltPatentpatentNumltltendl InventoryshowData
() EmployeeshowData()
69
Patent newInvention newInvention.showAll()
//OK to use showAll() in Patent
class newInvention.showData() //It
confuses to select which one is accessed // So
you can use the following way newInvention.Invento
ryshowData() newInvention.EmplyeeshowData()
70
Lab Exercises and Homework
  • Do Exercises 1 on page 300
  • Do Exercises 2 on page 300
  • Do Exercises 3 on page 300
  • Due date
Write a Comment
User Comments (0)
About PowerShow.com