Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 25
Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 25
25
Overriding
Parent ... Func1
Overriding
class Parent { public: void Func1(); void Func1(int); }; class Child: public Parent { public: void Func1(); };
Overriding
class Parent { public: void Func1(); void Func1(); };
//Error
Example
class Person{ public: void Walk(); }; class ParalyzedPerson: public Person{ public: void Walk(); };
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
Example
class Student : public Person{ char * major; public: Student(char * aName, char* m);
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
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
Name: Ahmed
Hierarchy of Inheritance
We represent the classes involved in inheritance relation in tree like hierarchy
Example
GrandParent
Parent1
Parent2
Child1
Child2