Your SlideShare is downloading. ×
0
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Oop +  lecture25
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Oop + lecture25

97

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
97
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Object-Oriented Programming (OOP) Lecture No. 25
  • 2. 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
  • 3. Overriding Parent ... Func1 Child ... Func1
  • 4. Overriding class Parent { public: void Func1(); void Func1(int); }; class Child: public Parent { public: void Func1(); };
  • 5. 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
  • 6. Overriding class Parent { public: void Func1(); void Func1(); //Error };
  • 7. Overriding Member Functions of Base Class • Derive class can override member function of base class such that the working of function is totally changed
  • 8. Example class Person{ public: void Walk(); }; class ParalyzedPerson: public Person{ public: void Walk(); };
  • 9. 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
  • 10. Example class Person{ char *name; public: Person(char *=NULL); const char *GetName() const; void Print(){ cout << “Name: ” << name << endl; } };
  • 11. Example class Student : public Person{ char * major; public: Student(char * aName, char* aMajor); void Print(){ cout <<“Name: ”<< GetName()<<endl << “Major:” << major<< endl; } ... };
  • 12. Example int main(){ Student a(“Ahmad”, “Computer Science”); a.Print(); return 0; }
  • 13. Output Output: Name: Ahmed Major: Computer Science
  • 14. 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
  • 15. Example class Student : public Person{ char * major; public: Student(char * aName, char* m); void Print(){ Print();//Print of Person cout<<“Major:” << major <<endl; } ... };
  • 16. Example int main(){ Student a(“Ahmad”, “Computer Science”); a.Print(); return 0; }
  • 17. 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
  • 18. Example class Student : public Person{ char * major; public: Student(char * aName, char* m); void Print(){ Person::Print(); cout<<“Major:” << major <<endl; } ... };
  • 19. Example int main(){ Student a(“Ahmad”, “Computer Science”); a.Print(); return 0; }
  • 20. Output Output: Name: Ahmed Major: Computer Science
  • 21. Overriding Member Functions of Base Class • The pointer must be used with care when working with overridden member functions
  • 22. Example int main(){ Student a(“Ahmad”, “Computer Scuence”); Student *sPtr = &a; sPtr->Print(); Person *pPtr = sPtr; pPtr->Print(); return 0; }
  • 23. Example Output: Name: Ahmed Major: Computer Science Name: Ahmed
  • 24. 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
  • 25. Hierarchy of Inheritance • We represent the classes involved in inheritance relation in tree like hierarchy
  • 26. Example GrandParent Parent1 Parent2 Child1 Child2
  • 27. Direct Base Class • A direct base class is explicitly listed in a derived class's header with a colon (:) class Child1:public Parent1 ...
  • 28. 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{};

×