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