reusability in which new classes are created from existing classes by absorbing their attributes and behaviors, and overriding or embellishing these with capabilities the new classes require Inheritance saves time in program development by reusing proven and debugged high-quality software
Attributes and behaviors What is a class composed of? Data members (attributes) Member functions (behaviors) A child class can “inherit” the data members and member functions from its parent class
members and member functions A child class (derived class) has: The same data members and the same functions as its parent class Its own data members and member functions The ability to make changes to the class functions from the parent class (overriding)
a new class constructed from information in the base class A derived class is more specific than its base class and represents a smaller group of objects For example, class Square is a derived class of base class Rectangle (see inheritance.cpp)
the class definition, no changes to the base class, while derived class has the syntax: class DerivedClass: public BaseClass class BaseClass{ //data members & member functions }; class DerivedClass: public BaseClass{ //data members & member functions };
Class Square is the derived class class Rectangle{ //data members & member functions }; class Square: public Rectangle{ //data members & member functions };
derived class “is-a” specialized and/or extended version of the base class Inheritance is not a “has-a” relationship “has-a” relationships deal with composition
“has-a” a brain A room “has-a” door A Fraction “has-a” numerator and denominator A Complex Number “has-a” real and imaginary part A Rectangle “has-a” length and width
Examples of “is-a” relationships A mammal “is-an” animal A person “is-a” mammal A square “is-a” shape A lab “is-a” room A Square “is-a” Rectangle
member function) of the base class can be “seen” by a derived class Any private data member (or private member function) of the base class cannot be “seen” by a derived class (but we need private access for data encapsulation)
with base class Protected data members & member functions can be seen by a derived class with the “is-a” property Protected data members & member functions cannot be seen by a class with a “has-a” property (so we still have data encapsulation)
how a class works by restricting access to data members, which can only be manipulated by member functions In other words, the implementation details of a class are hidden within the classes themselves Also called “data hiding” or “information hiding”
of its base class first If no constructor exists for derived class, the default constructor is created by the compiler The default constructor is a constructor with no arguments and no code in the function body, but it creates space for the data members when it creates an object
Rectangle, we can use this syntax to call the base class's constructor: DerivedClass(parameters): BaseClass(parameters){ ... } Square(double side):Rectangle(side,side){ /*data members initialized in base class Rectangle, so no code here*/ }
inheritance, we also have derived class Cuboid with base class Rectangle Cuboid is short for "Rectangular Cuboid" A Rectangular Cuboid is basically a box So in addition to the Rectangle’s length and width, the Cuboid also has a height See example program: inheritance.cpp
members in the derived class, we need to initialize them in the constructor Constructor definition for derived class Cuboid Cuboid(double length2, double width2, double height2): Rectangle(length2, width2){ setHeight(height2); }
constructors, so the derived class destructor is called before the base class destructor If no destructor exists for derived class, the default destructor is called
function implementations that it does not need to have To correct this potential problem, a programmer can override the function with another function which has the appropriate implementation Also note that friend functions are not inherited, so you have to rewrite them for each class
setWidth() does some error checking and sets the width However, in the derived class Square, which inherits Rectangle’s function setWidth(), we don’t want to change only the width, because if the width is different than the length, then it is no longer a Square So we override function setWidth(), so that it sets the length to the same value as the width
a function of the base class, when it is also overloaded in the derived class, precede the function with the base class name and binary scope resolution operator or “double-colon” (::) The syntax is: ClassName::function();
Rectangle’s setLength() and setWidth() functions void setSide(double side){ /*use Rectangle's setLength() and setWidth() functions, to set length and width to same value*/ Rectangle::setLength(side); Rectangle::setWidth(side); }
and the derived class (different classes) with the same name and same parameters When this function is called in the derived class, the derived-class version is automatically selected Overloading: functions in the same class with the same name and different parameters Such as constructors with different parameters
of inheritance, we also have derived class Cube with base class Cuboid A Cube is a Rectangular Cuboid with all equal sides The Cube inherits the Rectangle’s length and width, and also inherits the Cuboid’s height, and all member functions We have to override the set() member functions to keep equal values for the length, width and height (see example program: inheritance.cpp)
a base class and a derived class B (derived class) inherits from A (base class) C (derived class) inherits from B (base class) For example, class Cuboid is a derived class from class Rectangle, and class Cuboid is a base class for class Cube
base class set methods, to set length, width, and height to same value*/ Rectangle::setLength(side); Rectangle::setWidth(side); Cuboid::setHeight(side); }