Object-Oriented Programming (OOP) Lecture No. 25

Slides:



Advertisements
Similar presentations
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Chapter 1 Inheritance University Of Ha’il.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
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.
Introduction to Data Structures, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Object Oriented Programming Concepts.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
Inheritance, Polymorphism, and Virtual Functions
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Inheritence ( הורשה Inheritence ( הורשה ( השקפים מבוססים על שקפים של אליהו ברוטמן ומרינה.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
CMSC 202 Lesson 19 Polymorphism 2. Warmup What is wrong with the following code? What error will it produce? (Hint: it already compiles) for (unsigned.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Chapter 10 Inheritance and Polymorphism
Object Oriented Programming (OOP) Lecture No. 11.
Object Oriented Programming (OOP) Lecture No. 10.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
Overview of C++ Polymorphism
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
1 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 8 1.
 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.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
Object-Oriented Programming (OOP) Lecture No. 24.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Chapter 2 Objects and Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Object-Oriented Programming (OOP) Lecture No. 15
Object Oriented Programming (OOP) Lecture No. 8
Object-Oriented Programming (OOP) Lecture No. 45
Introduction to Programming
Inheritance, Polymorphism, and Virtual Functions
Inheritance: hiding methods
Inheritance & Polymorphism
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance, Polymorphism, and Virtual Functions
Overview of C++ Overloading
Object-Oriented Programming (OOP) Lecture No. 22
CPS120: Introduction to Computer Science
Introduction to Programming
Overview of C++ Polymorphism
Polymorphism 2 CMSC 202.
Eighth step for Learning C++ Programming
C++ Programming CLASS This pointer Static Class Friend Class
CPS120: Introduction to Computer Science
Object Oriented Programming (OOP) Lecture No. 11
Object-Oriented Programming (OOP) Lecture No. 23
CS410 – Software Engineering Lecture #8: Advanced C++ III
Object-Oriented Programming (OOP) Lecture No. 27
Lecture 6: Polymorphism
Chapter 7 Inheritance.
Computer Science II for Majors
Inheritance and Polymorphism
Presentation transcript:

Object-Oriented Programming (OOP) Lecture No. 25

Overriding Member Functions of Base Class Derived class can override the member functions of its base class To override a function the derived class simply provides a function with the same signature as that of its base class

Overriding Parent ... Func1 Child ... Func1

Overriding class Parent { public: void Func1(); void Func1(int); }; class Child: public Parent { void Func1();

Overloading vs. Overriding Overloading is done within the scope of one class Overriding is done in scope of parent and child Overriding within the scope of single class is error due to duplicate declaration

Overriding class Parent { public: void Func1(); void Func1(); //Error };

Overriding Member Functions of Base Class Derive class can override member function of base class such that the working of function is totally changed

Example class Person{ public: void Walk(); }; class ParalyzedPerson: public Person{

Overriding Member Functions of Base Class Derive class can override member function of base class such that the working of function is similar to former implementation

Example class Person{ char *name; public: Person(char *=NULL); const char *GetName() const; void Print(){ cout << “Name: ” << name << endl; } };

Example class Student : public Person{ char * major; public: Student(char * aName, char* aMajor); void Print(){ cout <<“Name: ”<< GetName()<<endl << “Major:” << major<< endl; } ... };

Example int main(){ Student a(“Ahmad”, “Computer Science”); a.Print(); return 0; }

Output Output: Name: Ahmed Major: Computer Science

Overriding Member Functions of Base Class Derive class can override member function of base class such that the working of function is based on former implementation

Example class Student : public Person{ char * major; public: Student(char * aName, char* m); void Print(){ Print();//Print of Person cout<<“Major:” << major <<endl; } ... };

Example int main(){ Student a(“Ahmad”, “Computer Science”); a.Print(); return 0; }

Output There will be no output as the compiler will call the print of the child class from print of child class recursively There is no ending condition

Example class Student : public Person{ char * major; public: Student(char * aName, char* m); void Print(){ Person::Print(); cout<<“Major:” << major <<endl; } ... };

Example int main(){ Student a(“Ahmad”, “Computer Science”); a.Print(); return 0; }

Output Output: Name: Ahmed Major: Computer Science

Overriding Member Functions of Base Class The pointer must be used with care when working with overridden member functions

Example int main(){ Student a(“Ahmad”, “Computer Scuence”); Student *sPtr = &a; sPtr->Print(); Person *pPtr = sPtr; pPtr->Print(); return 0; }

Example Output: Name: Ahmed Major: Computer Science

Overriding Member Functions of Base Class The member function is called according to static type The static type of pPtr is Person The static type of sPtr is Student

Hierarchy of Inheritance We represent the classes involved in inheritance relation in tree like hierarchy

Example GrandParent Parent1 Parent2 Child2 Child1

Direct Base Class A direct base class is explicitly listed in a derived class's header with a colon (:) class Child1:public Parent1 ...

Indirect Base Class An indirect base class is not explicitly listed in a derived class's header with a colon (:) It is inherited from two or more levels up the hierarchy of inheritance class GrandParent{}; class Parent1: public GrandParent {}; class Child1:public Parent1{};