Inheritance and Composition Reusing the code and functionality Unit - 04.

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1.
Inheritance, Polymorphism, and Virtual Functions
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Operator Overloading Customised behaviour of operators Unit - 06.
Chapter 12: Adding Functionality to Your Classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Inheritance Inheritance is a relationship among classes wherein one class shares the structure and/or behavior that is defined in one or more other classes.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Object Oriented Programming in C++ Chapter 6 Inheritance.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Chapter 10 Inheritance and Polymorphism
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see.
Overview of C++ Polymorphism
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
©Copyrights 2016 Eom, Hyeonsang All Rights Reserved Computer Programming Object Oriented Programming & C++ 1 st Lecture 엄현상 (Eom, Hyeonsang) Department.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Abstract Classes & Object Slicing. Object Slicing.
Programming in C++ Michal Brabec Petr Malý. Inheritance class derived-class: access-specifier base-class {} Base & Derived class Implementation inheritance.
A First Book of C++ Chapter 12 Extending Your Classes.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Chapter 2 Objects and Classes
C++ Lesson 1.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CS 3370 – C++ Object-oriented Programming
7. Inheritance and Polymorphism
Andy Wang Object Oriented Programming in C++ COP 3330
Object-Oriented Programming (OOP) Lecture No. 45
Inheritance and Polymorphism
Polymorphism &Virtual Functions
Polymorphism & Virtual Functions
Chapter 2 Objects and Classes
Reserved Words.
Programming with ANSI C ++
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Lecture 5 Inheritance, Polymorphism, Object Memory Model, The Visitor Pattern …and gazillion other tidbits!
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Overview of C++ Polymorphism
CS410 – Software Engineering Lecture #8: Advanced C++ III
Object Slicing & Dynamic Casts
Presentation transcript:

Inheritance and Composition Reusing the code and functionality Unit - 04

Unit Introduction This unit covers composition and inheritance

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

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

Example: Composition class X { private: private: int m_Data; // data member int m_Data; // data member public: public: X() {} // default constructor SetX (int k) // member function { m_Data = k; m_Data = k;}}; class Y { private: private: int m_Data;

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

Inheritance Parent class is called the base class 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 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

Example: Inheritance class X { int m_Data; int m_Data; public: public: X() {} X() {}}; class Y : public X // class Y publicly inherits class X { int m_Data; int m_Data; public: public: Y() {} Y() {}};

Constructor and Inheritance The constructor and destructor calls are automatic for the base class, in case the derived class object is created 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: The order of base and derived class constructors and destructors are: base constructor base constructor derived constructor derived constructor derived destructor derived destructor base destructor base destructor

Multiple Inheritance There could be two or more base class for a derived class There could be two or more base class for a derived class It can cause ambiguity and is not recommended in normal cases 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 There are ways to have single inheritance used instead of multiple inheritance to achieve the goal class Z : public X, public Y { // class definition }

Example: Multiple Inheritance class A { public: public: void SomeFunction(void) void SomeFunction(void){ // function code }}; class B { public: public: void SomeFunction(void) void SomeFunction(void){ // function code }};

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

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

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 // accessor methods for member variables};

Example: Upcasting and Downcasting (contd.) void main() { Person* pPerson = new Employee();// upcast, implicit pPerson->GetName();// fine pPerson->GetDepartment();// error Employee* pEmployee = (Employee*)pPerson;// downcast pEmployee->GetDepartment();// fine now }

C++ Casting Operators Replace the traditional, parenthetic casting technique Replace the traditional, parenthetic casting technique Explicit Explicit Self documenting Self documenting Signal a type conversion Signal a type conversion Less risk of illegal cast and data corruption Less risk of illegal cast and data corruption

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

Example: static_cast // Note that Base is not a virtual class class Base{ }; class Derived : public Base { }; void f(Base* pBase) { Derived* pDerived = static_cast pBase } // Also, enum fruit{apple=0, orange=1, banana=2}; int x = 2; fruit f1 = static_cast (x); // converts an int to enum

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_cast pBase // error // use dynamic_cast instead Derived* pDerived = dynamic_cast pBase // fine }

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_cast (d) ); }

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 // reinterpret casting a pointer to integral value unsigned int val = reinterpret_cast (p); unsigned int val = reinterpret_cast (p); val = val & 16; // hash code formula val = val & 16; // hash code formula return (unsigned short)val; // cast to the returned type return (unsigned short)val; // cast to the returned type} void main() { int a[10]; int a[10]; for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++) { cout << Hash(a + i) << endl;// using Hash() cout << Hash(a + i) << endl;// using Hash() }}

Inheritance Types Inheritance has three types: Inheritance has three types: public public private private protected protected The following chart gives the accessibility details of the inheritance types: The following chart gives the accessibility details of the inheritance types:

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 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 }

Inheritance vs. Composition Composition is generally used when you want the features of an existing class inside your new class, but not its interface 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) 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)

Unit Summary In this unit you have covered … Composition Composition Inheritance Inheritance Multiple inheritance Multiple inheritance Upcasting & inheritance Upcasting & inheritance C++ casting operators C++ casting operators Types of inheritance Types of inheritance Inheritance vs. composition Inheritance vs. composition