inheritance 7

Inheritance 7 - Inheritance Making Classes More Life-like Classes represent concepts Concepts can be extended Concepts can be abridged Concepts can

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

View Full Document Right Arrow Icon
Inheritance
Background image of page 1

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

View Full DocumentRight Arrow Icon
Inheritance 2 Making Classes More Life-like Classes represent concepts Concepts can be extended Concepts can be abridged Concepts can be modified Concepts are extended, abridged, and/or modified to produce new concepts, without altering existing concepts
Background image of page 2
Inheritance 3 Inheritance as a Solution The problem is: How do we make classes mimic concepts? One solution is Inheritance ! Inheritance models the “Is-A” relationship between objects. “Is-A” allows the new class to be specified in terms of the old class. Typical signs that 2 classes have an inheritance relationship: They are common in general ways (data, behavior) They are different in specific ways (data, behavior) One is a specialization of the other
Background image of page 3

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

View Full DocumentRight Arrow Icon
Inheritance 4 Inheritance Syntax #ifndef _EMPLOYEE_H_ #define _EMPLOYEE_H_ class Employee { public: enum Gender {MALE, FEMALE}; string _name; Gender _gender; }; class Worker : public Employee { public: float _wage; }; class Manager : public Employee { public: unsigned int _salary; unsigned int _level; }; #endif // _EMPLOYEE_H_
Background image of page 4
Inheritance 5 More Derived Classes class Secretary : public Worker { public: unsigned int _wpm; }; class Engineer : public Worker { public: string _school; string _degree; };
Background image of page 5

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

View Full DocumentRight Arrow Icon
Inheritance 6 Inheritance Usage #include “Employee.h” int main() { Secretary s; Engineer e; s._name = “Damon”; s._gender = Employee::MALE; s._wage = 11.50; s._wpm = 75; e._name = “Marie”; e._gender = Employee::FEMALE; e._wage = 25.25; e._school = “WWU”; e._degree = “CS”; e._wpm = 45; }
Background image of page 6
Inheritance 7 Observations First of all, note that a derived class inherits the data from all its ancestors. Second, notice that there is no limit to the number of levels allowed in an inheritance hierarchy. Other than that, do you see any errors? Notice anything odd?
Background image of page 7

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

View Full DocumentRight Arrow Icon
Inheritance 8 An Inheritance Hierarchy Employee Manager Worker Secretary Engineer
Background image of page 8
Inheritance 9 The Jargon The class being extended can be called: base class parent class ancestor class superclass The class created by the extension can be called: derived class child class subclass You can create several levels of inheritance…as the previous diagram showed
Background image of page 9

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

View Full DocumentRight Arrow Icon
Inheritance 10 Member Functions Member functions are inherited just like member data. . class Employee { public: void who(); }; void Employee::who() { cout << “Name:” << _name << endl; } int main() { Secretary s; Engineer e; s.who(); e.who(); }
Background image of page 10
Inheritance 11 Overriding Member Functions Base class member functions can be overridden in derived classes. class Car { public: int _speed; void accelerate() { _speed += 2; } }; class Yugo : public Car { public: void accelerate() { _speed += 1; } }; class Spider : public Car { public: void accelerate() { _speed += 5 } };
Background image of page 11

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

View Full DocumentRight Arrow Icon
Inheritance 12 Overriding Usage int main() { Yugo y; Spider s; y.accelerate(); s.accelerate(); } What if I wanted accelerate to take an optional parameter…how do I do this?
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/17/2010 for the course CS 162 taught by Professor Fant during the Spring '08 term at Portland State.

Page1 / 39

Inheritance 7 - Inheritance Making Classes More Life-like Classes represent concepts Concepts can be extended Concepts can be abridged Concepts can

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

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