Chapter 9 Inheritance.

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Understanding Inheritance Object-Oriented Programming Using C++ Second Edition 9.
Chapter 13: Object-Oriented Programming
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Inheritance Inheritance – most important and a useful feature of OOPs supported by C++ | Website for Students | VTU -NOTES -Question Papers.
An Object-Oriented Approach to Programming Logic and Design
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
1 CSC241: Object Oriented Programming Lecture No 13.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
1 CSC241: Object Oriented Programming Lecture No 12.
Lecture 21 Multiple Inheritance. What is Multiple Inheritance? We defined inheritance earlier in the semester as a relationship between classes. If class.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1 CSC241: Object Oriented Programming Lecture No 02.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Classes, Interfaces and Packages
Object-Oriented Programming: Inheritance and Polymorphism.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
The Object-Oriented Thought Process Chapter 03
Introduction to Object-oriented Programming
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Visit for more Learning Resources
User-Written Functions
Static data members Constructors and Destructors
Inheritance Based on slides by Ethan Apter
Object-Oriented Programming: Classes and Objects
Review: Two Programming Paradigms
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Chapter 5 Classes.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Object-Oriented Programming: Classes and Objects
User-Defined Functions
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Understanding Inheritance
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Object Oriented Programming (OOP) LAB # 8
PHP Classes and Object Orientation
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
Learning Objectives Inheritance Virtual Function.
Chapter 6 Methods: A Deeper Look
Lecture 22 Inheritance Richard Gesick.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Object-Oriented Programming Using C++ Second Edition
Object-Oriented Programming Using C++ Second Edition
Dr. Bhargavi Dept of CS CHRIST
Java – Inheritance.
Operator Overloading, Friends, and References
Programming with ANSI C ++
Object-Oriented Programming: Inheritance and Polymorphism
Tonga Institute of Higher Education IT 141: Information Systems
Institute of Petroloeum Technology, Gandhinagar
Tonga Institute of Higher Education IT 141: Information Systems
Review of Previous Lesson
Chapter 9 Introduction To Classes
Constructors and Deconstructor
Functions, Procedures, and Abstraction
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
C++ Object Oriented 1.
Presentation transcript:

Chapter 9 Inheritance

Inheritance In the C++ language, our basis for emulation is the object. We use objects to copy real life objects into our programs. Just like real life objects like humans, objects can have other objects that are similar and related in many ways.

Inheritance Each person has inherited traits from their parents, grand parents and so on. If we could take this idea into C++ Object Oriented Programming, then we could make an object, a person, and another object, the child person object, that will inherit traits of its parent, like hair color, but also have traits not found in the parent, like personality traits.

Inheritance Inheritance is the most powerful feature of object oriented programming, after classes themselves. Inheritance in C++ is the process of creating new classes, called derived classes, from existing or base classes.

Inheritance The derived class inherits all the capabilities of the base class but can add embellishments and refinements of its own. The base class is unchanged by this process. So you can think of a base class of “Computer” that has it’s own characteristics. And then a derived class of “Windows” or “Macintosh” that will add their own special capabilities that are more specific than just a computer.

Inheritance Inheritance is an essential part of OOP. It’s big payoff is that it permits code reusability. Once a base class is written and debugged, it need not be touched again, but, using inheritance, can nevertheless be adapted to work in different situations.

Inheritance Reusing existing code saves time and money and increases a programs reliability. Inheritance can also help in the original conceptualization of a programming problem, and in the overall design of a program.

Derived Class and Base Class Remember the count3.cpp example from Chapter 8, Operator Overloading. This program used a class Counter as a general-purpose counter variable. A count could be initialize to 0 or to a specified number with constructors, incremented with the ++ operator, and read with the get_count() operator.

Derived Class and Base Class Let’s suppose that we have worked long and hard to make the Counter class operate just the way we want, and we’re happy with the result, except for one thing. We really need a way to decrement the count. Maybe we are wanting to count people entering and exiting a bank.

Derived Class and Base Class We could insert a decrement routine directly into the source code of the Counter class. However there are several reason why we would not want, or be able to do this. First the counter class works very well and has undergone many hours of testing and debugging.

Derived Class and Base Class If we start messing around with the source code for Counter, the testing process will need to be carried out again, and of course we may foul up something and spend hours debugging code that worked fine before we modified it. Re-testing costs lots of money and time.

Derived Class and Base Class In some situations there might be another reason for not modifying the Counter class; we might not have access to the source code, especially if it was distributed as part of a class library. A lot of modules are given out as already compiled .exe, not in .cpp or source code.

Derived Class and Base Class To avoid these problems we can use inheritance to create a new class based on Counter, without modifying Counter itself. Here is the code for “counten.cpp”, which includes a new class, CoundDn, that adds a decrement operator to the Counter class.

Specifying the Derived Class Following the Counter class in the listing is the specification for a new class, CountDn. This class incorporates a new function, operator - - (), which decrements the count. However the new CountDn class inherits all the features of the counter class.

Specifying the Derived Class CountDn doesn’t need a constructor or the get_count() or operator++() functions, because they already exist in Counter. The first line of CounDn specifies that it is derived from Counter: class CountDn : public Counter

Specifying the Derived Class Here we use a single colon (not the double colon used for the scope resolution operator), followed by the keyword public and the name of the base class Counter. This sets up the relationship between the classes. This line says that CountDn is derived from the base class Counter.

Accessing Base Class Members An important topic in inheritance is knowing when a member function in the base class can be used by objects of the derived class. This is called accessibility. Let’s see how the compiler handles the accessibility issue in the counten.cpp example.

Substituting Base Class Constructors In the main() part of counten.cpp, we create an object of class CountDn: countDn c1; This causes c1 to be created as an object of class CountDn and initialized to 0. But how does this work, there is no constructor, the derived class will use an appropriate constructor from the base class.

Substituting Base Class Constructors In counten there’s no constructor in CountDn, so the compiler uses the no-argument constructor from Count. This flexibility on the part of the compiler; using one function because another isn’t available; appears regularly in inheritance situations. Generally, the substitution is what you want, but sometimes it can be annoying.

Substituting Base Class Member Functions The object c1 of the CountDn class also uses the operator++() and get_count() functions from the Counter class. The first is used to increment c1; ++c1;

Substituting Base Class Member Functions The second is used to display the count in c1; Cout << “\nc1=“ << c1.get_count(); Again the compiler, not finding these functions in the class of which c1 is a member, uses member functions from the base class.

Output of Counten In main() we increment c1 three times, print out the resulting value, decrement c1 twice, and finally print out the value again. Here’s the output: C1=0 After initialization C1=3 after ++c1, ++c1, ++c1 C1=1 after –c1, --c1

The protected Access Specifier We have increased the functionality of a class without modifying it. Well, almost without modifying it. Let’s look at the single change made to the counter class. The data in the classes we’ve looked at so far, including count in the Counter class in the earlier countpp3 program, have used the private access specifier.

The protected Access Specifier In the Counter class in Counten, count is given a new specifier: protected. What does this do? Let’s first review what we know about the access specifiers private and public. A member function of a class can always access class members, whether they are public or private.

The protected Access Specifier But an object declared externally can only invoke (using the dot operator for example) public members of the class. It’s not allowed to use private data members. This is all we need to know if we don’t use inheritance.

The protected Access Specifier With inheritance however, there is a whole new range of possibilities. A question that comes up is, can a member function of the derived class access members of the base class? In other words, can operator - - () in CountDn access count in Counter?

The protected Access Specifier The answer is that member functions can access members of the base class if the members are public, or if they are protected. They can’t access private members. We don’t want to make count public, since that would allow it to be accessed by any function anywhere in the program and eliminate the advantages of data hiding.

The protected Access Specifier A protected member, on the other hand, can be accessed by member functions in its own class or, in any class derived from its own class. It can’t be accessed from functions outside these classes, such as main(). This is just want we want.

The protected Access Specifier The moral of the story is that if you are writing a class that you suspect might be used, at any point in the future, as a base class for other classes, then any member data that the derived classes might need to access should be made protected rather than private. This ensures that the class is “Inheritance Ready”.

Dangers of protected There are dangers to making class members protected. Say that you’ve written a class library, which you’re distributing to the public. Any programmer who buys this library can access protected members of your classes simply by deriving other classes from them.

Dangers of protected This makes protected members considerably less secure than private members. To avoid corrupted data, it’s often safer to force derived classes to access data in the base class using only public functions in the base class, just as ordinary main() programs do.

Dangers of protected Using the protected specifier leads to simpler programming, so we rely on it now. You’ll need to weigh the advantages of protected against its disadvantages in your own programs.

Base Class Unchanged Remember that, even if other classes have been derived from it, the base class remains unchanged. In the main() part of counten, we could define objects of type Counter: Counter C2; object of base class Such objects would behave just as they would if CountDn didn’t exist.

Base Class Unchanged Note also that inheritance doesn’t work in reverse. The base class and its objects don’t know anything about any classes derived from the base class. In this example that means that objects of class Counter, such as c2, can’t use the operator - -() function in CountDn. If you want a counter that you can decrement, it must be of class CountDn, not Counter.

Derived Class Constructors There’s a potential glitch in the counten program. What happens if we want to initialize a CountDn object to a value? Can the one-argument constructor in Counter be used? The answer to this is no.

Derived Class Constructors As we saw in Counten, the compiler will substitute a no-argument constructor from the base class, but it draws the line at more complex constructors. To make such a definition work, we must write a new set of constructors for the derived class. This is shown in “counten2.cpp”.

Derived Class Constructors This program uses two new constructors in the CountDn class. Here is the no-argument constructor: CountDn() : Counter() { } This constructor has an unfamiliar feature: the function name following the colon. This construction causes the CountDn() constructor to call the Counter() constructor in the base class.

Derived Class Constructors In main(), when we say: CountDn c1; The compiler will create an object of type CountDn and then call the CountDn constructor, which carries out the work. The CountDn() constructor could add additional statements of its own, but in this case it doesn’t need to, so the function body between the braces is empty.

Derived Class Constructors Calling a constructor from the initialization list may seem odd, but it makes sense. You want to initialize any variables, whether they’re in the derived class of the base class, before any statements in either the derived or base class constructors are executed.

Derived Class Constructors By calling the base class constructor before the derived class constructor starts to execute, we accomplish this by the statement; CountDn c2(100); Main() uses the one argument constructor in CountDn.

Derived Class Constructors This constructor also calls the corresponding one argument constructor in the base class: Countdn(int c) : Counter (c ) { } The argument c is passed to Counter. You can also use a constructor with this statement: CountDn c3 = -- c2;