Division of Mathematics and Computer Science Maryville College
Outline Inheritance 1 Inheritance 2 3
Outline Inheritance 1 Inheritance 2 3
The "is-a" Relationship Object classification is typically hierarchical. An object can be said to be either its type or another. For example it is correct to say "An alpaca is a mammal." When the "is-a" relationship is present, that implies inheritance.
Overview of Inheritance Inheritance is when a subclass is created from a base class. In our example "Mammal" is the base class and "Alpaca" is the subclass. A subclass takes on all of the attributes and methods of its base class. A subclass can override base class functions. A subclass has access to the base class s public and protected members. A subclass is a more specific class than its base class.
Syntax Inheritance class Alpaca : public Mammal { //class stuff here }; Visibility Specifiers public Public members of the base class become public members of the subclass protected Public members of the base class become protected members of the subclass private All members of the base class become private members of the subclass.
Overriding Functions Inheritance If a function in the base class and subclass both have the same function signature (name and parameter types), then the function is overridden. Invoking the function on an object of the subclass type will call the subclass version of the function. This is the beginning of the real power of inheritance. Let s play with this concept for a moment. (inheritance.cpp example)
Outline Inheritance 1 Inheritance 2 3
Overview of is a Greek-derived term which means "many forms". A reference to a subclass can be treated as a reference to a base class. The base class reference still behaves like the subclass! Allows us to program to the "general case", and further modularize software development. Allows us to program to the "general case", and further modularize software development. This is the most powerful aspect of OOP, but it is the one of the most misunderstood and incorrectly used feature of C++.
The virtual Specifier A function can be specified as virtual during its initial definition. A virtual function is a function which has been marked for dynamic binding. Invoking a virtual function on a base class pointer or reference will call the most-specific version of the function. This is what allows subclasses to change behaviors in code they never see! Unless you are really really really really certain you should do otherwise, member functions should be marked virtual. Let s try it out. (polymorphism.cpp example)
and Pointers only works with pointers and references. A statically defined object cannot participate in polymorphism! This is why we generally use pointers when referring to objects.
Up-casting and Down-casting Consider the following: Mammal *m; Alpaca *a1, *a2; a1 = new Alpaca; Up-casting - When a reference or pointer to a subclass is assigned to a reference or pointer of a base class. In C++, up-casting is implicit. m = a1; Down-casting - When a reference or pointer to a base class is converted to a reference or pointer of a subclass. This must be done explicitly. a2 = (Alpaca*) m;
Pure Virtual Functions and Abstract Classes A pure virtual function has no implementation in the base class. Such a function is created by assigning it the value of nullptr at definition time: virtual void display()=0; A class containing a pure virtual function is said to be an "abstract class". An abstract class cannot be instantiated. Subclasses are required to override all pure virtual functions.
Outline Inheritance 1 Inheritance 2 3
Program Specifications Allow the user to move a cursor around on the canvas. Pressing the first letter of a shape begins drawing it. Move the cursor and press enter to set points in the shape. When the shape is complete, it will appear on the canvas.
The Shapes Program Classes