CS 211: Introduction to Computer Programming II - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

CS 211: Introduction to Computer Programming II

Description:

... PassCar from Car. Terminology: PassCar 'inherits' from Car. Inheritance ... Initialize the Car part of this object. Initialize the sunRoof part of this object. ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 14
Provided by: briand8
Category:

less

Transcript and Presenter's Notes

Title: CS 211: Introduction to Computer Programming II


1
CS 211Introduction to Computer Programming II
  • Instructor Brian M. Dennis
  • Teaching Assistants
  • Tom Lechner, Bin Lin, Rachel Goldsborough
  • http//www.cs.northwestern.edu/bmd/cs211/

2
Todays Topics
  • Introduction to inheritance
  • Reading
  • 9.1 9.5

3
Inheritance
  • Basic idea
  • Want a new data abstraction
  • Mostly the same as another abstraction, already
    created
  • Can we reuse the old one?
  • Stupid way
  • Cut and paste
  • Problems
  • Makes work for the programmer
  • Fixes to one don't propagate to the other
  • Increased code size

4
Inheritance
  • Solution in C
  • And most other OOP languages
  • Inheritance
  • Tied to class mechanism
  • "Derive" a new class from a "base" class
  • Types of inheritance
  • Single, can only inherit from one class
  • Multiple, can inherit from more than one class
  • Practice seems to indicate a pref for single
    inheritance

5
Inheritance
class Car public Car(int id 0, char
prod "") Car() int getVin()
void setVin(int nvin) char getProd()
void setProd(char nprod) void
display() private int vin char
producer
Nothing exciting, just your basic class
6
Inheritance
Reminder, passing info to submember constructor
include ltiostreamgt include ltcstringgt include
"Car.h" using namespace std CarCar(int id,
char prod) vin(id) int len
strlen(prod) producer new charlen 1
strcpy(producer, prod) int CargetVin()
return vin void CarsetVin(int nvin) vin
nvin
Just making a local copy of the passed in string.
7
Inheritance
Deallocate the old string.
char CargetProd() return producer void
CarsetProd(char prod) delete producer
int len strlen(prod) producer new charlen
1 strcpy(producer, prod) CarCar()
cout ltlt "Wrecking a " this-gtdisplay() cout
ltlt endl delete producer
Use the this pointer to call one of my functions
8
Inheritance
  • // Just for completeness' sake
  • void Cardisplay()
  • cout ltlt producer ltlt ""
  • ltlt vin ltlt "" ltlt endl

9
Inheritance
include "Car.h" class PassCar public Car
public PassCar(char tp, bool sd,
int id 0, char prod "") PassCar()
char getType() void setType(char s) bool
getSunRoof() void setSunRoof(bool b) void
display() private char passCarType
bool sunRoof
We are deriving PassCar from Car
Terminology PassCar "inherits" from Car
10
Inheritance
  • Original class A
  • Base class
  • New class B
  • Derived class
  • Additional syntax on class B's declaration
  • Within class declaration
  • Can add member variables
  • Can override member variables
  • Can add member functions
  • Can override member functions

11
Inheritance
include ltiostreamgt include ltcstringgt include
"PassCar.h" using namespace std PassCarPassCar
(char tp, bool sd, int id, char prod)
Car(id, prod), sunRoof(sd) int len
strlen(tp) passCarType new charlen 1
strcpy(passCarType, tp)
Initialize the Car part of this object.
Initialize the sunRoof part of this object.
12
Inheritance
  • In C we reuse two things
  • Instance variables
  • Instance behavior
  • Also have a whole bunch of goop to protect base
    classes from derived classes
  • Inheritance is transitive

13
Inheritance
  • Problem constructing a derived class
  • Base-class parts are constructed before derived
    class parts

14
Inheritance
char PassCargetType() return passCarType
void PassCarsetType(char s) int len
strlen(s) passCarType new charlen 1
strcpy(passCarType, s) bool
PassCargetSunRoof() return sunRoof void
PassCarsetSunRoof(bool b) sunRoof b
Nothing exciting here. Just straight ahead
definitions.
15
Inheritance
void PassCardisplay() cout ltlt "PassCar(" ltlt
getProd() ltlt "" ltlt passCarType ltlt
"" ltlt getVin() ltlt "" ltlt sunRoof PassCarP
assCar() cout ltlt "Junking a "
this-gtdisplay() delete passCarType
Note that I can call public methods of the Car
class just like my methods.
This definition overrides Car's definition of
display.
Calls PassCardisplay not Cardisplay
16
Inheritance
include ltiostreamgt include "PassCar.h" using
namespace std int main(int argc, char argv)
int vins 0 for (int i 1 i lt argc
i) Car hooptie(vins, argvi)
hooptie.display() cout ltlt endl
17
An Inheritance Example
... int main(int argc, char argv) int vins
0 srand(time(0)) for (int i 1 i lt
argc i 2) bool roofp (rand() 2) ?
true false PassCar hooptie(argvi1,
roofp, vins, argvi) hooptie.display()
cout ltlt endl Car acar hooptie
acar.display() cout ltlt endl
Assigning a derived object to a base object is
not a good idea.
Which display do we get?
18
Inheritance
  • Problem destructing a derived class
  • Derived parts are destructed before base parts

19
Builtin string class
  • char strings are lame
  • Can't hold null characters
  • Can be conflated with a pointer to a single char
  • Can't directly compare
  • Small library of operations
  • No bounds checking
  • string class fixes many of the problems

20
Thats a Wrap
  • Takeaway
  • Inheritance promotes reuse
  • Interface
  • Data structuring
  • Implementation code
  • Reading
  • 9.1 9.5
Write a Comment
User Comments (0)
About PowerShow.com