Object Oriented Programming in C++ Chapter 6 Inheritance.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Class and Objects.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
Lecture 9 Concepts of Programming Languages
C++ fundamentals.
 By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
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 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Polymorphism &Virtual Functions
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 8 More Object Concepts
1 CSC241: Object Oriented Programming Lecture No 13.
Case Study - Fractions Timothy Budd Oregon State University.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
OOP, Virtual Functions and Inheritance
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Inheritance Chapter 9. 2 What You Will Learn Software reusability (Recycling) Inheriting data members and functions from previously defined classes.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Peyman Dodangeh Sharif University of Technology Fall 2014.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
Object-Oriented Programming Chapter Chapter
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Object Oriented Programming
ISBN Object-Oriented Programming Chapter Chapter
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Inheritance and Composition Reusing the code and functionality Unit - 04.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Chapter 2 Objects and Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance CMSC 202, Version 4/02.
7. Inheritance and Polymorphism
Object-Oriented Programming (OOP) Lecture No. 45
Lecture 13.
Object-Oriented Design (OOD) and C++
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Advanced C++ Topics Chapter 8.
Virtual Functions Department of CSE, BUET Chapter 10.
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 19 - Inheritance Outline 19.1 Introduction
Chapter 11: Inheritance and Composition
Chapter 8: Class Relationships
Object-Oriented Programming (Part 2)
Chapter 5 Classes.
Presentation transcript:

Object Oriented Programming in C++ Chapter 6 Inheritance

Introduction In this section we will examine why we use inheritance base classes and derived classes protected members casting of derived class references to base class references casting of base class pointers to derived class pointers use of member functions redefinition of base class members public, protected and private base classes direct and indirect base class classes constructors and destructors in derived classes implicit conversion relationships multiple inheritance

Why do we use Inheritance? We use inheritance because it promotes software reuse by allowing the defining of a heirarchy of classes with successive refinement of behavior additional behavior restrictions on behaviour additional data members allows the construction of software by using existing class libraries and tailoring them to the current situation better supports the abstraction of the essentials of the problem

Base Classes and Derived Classes Derived classes inherit from their base class Derived classes obtain their own copies of all the base class’s data members and member functions (albeit with restrictions) Each instance of a derived class is also an instance of the base class (the inverse is not true) Derived classes can themselves become base classes, allowing successive refinement of an abstraction process and of the behavior

Inheritance syntax If we wished to implement this inheritance pattern, given the Student base class, we might write class Postgrad : public Student {... }; This is called public inheritance. Protected and private inheritance are also possible but are far less common. Post Grad Student Student

An Inheritance Hierarchy

Terminology Inheritance (Derivation) - Producing new classes by inheriting methods and data from an existing class or classes Superclass Base class Derived class Sub-class

Private and Protected Members When we use public inheritance Private members (either data or functions) of a base class are not accessible to derived classes, except through public member functions of the base class. Public members of a base class are accessible to the derived class. There is an intermediate category of access protection called protected. Protected members of the base class can only be accessed by members and friends of the base class and of any derived class. Protected data violates data encapsulation, in that changes to the base class protected members may require changes to all derived classes.

Protected Members not accessible Private Public Protected Accessible to derived classes only Derived Class

Constructors and Destructors in Derived Classes A base class initialiser can be called in the derived class’s constructor, if not the default constructor will be called. Base class constructors are called before the derived class constructor is called. Destructors are called in the reverse order of construction, i.e. derived class before base class.

Example class Buffer { int* ptr; int size; public: Buffer(int sz) { ptr = new int[ size=sz]; } int& operator[](int i) { return ptr[i]; } boolean inRange(int); ~Buffer() { delete [] ptr; } };

A Derived Class class Stack : public Buffer { int top; public: Stack(int sz) : Buffer(sz) { top = -1; } void push(int x); int pop( ); boolean empty( ); boolean full( ); };

Compatibility Between Base and Derived Classes An object of a derived class can be treated as an object of its base class. The reverse is not true.

Example: main() { Stack stk(30); int i, for( i=0; i<10; ++i) stk.push(i); fun1(stk); fun2(stk);... void fun1(Buffer bf) { for( int i=0; i<10; i++) cout << bf[i] << ‘ ‘ ; cout << ‘\n’ ; } void fun2(Stack st) { while( !st.empty() ) cout << st.pop() << ‘ ‘ ; cout << ‘\n’ ; }

Casting of References and Pointers It is possible to cast a reference to a derived class to that of the base class. This is common, as it allows you to get at the base classes functionality. It is also possible to cast a pointer of a derived class to that of the base class, and again this is common. It is also possible to cast a pointer of a base class to that of the derived class. This is perilous (but not illegal).

Example class Student { friend ostream &operator<< (ostream &, const Student &); friend istream &operator>> (istream &, Student &); public: Student(); private: char name[30]; char degree[15]; }; class Postgrad : public Student { friend ostream & operator<< (ostream &, const Postgrad &); friend istream & operator>> (istream &, Postgrad &); public: Postgrad(); private: char Supervisor[20]; char Thesis[20]; };

Student class I/O operators ostream &operator<< (ostream &output, const Student &s) { output << "Name : " << s.name << endl; output << "Degree : " << s.degree << endl; return output; } istream &operator>> (istream &input, Student &s) { input >> s.name >> s.degree; return input; }

Post Grad class I/O operators ostream &operator<< (ostream &output, const Postgrad &p) { output p; output << "Supervisor : " << p.Supervisor << endl; output << "Thesis Title : " << p.Thesis << endl; return output; } istream &operator>> (istream &input, Postgrad &p) { cout << "Please input Name then Degree : "; input >> static_cast p; cout << "Please input Supervisor and Title : " ; input >> p.Supervisor >> p.Thesis; return input; }

Test program main() { Student s, *s1; Postgrad p, *p1; cout << "Please input Name then Degree :"; cin >> s; cout << "You input " << s << endl; cout << "Postgraduate Student Entry\n"; cin >> p; cout << "You input\n" << p; p1 = static_cast &s; s1 = static_cast &p; cout << *s1; cout << *p1; return 0; } Casting of pointers, the first of the base class to the derived class, the next of the derived class to the base class

The Output Please input Name then Degree :a b You input Name : a Degree : b Postgraduate Student Entry Please input Name then Degree : c d Please input Supervisor and Title : e f You input Name : c Degree : d Supervisor : e Thesis Title : f Name : c Degree : d Name : a Degree : b Supervisor : -_n_ Thesis Title : orland C++ - Copyright 1991 Borland Intl.

Public, Protected and Private Base Classes private inheritance public inheritance protected inheritance Base Class Specifer public protected private public in derived class protected in derived class hidden hidden private in derived class hidden

Direct and Indirect Base Classes If a class inherits explicitly from a base class, then this is called a direct base class. If, however, a class inherits from 2 or more levels up the heirarchy, and not explicitly, this is called an indirect base class Person Indirect Base Class Employee Direct Base Class Manager Derived Class

What is and is not inherited Inherited Member functions Data members Not inherited Constructors & Destructors Friendship New data members or member functions may be added Inherited functions may be redefined Inherited members cannot be removed

Inheritance Benefits Software reusability Code sharing Consistency of interface Software components Rapid prototyping Polymorphism Information hiding Costs Execution speed Program size Message passing overhead Program complexity