CS 11 C++ track: lecture 8 Today: Inheritance. Inheritance (1) In C++ we create classes and...
-
Upload
martin-crawford -
Category
Documents
-
view
215 -
download
0
Transcript of CS 11 C++ track: lecture 8 Today: Inheritance. Inheritance (1) In C++ we create classes and...
![Page 1: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/1.jpg)
CS 11 C++ track: lecture 8
Today:
Inheritance
![Page 2: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/2.jpg)
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
![Page 3: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/3.jpg)
Inheritance (2)class Fruit { private: Color *color; int calories; int seeds; public: Fruit(); ~Fruit(); int seedsRemaining() const; int numCalories() const;}
![Page 4: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/4.jpg)
Inheritance (3)#include "Fruit.hh"
class Banana : public Fruit { private: bool peelExists; public: Banana(); ~Banana(); bool stillHasPeel() const;}
![Page 5: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/5.jpg)
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
![Page 6: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/6.jpg)
Inheritance (5)void printCalories(Fruit *f) { cout << f->numCalories << endl;}
// later...Fruit *f = new Fruit();Banana *b = new Banana();printCalories(f); // OKprintCalories(b); // also OK
![Page 7: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/7.jpg)
Inheritance (6)void printPeelStatus(Banana *b) { cout << b->stillHasPeel() << endl;}
// later...Fruit *f = new Fruit();Banana *b = new Banana();printPeelStatus(b); // OKprintPeelStatus(f); // not OK!// a Fruit is not a Banana!
![Page 8: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/8.jpg)
Inheritance (7)// Add some definitions:Fruit::seedsRemaining() { return 10; // or whatever..}
// Want to override for Bananas:Banana::seedsRemaining() { return 0;}
![Page 9: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/9.jpg)
Inheritance (8)void printSeedsRemaining(Fruit *f) { cout << f->seedsRemaining() << endl;}
// Later:Fruit *f = new Fruit();Banana *b = new Banana();printSeedsRemaining(f); // prints?printSeedsRemaining(b); // prints?
![Page 10: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/10.jpg)
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?
![Page 11: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/11.jpg)
Virtual methods (1)class Fruit { private: Color *color; int calories; int seeds; public: Fruit(); ~Fruit(); virtual int seedsRemaining() const; int numCalories() const;}
![Page 12: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/12.jpg)
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
![Page 13: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/13.jpg)
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
![Page 14: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/14.jpg)
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
![Page 15: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/15.jpg)
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;}
![Page 16: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/16.jpg)
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
![Page 17: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/17.jpg)
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?"
![Page 18: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/18.jpg)
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;}
![Page 19: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/19.jpg)
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"
![Page 20: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/20.jpg)
The STL STL stands for Standard Template Library Lots of useful classes for various uses vector<int> "vector" (array) of
integers list<Fruit *> linked list of Fruit* map<string, Fruit*> "map"
(association) between string and Fruit* Need to use for lab 7 More on web page
![Page 21: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d945503460f94a7c037/html5/thumbnails/21.jpg)
Lab 7 Very easy exercise on inheritance Shouldn't take long Can use extra time to get caught up on
older labs