1 / 14

Inheritance in C++

Inheritance in C++. Bryce Boe 2012/08/28 CS32, Summer 2012 B . Overview. Assignment Operator Inheritance Descendants and Ancestors Instance variables and methods Protected Constructors Calling ancestor functions Polymorphism. Overloading Assignment Operator.

clare
Télécharger la présentation

Inheritance in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B

  2. Overview • Assignment Operator • Inheritance • Descendants and Ancestors • Instance variables and methods • Protected • Constructors • Calling ancestor functions • Polymorphism

  3. Overloading Assignment Operator • Used when assigning an object to another object: • Tuple a_tuple, b_tuple(5); • a_tuple = b_tuple; void operator =(const Tuple &other); Tuple& operator =(const Tuple &other);

  4. Inheritance • Classes can extend existing classes • Member variables and member functions are inherited by the extending class • An inheriting class is called a child class, derived class, or subclass • The inherited calss is called a parent class, base class, or superclass.

  5. How to inherit class NamedTuple: public Tuple { … };

  6. Ancestors and Descendants • Any class that inherits directly or indirectly from some class is said to be a descendant of that class • C inherits B which inherits A • Both C and B are descendants of A • Any class which has descendants, is said to be an ancestor of those classes • C inherits B which inherits A • A is an ancestor to both B and C

  7. Constructors are not inherited • Constructors need to be redefined (you probably want to anyway as you’ll likely add instance variables to a new class) Exception: If no constructors are defined in the child-class, then it will inherit the default constructor

  8. Instance Methods and Variables • Child classes inherit both instance methods and variables • Private instance methods are unusable as they cannot be invoked • Private instance variables still store data, but they can only be used via accessors and mutators

  9. Protected Keyword • Declaring variables as protected in the parent class, allows descendant classes to access them directly

  10. Redefining Instance Methods • Instance methods can be redefined in child classes • The method just needs to be declared and defined in the context of the child class Note: Function names only have to be declared if new or being redefined

  11. Destructor • The parent class destructors are automatically invoked after the child class destructors

  12. virtual keyword • The virtual keyword allows for dynamic dispatch • Dynamic dispatch means to lookup the appropriate function to call at run-time • Useful when working only with ancestor class types, but you want to call the specific descendant class function

  13. More on Virtual • The virtual property is inherited, thus it isn’t required to have virtual in the function declaration when overriding a method

  14. Polymorphism • The use of the virtual keyword is polymorphism • The behavior of an instance changes depending on its actual implementation

More Related