SlideShare a Scribd company logo
1 of 22
C++ Inheritance and a
      QuickTime™
           decompressor
 are needed to see this picture.
           Gordon College
              CPS212
Basics
   OO-programming can be defined as a
    combination of Abstract Data Types (ADTs)
    with Inheritance and Dynamic Binding.
       Encapsulation, inheritance and polymorphism
   Each practice handles a different aspect of
    system composition:
       Encapsulation can be seen as a 2D component - public and
        private interface
       Inheritance adds an additional dimension - the ADT picks up
        the characteristics of another component.
       Polymorphism adds to inheritance - postponing
        implementation decisions until later (perhaps even run-time).
Data Abstraction vs. Inheritance
Basics
   Recall that inheritance is a means of
    specifying hierarchical relationships between
    types
   C++ classes can inherit both data and
    function members from other (parent) classes
   Terminology: "the child (derived or subclass)
    type inherits (or is derived from) the parent
    (base or superclass) type."
The derived type is just the base type
plus:
   Added specializations
       Change implementation
        without changing the
        base class interface
 Added Generalizations
/Extensions
       new operations and/or
        data
What a derived class inherits
   Every data member defined in the parent
    class (although such members may not
    always be accessible in the derived class!)
   Every ordinary member function of the parent
    class (although such members may not
    always be accessible in the derived class!)
What a derived class doesn't inherit
   The base class's constructors and
    destructor
   The base class's assignment operator
   The base class's friends
What a derived class can add
   New data members
   New member functions (also overwrite existing
    ones)
   New constructors and destructor
   New friends
When a derived-class object is created & destroyed

    Space is allocated (on the stack or the heap) for the full object
     (that is, enough space to store the data members inherited from
     the base class plus the data members defined in the derived
     class itself)
    The base class's constructor is called to initialize the data
     members inherited from the base class
    The derived class's constructor is then called to initialize the
     data members added in the derived class
    The derived-class object is then usable
    When the object is destroyed (goes out of scope or is deleted)
     the derived class's destructor is called on the object first
    Then the base class's destructor is called on the object
    Finally the allocated space for the full object is reclaimed
Inheritance in C++
   The class header is modified to allow a
    derivation list consisting of base classes
    (C++ allows multiple inheritance)

class Foo { };
class Bar : public Foo { };
class More : public Foo, public Bar { };
Key Properties of C++ Inheritance
   The “is-a” relationship is maintained
       A pointer to the base type may point to a derived
        type object
   The above relationship combined with
    dynamic binding - promotes a type-secure,
    polymorphic style of programming
       The programmer need not know the actual type of
        an object at compile-time (dynamic-binding)
Simple Base Class (Screen class)   Derived from Screen (Window
                                   class)
Derived from Window (Menu
class)
                                               Inheritance Hierarchy




   Multiple Levels of Derivation

         A pointer to a derived class can
         be assigned to a pointer of any of
         its public base classes without
         requiring an explicit cast: Menu m;
                                   Window &w = m;
                                   Screen *ps1 = &w;
                                   Screen *ps2 = &m;
Public vs private inheritance
   The public keyword in the inheritance syntax
    means that publicly accessible members
    inherited from the base class stay publicly
    accessible in the derived class
   But sometimes its preferable to inherit the
    public members of a parent in such a way
    that they become private in the child
Private inheritance
   Public members of base class become
    private members of derived class
   Public and protected members are only
    available to derived-class member
    functions - not to a derived object.
Protected inheritance
   private members of the base class are not
    accessible in the derived class (to preserve
    encapsulation)
   Protected qualification allows encapsulated
    data members which are not publicly
    accessible to be accessible by derived
    classes
   ("Protected" members are not accessible
    from outside the class, except in derived
    classes)
Initializer lists
   Derived class constructor automatically
    calls the "no argument" base class
    constructor(s)
   But what if a base class has a non-void
    constructor which needs to be called?
       specify which base-class constructor gets
        called, as part of the definition of the
        derived-class constructor
Initializer lists
class Coefficient
{                                               class StatusCoefficient : public Coefficient
public:                                         {
     Coefficient(void)                          public:
                                                     StatusCoefficient(void)
     { myValue = 0; myAccesses = 0; }                { myStatus = OverStatus; }

         Coefficient(double initval)                 StatusCoefficient(double initval,
         { myValue = initval; myAccesses = 0;   Status initStatus)
     }                                               : Coefficient(initval)
                                                     { myStatus = initStatus; }
         // ETC.                                     // ETC.
};                                              };
Using Initializer lists to initialize
values
    Initializer lists can also be used to initialize
     class members with specific value (instead of
   assigning to them in the body of the constructor)
class First
{
     public:
            First() : _foo( "initialize foo first" ), _bar( "then bar" ) { }

      private:
      string _foo;
      string _bar;
};
Polymorphism
   Meaning: some code or operations or objects
    behave differently in different contexts
    Example: the + operation behaves differently depending on the
      operands (overloading)
   member function with the same name can me
    implemented in different classes
       Use :: to refer to a function in the base class with the same
        name - baseCL::function()
   appropriate version of function is determined
    at runtime – dynamically
   virtual member functions used to implement
    polymorphism
Polymorphism
   Dynamic binding:
       At runtime the C++ program selects which
        member function to execute - if the function
        is virtual and exists at the different levels
        within a inheritance hierarchy
   Contrasts with Static Binding
      emp.displayEmployeeInfo();
Resources
   Data Structures with C++, William Ford and William
    Topp
   Single and Multiple Inheritance in C++, Douglas
    Schmidt

More Related Content

What's hot (20)

Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Csharp4 inheritance
Csharp4 inheritanceCsharp4 inheritance
Csharp4 inheritance
 
Seminar on java
Seminar on javaSeminar on java
Seminar on java
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Inheritance
InheritanceInheritance
Inheritance
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java Programming - Inheritance
Java Programming - InheritanceJava Programming - Inheritance
Java Programming - Inheritance
 
Interface
InterfaceInterface
Interface
 

Viewers also liked

Viewers also liked (18)

Inheritance
InheritanceInheritance
Inheritance
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritance
 
Intro To C++ - Class #22: Inheritance, Part 1
Intro To C++ - Class #22: Inheritance, Part 1Intro To C++ - Class #22: Inheritance, Part 1
Intro To C++ - Class #22: Inheritance, Part 1
 
Inheritance
InheritanceInheritance
Inheritance
 
Hybrid Inheritance in C++
Hybrid Inheritance in C++Hybrid Inheritance in C++
Hybrid Inheritance in C++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Intro To C++ - Class #23: Inheritance, Part 2
Intro To C++ - Class #23: Inheritance, Part 2Intro To C++ - Class #23: Inheritance, Part 2
Intro To C++ - Class #23: Inheritance, Part 2
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance
 
INHERITANCE-Oopc ppt-ta4
INHERITANCE-Oopc ppt-ta4INHERITANCE-Oopc ppt-ta4
INHERITANCE-Oopc ppt-ta4
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphism
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 

Similar to Inheritance

lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdfWaqarRaj1
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceKuntal Bhowmick
 
Unit3 packages & interfaces
Unit3 packages & interfacesUnit3 packages & interfaces
Unit3 packages & interfacesKalai Selvi
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer ProgrammingInocentshuja Ahmad
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Javabackdoor
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfacesDevaKumari Vijay
 
Unit3 java
Unit3 javaUnit3 java
Unit3 javamrecedu
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxRudranilDas11
 

Similar to Inheritance (20)

Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
java tutorial 3
 java tutorial 3 java tutorial 3
java tutorial 3
 
Unit3 packages & interfaces
Unit3 packages & interfacesUnit3 packages & interfaces
Unit3 packages & interfaces
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
 
Inheritance
InheritanceInheritance
Inheritance
 
OOP
OOPOOP
OOP
 
Lecturespecial
LecturespecialLecturespecial
Lecturespecial
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
 
06 inheritance
06 inheritance06 inheritance
06 inheritance
 
Unit3 java
Unit3 javaUnit3 java
Unit3 java
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 

Inheritance

  • 1. C++ Inheritance and a QuickTime™ decompressor are needed to see this picture. Gordon College CPS212
  • 2. Basics  OO-programming can be defined as a combination of Abstract Data Types (ADTs) with Inheritance and Dynamic Binding.  Encapsulation, inheritance and polymorphism  Each practice handles a different aspect of system composition:  Encapsulation can be seen as a 2D component - public and private interface  Inheritance adds an additional dimension - the ADT picks up the characteristics of another component.  Polymorphism adds to inheritance - postponing implementation decisions until later (perhaps even run-time).
  • 3. Data Abstraction vs. Inheritance
  • 4. Basics  Recall that inheritance is a means of specifying hierarchical relationships between types  C++ classes can inherit both data and function members from other (parent) classes  Terminology: "the child (derived or subclass) type inherits (or is derived from) the parent (base or superclass) type."
  • 5. The derived type is just the base type plus:  Added specializations  Change implementation without changing the base class interface  Added Generalizations /Extensions  new operations and/or data
  • 6. What a derived class inherits  Every data member defined in the parent class (although such members may not always be accessible in the derived class!)  Every ordinary member function of the parent class (although such members may not always be accessible in the derived class!)
  • 7. What a derived class doesn't inherit  The base class's constructors and destructor  The base class's assignment operator  The base class's friends
  • 8. What a derived class can add  New data members  New member functions (also overwrite existing ones)  New constructors and destructor  New friends
  • 9. When a derived-class object is created & destroyed  Space is allocated (on the stack or the heap) for the full object (that is, enough space to store the data members inherited from the base class plus the data members defined in the derived class itself)  The base class's constructor is called to initialize the data members inherited from the base class  The derived class's constructor is then called to initialize the data members added in the derived class  The derived-class object is then usable  When the object is destroyed (goes out of scope or is deleted) the derived class's destructor is called on the object first  Then the base class's destructor is called on the object  Finally the allocated space for the full object is reclaimed
  • 10. Inheritance in C++  The class header is modified to allow a derivation list consisting of base classes (C++ allows multiple inheritance) class Foo { }; class Bar : public Foo { }; class More : public Foo, public Bar { };
  • 11. Key Properties of C++ Inheritance  The “is-a” relationship is maintained  A pointer to the base type may point to a derived type object  The above relationship combined with dynamic binding - promotes a type-secure, polymorphic style of programming  The programmer need not know the actual type of an object at compile-time (dynamic-binding)
  • 12. Simple Base Class (Screen class) Derived from Screen (Window class)
  • 13. Derived from Window (Menu class) Inheritance Hierarchy Multiple Levels of Derivation A pointer to a derived class can be assigned to a pointer of any of its public base classes without requiring an explicit cast: Menu m; Window &w = m; Screen *ps1 = &w; Screen *ps2 = &m;
  • 14. Public vs private inheritance  The public keyword in the inheritance syntax means that publicly accessible members inherited from the base class stay publicly accessible in the derived class  But sometimes its preferable to inherit the public members of a parent in such a way that they become private in the child
  • 15. Private inheritance  Public members of base class become private members of derived class  Public and protected members are only available to derived-class member functions - not to a derived object.
  • 16. Protected inheritance  private members of the base class are not accessible in the derived class (to preserve encapsulation)  Protected qualification allows encapsulated data members which are not publicly accessible to be accessible by derived classes  ("Protected" members are not accessible from outside the class, except in derived classes)
  • 17. Initializer lists  Derived class constructor automatically calls the "no argument" base class constructor(s)  But what if a base class has a non-void constructor which needs to be called?  specify which base-class constructor gets called, as part of the definition of the derived-class constructor
  • 18. Initializer lists class Coefficient { class StatusCoefficient : public Coefficient public: { Coefficient(void) public: StatusCoefficient(void) { myValue = 0; myAccesses = 0; } { myStatus = OverStatus; } Coefficient(double initval) StatusCoefficient(double initval, { myValue = initval; myAccesses = 0; Status initStatus) } : Coefficient(initval) { myStatus = initStatus; } // ETC. // ETC. }; };
  • 19. Using Initializer lists to initialize values  Initializer lists can also be used to initialize class members with specific value (instead of assigning to them in the body of the constructor) class First { public: First() : _foo( "initialize foo first" ), _bar( "then bar" ) { } private: string _foo; string _bar; };
  • 20. Polymorphism  Meaning: some code or operations or objects behave differently in different contexts Example: the + operation behaves differently depending on the operands (overloading)  member function with the same name can me implemented in different classes  Use :: to refer to a function in the base class with the same name - baseCL::function()  appropriate version of function is determined at runtime – dynamically  virtual member functions used to implement polymorphism
  • 21. Polymorphism  Dynamic binding:  At runtime the C++ program selects which member function to execute - if the function is virtual and exists at the different levels within a inheritance hierarchy  Contrasts with Static Binding emp.displayEmployeeInfo();
  • 22. Resources  Data Structures with C++, William Ford and William Topp  Single and Multiple Inheritance in C++, Douglas Schmidt

Editor's Notes

  1. See example - inheritance project
  2. See example - inheritance project “inheritance2”
  3. See example - inheritance project “inheritance2”
  4. See example - inheritance3 project
  5. What base class constructor gets called - when a derived class object is created?
  6. See page 735 - (see example at bottom of page) derivedCL::derivedCL (bArg1,…bArgm, dArg1…dArgn): baseCL(bArg1,…bArgm), dData1(dArg1),…dDatan(dArgn) { };
  7. See example - polymorphism