Recitation - Mar 21, 2011
Inheritance
- Additional language feature/construct.
- Facilitates the design of classes being modeled as real world
concepts.
- Can be used to make the code more closely resemble these real world
concepts.
- Inheritance can be used to more directly support the application
design.
- Expresses a relationship between classes.
- Derived class can be thought of as inheriting the
properties from its base class.
- Base class is sometimes referred to as a superclass.
- Derived class is sometimes referred to as a subclass.
- Graphical representation
- Syntax
class BASE { /* ... */ }
class DERIVED : public BASE { /* ... */ }
class Shape { /* ... */ }
class Rectangle : public Shape { /* ... */ }
Virtual Functions
- Declares functions in a base class that can be redefined in a
derived class.
- Pure virtual function is a function that must be
defined by a non-abstract, dervied class.
- Abstract class
- class that has one or more pure virtual functions.
- class that only makes sense as a base for a derived object,
but does not make sense as a standalone object.
- Syntax
class CLASS_NAME {
...
virtual void FUNCTION() = 0;
};
Examples
Additional Notes
- Test 2 is on Tues, March 29