lecture33 Inheritance(2)

lecture33 Inheritance(2) - CMPT 128: Introduction to...

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

View Full Document Right Arrow Icon
1 © Janice Regan, CMPT 128, February. 2007 CMPT 128: Introduction to Computing Science for Engineering Students Inheritance (2)
Background image of page 1

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

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128, February. 2007 2 Inheritance Base class (more general class) is composed of all attributes and methods that are common to all its derived (specialized) classes Derived class (specialized class) may add additional attributes and methods specific to the objects in the derived class.
Background image of page 2
© Janice Regan, CMPT 128, February. 2007 3 Protected vs. Private Any base class will look much like the classes we have discussed A common difference may be the use of the protected keyword (used instead of or in addition to private and public) Private members of a base class are not accessible outside the base class, not even to derived classes of the base class (classes inherited from the base class) Because a derived class will usually want to have direct access to all its members (including those inherited) we need a new type of access Protected access means that only the base class and any classes derived (inherited) from it have access to the members.
Background image of page 3

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

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128, February. 2007 4 Using Base Class Private Members Derived class "inherits" private attributes and member functions Derived class cannot directly access private members or private member functions of the base class Private attributes and member functions can ONLY be accessed "by name" in member functions of the class they’re defined in
Background image of page 4
© Janice Regan, CMPT 128, February. 2007 5 Private Member Functions: Impact Larger impact here vs. attributes attributes can be accessed indirectly using the accessor or mutator member functions of the base class Private member functions of the base class are not available at all This is correct behaviour Private member functions should be simply "helper" functions Should be used only in class they’re defined
Background image of page 5

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

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128, February. 2007 6 Inheritance Basics C++ Base class "General" class from which others derive Derived class New, specialized class Automatically has base class’s: Member variables (attributes) Member functions (constructors, destructors, and friends are not considered member functions for this purpose) Can have additional member functions and attributes Can replace or add to base class’s member functions Will need it’s own constructors, destructors friends and overloaded operators
Background image of page 6
© Janice Regan, CMPT 128, February. 2007 7 Derived Classes Consider example: Class of “Shapes" Some types of shapes are: Circles Squares Triangles Each type of shape is a specialized case of a shape (a class derived or inherited from the shape class)
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Base Class Often don’t want to create objects of the base class A shape contains only basic abstract information about itself it is difficult to specify sensible function
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 44

lecture33 Inheritance(2) - CMPT 128: Introduction to...

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

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