Inheritance & Polymorphism

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
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.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Chapter 15 What is Inheritance?
Inheritance, Polymorphism, and Virtual Functions
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Chapter 12: Adding Functionality to Your Classes.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
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.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter -6 Polymorphism
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Coming up: Inheritance
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.
Overview of C++ Polymorphism
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
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,
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
1 CSE 2341 Object Oriented Programming with C++ Note Set #12.
Pointer to an Object Can define a pointer to an object:
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Andy Wang Object Oriented Programming in C++ COP 3330
Object-Oriented Design (OOD) and C++
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Object-Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Polymorphism.
Review: Two Programming Paradigms
Introduction to Classes
Polymorphism & Virtual Functions
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Polymorphism Lec
OOP’S Concepts in C#.Net
Introduction to Classes
Java Programming Language
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Overview of C++ Overloading
Polymorphism Polymorphism
Programming II Polymorphism A.AlOsaimi.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Inheritance: Polymorphism and Virtual Functions
CISC/CMPE320 - Prof. McLeod
Overview of C++ Polymorphism
Inheriting Multiple Base Classes
Inheritance: Polymorphism and Virtual Functions
Lecture 10 Concepts of Programming Languages
CS410 – Software Engineering Lecture #8: Advanced C++ III
C++ Object Oriented 1.
Presentation transcript:

Inheritance & Polymorphism

Inheritance

Contents of grade.h #ifndef GRADE_H #define GRADE_H // Grade class declaration class Grade { private: char letter; float score; void calcGrade(void); public: void setScore(float s) { score = s; calcGrade();} float getScore(void) {return score; } char getLetter(void) { return letter; } }; #endif

Contents of test.h #ifndef TEST_H #define TEST_H // Test class declaration class Test { private: int numQuestions; float pointsEach; int numMissed; public: Test(int, int); }; #endif

Contents of test.h #ifndef TEST_H #define TEST_H #include "grade.h" // Must include Grade class declaration. // Test class declaration class Test : public Grade { private: int numQuestions; float pointsEach; int numMissed; public: Test(int, int); }; #endif

Passing Arguments to Base Class Constructors Assume a class called Cube is derived from a class called Rect. Here is how the constructor looks in Rect: Rect::Rect(float w, float l) { width = w; length = l; area = length * width; }

Cube constructor: Cube::Cube(float wide, float long, float high) : Rect(wide, long) { height = high; volume = area * high; }

General Form: <class name>::<class name>(parameter list) : <base class name> (parameter list) Note that the derived class constructor must take enough arguments to be able to pass to the base class constructor

Inheritance & Constructor Ordering class A { public: A(int i) :m_i(i) { cout << "A“ << endl;} ~A() {cout<<"~A"<<endl;} private: int m_i; }; class B : public A { B(int i, int j) :A(i), m_j(j) { cout << “B” << endl;} ~B() {cout << “~B” << endl;} int m_j_; int main (int, char *[]) { B b(2,3); return 0; Class and member construction order B constructor called on object b in main Passes integer values 2 and 3 B constructor calls A constructor passes value 2 to A constructor via initializer list A constructor initializes member m_i with passed value 2 Body of A constructor runs outputs “A” B constructor initializes member m_j with passed value 3 Body of B constructor runs outputs “B”

Inheritance & Destructor Ordering class A { public: A(int i) :m_i(i) { cout << "A“ << endl;} ~A() {cout<<"~A"<<endl;} private: int m_i; }; class B : public A { B(int i, int j) :A(i), m_j(j) { cout << “B” << endl;} ~B() {cout << “~B” << endl;} int m_j_; int main (int, char *[]) { B b(2,3); return 0; Class and member destructor order: B destructor called on object b in main Body of B destructor runs outputs “~B” B destructor calls member m_j “destructor” int is a built-in type, so it’s a no-op B destructor calls A destructor Body of A destructor runs outputs “~A” A destructor calls member m_i destructor again a no-op Compare dtor and ctor order at the level of each class, the order of steps is reversed in ctor vs. dtor ctor: base class, members, body dtor: body, members, base class

Virtual Functions class A { public: A () {cout<<" A";} virtual ~A () {cout<<" ~A";} }; class B : public A { B () :A() {cout<<" B";} virtual ~B() {cout<<" ~B";} int main (int, char *[]) { // prints "A B" A *ap = new B; // prints "~B ~A" : would only // print "~A" if non-virtual delete ap; return 0; Used to support polymorphism with pointers and references Declared virtual in a base class can be overridden in derived class Overriding only happens when signatures are the same Otherwise it just overloads the function or operator name Ensures derived class function definition is resolved dynamically E.g., that destructors farther down the hierarchy get called

Virtual Functions Only matter with pointer or reference class A { public: void x() {cout<<"A:x";}; virtual void y() {cout<<"A:y";}; }; class B : public A { void x() {cout<<"B:x";}; virtual void y() {cout<<"B:y";}; int main () { B b; A *ap = &b; B *bp = &b; b.x (); // prints "B:x" b.y (); // prints "B:y" bp->x (); // prints "B:x" bp->y (); // prints "B:y" ap.x (); // prints "A:x" ap.y (); // prints "B:y" return 0; Only matter with pointer or reference Calls on object itself resolved statically E.g., b.y(); Look first at pointer/reference type If non-virtual there, resolve statically E.g., ap->x(); If virtual there, resolve dynamically E.g., ap->y(); Note that virtual keyword need not be repeated in derived classes But it’s good style to do so Caller can force static resolution of a virtual function via scope operator E.g., ap->A::y(); prints “A::y”

Pure Virtual Functions class A { public: virtual void x() = 0; virtual void y() = 0; }; class B : public A { virtual void x(); class C : public B { virtual void y(); int main () { A * ap = new C; ap->x (); ap->y (); delete ap; return 0; A is an Abstract Base Class Similar to an interface in Java Declares pure virtual functions (=0) Derived classes override pure virtual methods B overrides x(), C overrides y() Can’t instantiate class with declared or inherited pure virtual functions A and B are abstract, can create a C Can still have a pointer to an abstract class type Useful for polymorphism

Notes on virtual If a method is declared virtual in a class, … it is automatically virtual in all derived classes It is a really, really good idea to make destructors virtual! virtual ~Shape(); Reason: to invoke the correct destructor, no matter how object is accessed Polymorphism CS-2303, C-Term 2010

Notes on virtual You cannot override one virtual function with two or more ambiguous virtual functions

Notes on virtual What about this?

The “using” declaration and class members A using declaration in a definition of a class A allows you to introduce a name of a data member or member function from a base class of A into the scope of A A using declaration in a class A may name one of the following: A member of a base class of A A member of an anonymous union that is a member of a base class of A An enumerator for an enumeration type that is a member of a base class of A

Using Declaration When to use? if you want to create a set of overloaded member functions from base and derived classes If you want to change the access of a class member

What is the output?

Now, What is the output?

Another example: Using declaration class C { protected: int y; public: int z; }; class B : public C { int main() { B obj_B; obj_B.y =10; obj_B.z=8; } Using can be used to change the access Level of derived class objects on base Class members

Using: changing the access level

More examples: is this valid? class C { private: int y; public: int z; }; class B : public C { using C::y; using C::z; int main() { B obj_B; obj_B.y =10; obj_B.z=8; }

Notes on “using” You cannot restrict the access to x with a using declaration You may increase the access of the following members: A member inherited as private. (You cannot increase the access of a member declared as private because a using declaration must have access to the member's name.) A member either inherited or declared as protected

Why is there the “PRIVATE” inheritance? To reduce access to base class members, and functionalities from the child class To simulate composition (inclusion)

Composition the "Car has-a Engine" relationship can be expressed using simple composition

Inclusion The "Car has-a Engine" relationship can also be expressed using private inheritance:

Inclusion The "Car has-a Engine" relationship can also be expressed using private inheritance:

Similarities There are several similarities between these two variants: In both cases there is exactly one Engine member object contained in every Car object In neither case can users (outsiders) convert a Car* to an Engine* In both cases the Car class has a start() method that calls the start() method on the contained Engine object.

Differences There are also several distinctions: The simple-composition variant is needed if you want to contain several Engines per Car The private-inheritance variant can introduce unnecessary multiple inheritance The private-inheritance variant allows members of Car to convert a Car* to an Engine* The private-inheritance variant allows access to the protected members of the base class The private-inheritance variant allows Car to override Engine's virtual functions The private-inheritance variant makes it slightly simpler (20 characters compared to 28 characters) to give Car a start() method that simply calls through to the Engine's start() method

Which should I prefer? Use composition when you can, private inheritance when you have to!!! Normally you don't want to have access to the internals of too many other classes: using private inheritance increases the risk of error

When to use inclusion? You need to modify functionalities for the composed class

When to use inclusion? A legitimate, long-term use for private inheritance: you want to build a class Fred that uses code in a class Wilma the code from class Wilma needs to invoke member functions from your new class, Fred In this case, Fred calls non-virtuals in Wilma, and Wilma calls (usually pure virtuals) in itself, which are overridden by Fred

Why is there the protected inheritance? protected inheritance allows derived classes of derived classes to know about the inheritance relationship Your grand kids are effectively exposed to your implementation details Benefits: it allows derived classes of the protected derived class to exploit the relationship to the protected base class Costs: the protected derived class can't change the relationship without potentially breaking further derived classes