Inheritance
Adapted from Textbook (SAVITCH)
Inheritance
• We may need a process by which a new class
can be created from another class.
• Inheritance gives an ability to define new classes
using existing classes as a basis; those are
called “derived” and “base” classes, respectively
(a.k.a. parent/ancestor-child/descendant
classes).
• Inheritance provides code reuse
– Allows one class to "derive" from another,
adding features
Inheritance
• Derived class inherits data members and
member functions from base class(es).
• Derived class objects inherit members of
base class
–And may add members
Inheritance
• Private member variables in base class cannot
be accessed "by name" in derived
• Protected members in base class:
– Can be accessed "by name" in derived class
member functions
• Constructors are not inherited
– Are invoked from derived class’ constructor
Single and Multiple
• When a class is derived from a base class
this inheritance is called “single
inheritance”.
• A class may inherit from more than one
class; this type of inheritance is “multiple
inheritance”.
Access Restrictions
• Inheritance does not occur without
limitations.
• Based on access restrictions there are
three forms:
– Public
– Protected
– Private
Syntax
class X{
…
};
class Y : public X{
…
};
Class Y is derived from class X with “public”
derivation
Protected Members
• Members that can be accessed within the
same class and by classes derived from
that class, also by friends of the class.
#include <iostream>
using namespace std;
class A{
protected:
int number;
};
class B:public A{
public:
void f(){number=0; cout << ++number;}
};
main(){
B myb;
myb.f();
}
Example
class A{
public:
int a,b;
};
class B : public A{
public:
int b,c;
};
main(){
B b; // b as object name of type B
b.a=1; // a as inherited from the base class A
b.b=3; // 2nd b is of derived class B
b.A::b=2; // 2nd b is of base class A
b.c=4; // c is of derived class B
}
Multiple Inheritance
• You may need a class to inherit more than one
base class.
• C++ provides such flexibility with multiple
inheritance
• For example, a police car and an ambulance are
both emergency vehicles.
– Both classes may inherit from vehicle and
emergency parent classes although vehicle and
emergency classes are not linked to each other
Example
car
emergency truck
vehicle
police car ambulance fire engine
class vehicle
{...};
class emergency
{...};
class car:public vehicle
{...};
class truck:public vehicle
{...};
class policeCar:public car, public emergency
{...};
class ambulance:public car, public emergency
{...};
class fireEngine:public truck, public emergency
{...};

week14 (1).ppt

  • 1.
  • 2.
    Inheritance • We mayneed a process by which a new class can be created from another class. • Inheritance gives an ability to define new classes using existing classes as a basis; those are called “derived” and “base” classes, respectively (a.k.a. parent/ancestor-child/descendant classes). • Inheritance provides code reuse – Allows one class to "derive" from another, adding features
  • 3.
    Inheritance • Derived classinherits data members and member functions from base class(es). • Derived class objects inherit members of base class –And may add members
  • 4.
    Inheritance • Private membervariables in base class cannot be accessed "by name" in derived • Protected members in base class: – Can be accessed "by name" in derived class member functions • Constructors are not inherited – Are invoked from derived class’ constructor
  • 5.
    Single and Multiple •When a class is derived from a base class this inheritance is called “single inheritance”. • A class may inherit from more than one class; this type of inheritance is “multiple inheritance”.
  • 6.
    Access Restrictions • Inheritancedoes not occur without limitations. • Based on access restrictions there are three forms: – Public – Protected – Private
  • 7.
    Syntax class X{ … }; class Y: public X{ … }; Class Y is derived from class X with “public” derivation
  • 8.
    Protected Members • Membersthat can be accessed within the same class and by classes derived from that class, also by friends of the class. #include <iostream> using namespace std; class A{ protected: int number; }; class B:public A{ public: void f(){number=0; cout << ++number;} }; main(){ B myb; myb.f(); }
  • 9.
    Example class A{ public: int a,b; }; classB : public A{ public: int b,c; }; main(){ B b; // b as object name of type B b.a=1; // a as inherited from the base class A b.b=3; // 2nd b is of derived class B b.A::b=2; // 2nd b is of base class A b.c=4; // c is of derived class B }
  • 10.
    Multiple Inheritance • Youmay need a class to inherit more than one base class. • C++ provides such flexibility with multiple inheritance • For example, a police car and an ambulance are both emergency vehicles. – Both classes may inherit from vehicle and emergency parent classes although vehicle and emergency classes are not linked to each other
  • 11.
    Example car emergency truck vehicle police carambulance fire engine class vehicle {...}; class emergency {...}; class car:public vehicle {...}; class truck:public vehicle {...}; class policeCar:public car, public emergency {...}; class ambulance:public car, public emergency {...}; class fireEngine:public truck, public emergency {...};