C inheritance
Download
1 / 22

C++ Inheritance - PowerPoint PPT Presentation


  • 443 Views
  • Updated On :

C++ Inheritance. 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:

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'C++ Inheritance' - Mercy


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
C inheritance

C++ Inheritance

Gordon College

CPS212


Basics
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
DataAbstractionvs. Inheritance


Basics1
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
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
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
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
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
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
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
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
Simple Base Class (Screen class)

Derived from Screen (Window class)


Derived from window menu 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
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
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
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
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 lists1
Initializer lists

class StatusCoefficient : public Coefficient

{

public:

StatusCoefficient(void)

{ myStatus = OverStatus; }

StatusCoefficient(double initval, Status initStatus)

: Coefficient(initval)

{ myStatus = initStatus; }

// ETC.

};

class Coefficient

{

public:

Coefficient(void)

{ myValue = 0; myAccesses = 0; }

Coefficient(double initval)

{ myValue = initval; myAccesses = 0; }

// ETC.

};


Using initializer lists to initialize values
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
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


Polymorphism1
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
Resources

  • Data Structures with C++, William Ford and William Topp

  • Single and Multiple Inheritance in C++, Douglas Schmidt