Download - Oop + lecture23
![Page 1: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/1.jpg)
Object-Oriented Programming (OOP)
Lecture No. 23
![Page 2: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/2.jpg)
Date Classclass Date{
int day, month, year;static Date defaultDate;
public:void SetDay(int aDay);int GetDay() const;void AddDay(int x);… static void SetDefaultDate(
int aDay,int aMonth, int aYear);
![Page 3: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/3.jpg)
Date Class...
private:bool IsLeapYear();
};
int main(){Date aDate;aDate.IsLeapYear(); //Errorreturn 0;
}
![Page 4: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/4.jpg)
Creating SpecialDate Class
Special Date
Date Special Date
AddSpecialYear...
![Page 5: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/5.jpg)
Creating SpecialDate Classclass SpecialDate: public Date{
…public:
void AddSpecialYear(int i){...if(day == 29 && month == 2&& !IsLeapyear(year+i)){ //ERROR!
...}
}};
![Page 6: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/6.jpg)
Modify Access Specifier• We can modify access specifier
“IsLeapYear” from private to public
![Page 7: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/7.jpg)
Modified Date Class
class Date{
public:
...
bool IsLeapYear();
};
![Page 8: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/8.jpg)
Modified AddSpecialYearvoid SpecialDate :: AddSpecialYear
(int i){
...
if(day == 29 && month == 2
&& !IsLeapyear(year+i)){
...
}
}
![Page 9: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/9.jpg)
Protected members• Protected members can not be
accessed outside the class
• Protected members of base class become protected member of derived class in Public inheritance
![Page 10: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/10.jpg)
Modified Date Classclass Date{
…protected:
bool IsLeapYear();};
int main(){Date aDate;aDate.IsLeapYear(); //Errorreturn 0;
}
![Page 11: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/11.jpg)
Modified AddSpecialYearvoid SpecialDate :: AddSpecialYear
(int i){
...
if(day == 29 && month == 2
&& !IsLeapyear(year+i)){
...
}
}
![Page 12: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/12.jpg)
Disadvantages• Breaks encapsulation
–The protected member is part of base class’s implementation as well as derived class’s implementation
![Page 13: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/13.jpg)
“IS A” Relationship
• Public inheritance models the “IS A” relationship
• Derived object IS A kind of base object
![Page 14: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/14.jpg)
Exampleclass Person {
char * name;public: ...
const char * GetName();};class Student: public Person{
int rollNo;public: ...
int GetRollNo();};
![Page 15: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/15.jpg)
Exampleint main()
{
Student sobj;
cout << sobj.GetName();
cout << sobj.GetRollNo();
return 0;
}
![Page 16: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/16.jpg)
“IS A” Relationship
• The base class pointer can point towards an object of derived class
![Page 17: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/17.jpg)
int main(){
Person * pPtr = 0;
Student s;
pPtr = &s;
cout << pPtr->GetName();
return 0;
}
Example
![Page 18: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/18.jpg)
ExamplepPtr = &s;
derived member1derived member2
...
base member1base member2
...
spPtr
![Page 19: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/19.jpg)
int main(){
Person * pPtr = 0;
Student s;
pPtr = &s;
//Error
cout << pPtr->GetRollNo();
return 0;
}
Example
![Page 20: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/20.jpg)
Static Type• The type that is used to declare a
reference or pointer is called its static type–The static type of pPtr is Person
–The static type of s is Student
![Page 21: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/21.jpg)
Member Access• The access to members is
determined by static type
• The static type of pPtr is Person
• Following call is erroneous
pPtr->GetRollNo();
![Page 22: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/22.jpg)
“IS A” Relationship• We can use a reference of
derived object where the reference of base object is required
![Page 23: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/23.jpg)
Exampleint main(){
Person p;
Student s;
Person & refp = s;
cout << refp.GetName();
cout << refp.GetRollNo(); //Error
return 0;
}
![Page 24: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/24.jpg)
Examplevoid Play(const Person& p){
cout << p.GetName() << “ is playing”;
}void Study(const Student& s){
cout << s.GetRollNo() << “ is Studying”;
}
![Page 25: Oop + lecture23](https://reader035.vdocument.in/reader035/viewer/2022081422/5593676a1a28ab9a2d8b466f/html5/thumbnails/25.jpg)
Exampleint main(){
Person p;
Student s;
Play(p);
Play(s);
return 0;
}