04c_inherit

04c_inherit - Data Structures - CSCI 102 CS102 C+...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Data Structures - CSCI 102 Copyright © William C. Cheng CS102 Inheritance Bill Cheng http://merlot.usc.edu/cs102-s11
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Polymorphism (Ch 13) Virtual functions Abstract classes Interfaces C Structs (Ch 10) Topics to cover 2 Data Structures - CSCI 102 Copyright © William C. Cheng C++ Object-Oriented Programming C++ Classes (Ch 11) Constructors Destructors Member functions Exception Handling (Ch 15) Namespaces (Ch 8) Operator Overloading (Ch 14) Class Composition & Inheritance (Ch 12)
Background image of page 2
A car is built from a frame, transmission, tires, steering wheel, etc. In the real world, complex objects are built from smaller, simpler objects 3 Data Structures - CSCI 102 Copyright © William C. Cheng Object Composition A computer is built from a CPU, a hard drive, a power supply, RAM, etc. Use classes as member variables in other classes Object composition is the process of creating more complex objects from simple ones Becomes a "Has-A" relationship e.g. A car "Has-A" steering wheel
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
It keeps your objects simple. ..an individual class should focus on doing ONE thing well Why should we use composition? 4 Data Structures - CSCI 102 Copyright © William C. Cheng Composition More complex objects don’t need to know the details of what other objects are doing Destroying the complex object means destroying all the objects it contains Composition usually implies ownership Making objects self-contained and single-purpose promotes reusability Destroying the complex object does not destroy all the objects it contains Composition without ownership (a.k.a. Aggregation)
Background image of page 4
Has a name and hit points A creature in a videogame 5 Data Structures - CSCI 102 Copyright © William C. Cheng Composition Example We’ve already created a class to handle physical locations (remember "Point"?) Has a physical location on a grid class Creature { string name; int hp; Point location; };
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
In the real world, complex objects share many attributes with similar objects 6 Data Structures - CSCI 102 Copyright © William C. Cheng Object Inheritance A manager and a programmer both have a name, a salary, a job title, etc. They are both types of employees. Object inheritance is the process of creating more complex objects by acquiring the attributes and behaviors of existing ones Create new classes from existing classes A "Base" class (a.k.a. Superclass) A "Derived" class (a.k.a. Subclass) Becomes an "Is-A" relationship e.g. A programmer "Is-An" employee
Background image of page 6
7 Data Structures - CSCI 102 Copyright © William C. Cheng Inheritance Derived classes inherit all data/functions from their parent Managers may also have a list of people they manage
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/17/2011 for the course CSI 102 taught by Professor Billcheng during the Spring '11 term at USC.

Page1 / 37

04c_inherit - Data Structures - CSCI 102 CS102 C+...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online