CS 11 C++ track: lecture 8 Today: Inheritance. Inheritance (1) In C++ we create classes and instantiate objects An object of class Fruit "is-a" Fruit.

Slides:



Advertisements
Similar presentations
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Advertisements

Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Inheritance (notes for 10/26 lecture). Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically)
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Intro to Generic Programming Templates and Vectors.
LECTURE 07 Programming using C# Inheritance
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Inheritance using Java
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
Lecture 10 Inheritance “Absolute C++” Chapter 14.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Templates ~ their instantiation and specialization.
CS 11 C++ track: lecture 7 Today: Templates!. Templates: motivation (1) Lots of code is generic over some type Container data types: List of integers,
CS2110: SW Development Methods Inheritance in OO and in Java Part 1: Introduction Readings: A few pages in Ch. 2 of MSD text introduce this Section 3.3.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
CSC 142 Computer Science II Zhen Jiang West Chester University
Lecture 21 Multiple Inheritance. What is Multiple Inheritance? We defined inheritance earlier in the semester as a relationship between classes. If class.
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
1/20/03A2-1 CS494 Interfaces and Collection in Java.
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.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Lecture 6 : Intro. to Generic Programming Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Chapter 10 Inheritance and Polymorphism
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
C# G 1 CSC 298 Object Oriented Programming Part 2.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Inheritance Notes Chapter 6 1. Inheritance  you know a lot about an object by knowing its class  for example what is a Komondor? 2
Overview of C++ Templates
Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
CSC 143N 1 CSC 143 Object-Oriented Programming [Chapter 8]
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Templates.
Polymorphism.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Week 6 Object-Oriented Programming (2): Polymorphism
CIS 199 Final Review.
Templates An introduction.
Final Exam Review Inheritance Template Functions and Classes
Lecture 6: Polymorphism
Presentation transcript:

CS 11 C++ track: lecture 8 Today: Inheritance

Inheritance (1) In C++ we create classes and instantiate objects An object of class Fruit "is-a" Fruit with methods to do things that Fruits do An object of class Banana is also a Fruit probably does a lot of the same things that Fruits do but maybe in a slightly different way as well as other things Want to make this relationship explicit and re-use code that is common to both classes

Inheritance (2) class Fruit { private: Color *color; int calories; int seeds; public: Fruit(); ~Fruit(); int seedsRemaining() const; int numCalories() const; }

Inheritance (3) #include "Fruit.hh" class Banana : public Fruit { private: bool peelExists; public: Banana(); ~Banana(); bool stillHasPeel() const; }

Inheritance (4) Now Banana is a "subclass" of Fruit Has its own methods, plus methods of Fruit Any function that accepts a Fruit argument can also take a Banana because a Banana "is-a" Fruit too! works because all Fruit methods also defined in Bananas

Inheritance (5) void printCalories(Fruit *f) { cout numCalories << endl; } // later... Fruit *f = new Fruit(); Banana *b = new Banana(); printCalories(f); // OK printCalories(b); // also OK

Inheritance (6) void printPeelStatus(Banana *b) { cout stillHasPeel() << endl; } // later... Fruit *f = new Fruit(); Banana *b = new Banana(); printPeelStatus(b); // OK printPeelStatus(f); // not OK! // a Fruit is not a Banana!

Inheritance (7) // Add some definitions: Fruit::seedsRemaining() { return 10; // or whatever.. } // Want to override for Bananas: Banana::seedsRemaining() { return 0; }

Inheritance (8) void printSeedsRemaining(Fruit *f) { cout seedsRemaining() << endl; } // Later: Fruit *f = new Fruit(); Banana *b = new Banana(); printSeedsRemaining(f); // prints? printSeedsRemaining(b); // prints?

Inheritance (9) Problem! When Banana is treated like a generic Fruit in printSeedsRemaining(), Fruit's seedsRemaining() method is called not what we want Want Banana's seedsRemaining() method to be called How do we achieve this?

Virtual methods (1) class Fruit { private: Color *color; int calories; int seeds; public: Fruit(); ~Fruit(); virtual int seedsRemaining() const; int numCalories() const; }

Virtual methods (2) That's all we need to change! We have made seedsRemaining() a virtual method That means that even when a Banana is being treated like a generic Fruit, the Banana version of seedsRemaining() will be called NOTE: When Banana treated like a generic Fruit, can't call Banana-specific methods

Virtual methods (3) Virtual methods are what we want in this case Why not make ALL methods virtual? because there is a cost associated with it Only make methods virtual if you expect that you will need to override them

Pure virtual methods (1) Still something strange Does the concept of a "generic Fruit" mean anything? no. But we can create generic Fruits and call methods on them What if we want to say "this is what a Fruit can do" but not specify behavior leave to subclasses to do that

Pure virtual methods (2) class Fruit { private: Color *color; int calories; int seeds; public: Fruit(); ~Fruit(); virtual int seedsRemaining() const = 0; virtual int numCalories() const = 0; }

Pure virtual methods (3) Now can't define Fruit instances because there is no definition for methods seedsRemaining() and numCalories() But can still use it as base class for Banana which does define those methods The = 0 syntax (along with the virtual keyword) indicates that a method is a pure virtual method Must be defined in subclasses to get an object you can instantiate

Protected fields (1) Fields that are private are really private! Only objects of that class can access them directly Objects of subclasses can't access those fields even though they "own" the fields in a sense Sometimes this is what you want and sometimes it isn't How do we say "I want this field to be directly accessible even in subclasses?"

Protected fields (2) class Fruit { protected: Color *color; int calories; int seeds; public: Fruit(); ~Fruit(); virtual int seedsRemaining() const = 0; virtual int numCalories() const = 0; }

Protected fields (3) Define fields to be protected Now, subclasses will also be able to access the field directly Which is better, private or protected? beats me much controversy in OO theory about this private is safer protected can be more efficient don't need extra accessors and may be more "conceptually correct"

The STL STL stands for Standard Template Library Lots of useful classes for various uses vector  "vector" (array) of integers list  linked list of Fruit* map  "map" (association) between string and Fruit* Need to use for lab 7 More on web page

Lab 7 Very easy exercise on inheritance Shouldn't take long Can use extra time to get caught up on older labs