inheritance 7 - Inheritance Making Classes More Life-like...

Info icon This preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Inheritance
Image of page 1

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

View Full Document Right 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
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
Image of page 3

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

View Full Document Right 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_
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; };
Image of page 5

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

View Full Document Right 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; }
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?
Image of page 7

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

View Full Document Right Arrow Icon
Inheritance 8 An Inheritance Hierarchy Employee Manager Worker Secretary Engineer
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
Image of page 9

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

View Full Document Right 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(); }
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 } };
Image of page 11

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

View Full Document Right 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?
Image of page 12
Inheritance 13 More on Overriding I also have the option of implementing an overridden member function in a derived class by making a call to a member function in the base class!
Image of page 13

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

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern