Inheritance and Composition - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Inheritance and Composition

Description:

Unit - 04 Inheritance and Composition Reusing the code and functionality Unit Introduction This unit covers composition and inheritance Unit Objectives After covering ... – PowerPoint PPT presentation

Number of Views:170
Avg rating:3.0/5.0
Slides: 26
Provided by: tech471
Category:

less

Transcript and Presenter's Notes

Title: Inheritance and Composition


1
Inheritance and Composition
Unit - 04
  • Reusing the code and functionality

2
Unit Introduction
  • This unit covers composition and inheritance

3
Unit Objectives
  • After covering this unit you will understand
  • Composition
  • Inheritance
  • Inheritance constructor call sequence
  • Multiple inheritance
  • Upcasting inheritance
  • C casting operators
  • Different types of Inheritance
  • Inheritance vs. composition

4
Composition (Aggregation)
  • Creating and using objects of other class within
    a different class
  • Also known as embedded object (or sub-object)

5
Example Composition
  • class X
  • private
  • int m_Data // data member
  • public
  • X() // default constructor
  • SetX (int k) // member function
  • m_Data k
  • class Y
  • private
  • int m_Data

6
Example Composition (contd.)
  • public
  • X x // composition
  • Y() //default constructor
  • void main()
  • Y y // creating object of class Y
  • y.x.SetX(20) // Access/sets the embedded
    object

7
Inheritance
  • Parent class is called the base class
  • The class which inherits the base class is called
    the derived class and has all the features of
    the base class, plus its own features

8
Example Inheritance
  • class X
  • int m_Data
  • public
  • X()
  • class Y public X // class Y publicly inherits
    class X
  • int m_Data
  • public
  • Y()

9
Constructor and Inheritance
  • The constructor and destructor calls are
    automatic for the base class, in case the derived
    class object is created
  • The order of base and derived class constructors
    and destructors are
  • base constructor
  • derived constructor
  • derived destructor
  • base destructor

10
Multiple Inheritance
  • There could be two or more base class for a
    derived class
  • It can cause ambiguity and is not recommended in
    normal cases
  • There are ways to have single inheritance used
    instead of multiple inheritance to achieve the
    goal
  • class Z public X, public Y
  • // class definition

11
Example Multiple Inheritance
  • class A
  • public
  • void SomeFunction(void)
  • // function code
  • class B
  • public
  • void SomeFunction(void)
  • // function code

12
Example Multiple Inheritance (contd.)
  • class C public A, public B
  • public
  • void MyFunction(void)
  • SomeFunction() // Ambiguous as the compiler
    // does not know whether to //choose
    SomeFunction() of A or B

13
Upcasting and Downcasting
  • Casting from derived to base class is known as
    Upcasting (implicit)
  • Upcasting lose the derived class properties
  • Casting from base to derived class is known as
    Downcasting
  • Downcasting makes derived classs properties
    available

14
Example Upcasting and Downcasting
class Person private int Age char
Name char Gender public // accessor
methods for member variables class Employee
public Person private char
Department int EmployeeCode public //
accessor methods for member variables
15
Example Upcasting and Downcasting (contd.)
void main() Person pPerson new
Employee() // upcast, implicit pPerson-gtGetName(
) // fine pPerson-gtGetDepartment() //
error Employee pEmployee (Employee)pPerson /
/ downcast pEmployee-gtGetDepartment() // fine
now
16
C Casting Operators
  • Replace the traditional, parenthetic casting
    technique
  • Explicit
  • Self documenting
  • Signal a type conversion
  • Less risk of illegal cast and data corruption

17
C Casting Operators (contd.)
  • static_cast
  • Compile-time cast
  • Casting classes that provide static polymorphism
    (non-virtual classes)
  • dynamic_cast
  • Run-time cast, to cast classes that provide
    dynamic polymorphism (virtual classes)
  • const_cast
  • To remove const-ness
  • reinterpret_cast
  • For conversion of unrelated types

18
Example static_cast
// Note that Base is not a virtual class class
Base class Derived public Base void
f(Base pBase) Derived pDerived
static_castltDerivedgtpBase // Also, enum
fruitapple0, orange1, banana2 int x
2 fruit f1 static_castltfuitgt(x) // converts
an int to enum
19
Example dynamic_cast
// Note that Base is a virtual class class
Base virtual void f() class Derived
public Base void f () void f(Base
pBase) //Derived pDerived
static_castltDerivedgtpBase // error // use
dynamic_cast instead Derived pDerived
dynamic_castltDerivedgtpBase // fine
20
Example const_cast
  • double f(double d) // f accepts reference to
    type double
  • void g(const double d)
  • // while calling f() the const-ness of d is
    removed
  • double val f( const_castltdoublegt(d) )

21
Example reinterpret_cast
// a practical example could returns hash code
based on an // address unsigned short Hash(void
p) // reinterpret casting a pointer to
integral value unsigned int val
reinterpret_castltunsigned intgt (p) val val
16 // hash code formula return (unsigned
short)val // cast to the returned type void
main() int a10 for (int i 0 i lt 10
i) cout ltlt Hash(a i) ltlt endl //
using Hash()
22
Inheritance Types
  • Inheritance has three types
  • public
  • private
  • protected
  • The following chart gives the accessibility
    details of the inheritance types

23
Inheritance Types (contd.)
  • Protected Function in the base class is treated
    as Private Function in the child class (taking
    the example of the value in bold), when inherited
    using the private keyword
  • class X public Y, private Z, protected W
  • // class definition

24
Inheritance vs. Composition
  • Composition is generally used when you want the
    features of an existing class inside your new
    class, but not its interface
  • Use inheritance for sub-typing, where you want
    your new type to have exactly the same interface
    as the existing type (plus any other member
    functions you want to add)

25
Unit Summary
  • In this unit you have covered
  • Composition
  • Inheritance
  • Multiple inheritance
  • Upcasting inheritance
  • C casting operators
  • Types of inheritance
  • Inheritance vs. composition
Write a Comment
User Comments (0)
About PowerShow.com