Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions.

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
C++ Review. User Defined Types Built-in data types are simple. Specific problems may demand aggregate/more complex data types. – Ex: polygons, matrices,
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance, Polymorphism, and Virtual Functions
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
Inheritance using Java
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Polymorphism and Virtual Functions. Motivation Polymorphism is one of the fundamental mechanisms offered by OOP, and it is directly related to inheritance.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Chapter 10 Inheritance and Polymorphism
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.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
1 Chapter 4: Another way to define a class Inheritance..!!
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Computer Science Department Inheritance & Polymorphism.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
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.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
CMSC 202 Polymorphism.
Object-Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Polymorphism.
Inheritance and Run time Polymorphism
Learning Objectives Inheritance Virtual Function.
Inheritance, Polymorphism, and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Programming II Polymorphism A.AlOsaimi.
Polymorphism CMSC 202, Version 4/02.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Lecture 6: Polymorphism
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

Copyright 2005, The Ohio State University 1 Inheritance What is Inheritance? Overriding vs. Overloading Access Control –Inheritance Types Virtual Functions Abstract Classes

Copyright 2005, The Ohio State University 2 What is Inheritance ? A method to form new classes using the ones that are already defined Defines an hierarchy Defines a is-a relationship –Student is-a Person –Apple is-a Fruit –Circle is-a Shape Also known as Generalization –Person is an abstraction/generalization of Student –Fruit is a generalization of Apple Derived Class vs. Base Class

Copyright 2005, The Ohio State University 3 Derived Classes Also known as Sub classes Derived class inherits attributes and behavior from Base class => Inheritance Manager is-a Employee class Employee { string name; Date hiring_date; int emp_no; … } class Manager : public Employee { // name, hiring_date, emp_no are inherited int level; Employee *group; … } Employee Manager Base class Derived class Inheritance Type

Copyright 2005, The Ohio State University 4 Examples Rectangle Triangle Polygon Point Circle3D-Point Sphere Mammal Pet Cat

Copyright 2005, The Ohio State University 5 Derived class is generally bigger than Base class –Adds extra attributes and behavior => Specialization –Ex: class Bank_Account - account #, owner, balance class Saving_Account : public Bank_Account - interest rate, interest earned Manager can be used wherever Employee can be used void foo (Manager mm, Employee ee) { Employee * pe = &mm; // ok: every Manager is an Employee Manager * pm = ⅇ // error: Employee need not be a Manager pm->level = 2; // not possible: pm does not know about level pm = static_cast (pe); // ok: pe points to a Manager pm->level = 2; // ok ! } Base Class pointer can point to a Derived Class but not vice versa !!

Copyright 2005, The Ohio State University 6 Derived Classes can choose to modify the behavior of inherited methods  Overriding In case of Specialization, modification can be necessary class Bank_Account { private: string address; int account_no; float balance; public: string name; void print () const; … } class Savings_Acc : public Bank_Account { private: float interest_rate, interest_earned; Date balance_date; public: void print () const { Bank_Account::print ( ); cout << interest_rate … } … } Overriding

Copyright 2005, The Ohio State University 7 Overriding vs. Overloading Overriding operates across scopes Overloading operates within a scope class Point { int x, y; public: void update (int xx, int yy); void update (int xx); void update (int yy); … } class Circle : public Point { int radius; public: update ( int xx, int yy, int rr ); … } Overriding Overloading

Copyright 2005, The Ohio State University 8 Access Control on members Access control of inherited members in derived classes are governed by: 1.Access control of members 2.Inheritance Type Access control of members –Derived class can access public and protected members of base class –protected: Acts as public to derived classes and private to others

Copyright 2005, The Ohio State University 9 Inheritance Types privateprotectedpublic private--- protectedprivateprotected publicprivateprotectedpublic Type of Inheritance Inheritance type defines the access control in derived class No derived class can access private of A In B_priv, public and protected parts of A are private In B_prot, public and protected parts of A are protected In B_public, public parts of A are public and protected parts of A are protected A B_priv Access control in A Access control in B B_prot B_public

Copyright 2005, The Ohio State University 10 Virtual Functions (1) class Employee { // data members public: void print() const; … } class Manager : public Employee { // data members public: void print() const; … } void global_print ( Employee *pe) { pe->print(); } Which print() is invoked??? Given a pointer Base_class *, to which derived type does the object pointed to belong?

Copyright 2005, The Ohio State University 11 Virtual Functions (2) Intelligent way! - Type Fields class Employee { char type; Employee() : type(‘E’) { … } // data members public: void print() const; … } class Manager : public Employee { Manager() { type = ‘M’; } // data members public: void print() const; … } void global_print ( Employee *pe) { if (pe->type == ‘E’) // Print Employee else // Print Manager }

Copyright 2005, The Ohio State University 12 Virtual Functions (3) Type Fields is smart method but it suffers from following: –Cumbersome –Difficult to manage –Difficult to extend Elegant way of doing it is by using Virtual Functions If a function is defined in base and derived classes and if it is virtual, then given a pointer we can unambiguously (at run time) determine which function needs to be invoked Since ambiguity is resolved at run time, it is known as late Binding or Dynamic Binding

Copyright 2005, The Ohio State University 13 Virtual Functions (4) class Foo { public: void f() { cout << "Foo::f()"; } virtual void g() { cout << "Foo::g()"; } class Bar : public foo { public: void f() { cout << “Bar::f()"; } virtual void g() { cout << “Bar::g()"; } Foo foo; Bar bar; Foo *baz = &bar ; Bar *quux = &bar ; foo.f(); // "Foo::f()" foo.g(); // "Foo::g()" bar.f(); // "Bar::f()" bar.g(); // "Bar::g()" // So far everything we would expect... baz->f(); // "Foo::f()" baz->g(); // "Bar::g()" quux->f(); // "Bar::f()" quux->g(); // "Bar::g()"

Copyright 2005, The Ohio State University 14 Abstract Classes (1) Not all classes are concrete They might represent some abstract concept Rectangle Triangle Shape Class shape can define abstract methods like draw and rotate which should be overridden in derived classes class Shape {// intelligent but inelegant public: virtual void draw () { cout << “error: draw() not defined here.”; } virtual void rotate (int degrees) { cout << “error: rotate() not defined here.”; } }

Copyright 2005, The Ohio State University 15 Abstract Classes (2) class Shape {// elegant public: virtual void draw () = 0; // pure virtual function virtual void rotate (int degrees) = 0; // pure virtual function } Class with one or more pure virtual functions is abstract Abstract class can only be used as an interface or base class –i.e., can not instantiate an object of abstract class –Shape s; // silly: shapeless shape We can instantiate only a concrete class

Copyright 2005, The Ohio State University 16