object oriented programming (oop) - cs304 power point slides lecture 23
TRANSCRIPT
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 1/25
Object-Oriented Programming
(OOP)Lecture No. 23
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 2/25
Date Class
class 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);
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 3/25
Date Class...
private:
bool IsLeapYear();
};
int main(){
Date aDate;
aDate.IsLeapYear(); //Errorreturn 0;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 4/25
Creating SpecialDate Class
Special Date
Date Special Date
AddSpecialYear
...
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 5/25
Creating SpecialDate Classclass SpecialDate: public Date{
…
public:
void AddSpecialYear(int i){
...if(day == 29 && month == 2
&& !IsLeapyear(year+i)){ //ERROR!
...
}}
};
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 6/25
Modify Access Specifier
• We can modify access specifier“IsLeapYear” from private to
public
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 7/25
Modified Date Class
class Date{
public:
...bool IsLeapYear();
};
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 8/25
Modified AddSpecialYear
void SpecialDate :: AddSpecialYear(int i){
...
if(day == 29 && month == 2
&& !IsLeapyear(year+i)){
...}
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 9/25
Protected members
• Protected members can not beaccessed outside the class
• Protected members of base class
become protected member of
derived class in Public inheritance
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 10/25
Modified Date Classclass Date{
…
protected:
bool IsLeapYear();
};
int main(){
Date aDate;
aDate.IsLeapYear(); //Errorreturn 0;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 11/25
Modified AddSpecialYear
void SpecialDate :: AddSpecialYear(int i){
...
if(day == 29 && month == 2
&& !IsLeapyear(year+i)){
...}
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 12/25
Disadvantages
• Breaks encapsulation
–The protected member is part of
base class’s implementation as well
as derived class’s implementation
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 13/25
“IS A” Relationship
• Public inheritance models the “IS
A” relationship
• Derived object IS A kind of baseobject
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 14/25
Exampleclass Person {
char * name;public: ...
const char * GetName();
};class Student: public Person{
int rollNo;
public: ...int GetRollNo();
};
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 15/25
Exampleint main()
{
Student sobj;
cout << sobj.GetName();
cout << sobj.GetRollNo();
return 0;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 16/25
“IS A” Relationship
• The base class pointer can pointtowards an object of derived class
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 17/25
int main(){Person * pPtr = 0;
Student s;
pPtr = &s;
cout << pPtr-> GetName();
return 0;
}
Example
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 18/25
Example
pPtr = &s;
derived member1
derived member2
...
base member1
base member2...
s
pPtr
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 19/25
int main(){Person * pPtr = 0;
Student s;
pPtr = &s;
//Error
cout << pPtr->GetRollNo();return 0;
}
Example
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 20/25
Static Type
• The type that is used to declare areference or pointer is called its
static type
–The static type of pPtr is Person
–The static type of s is Student
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 21/25
Member Access
• The access to members isdetermined by static type
• The static type of pPtr is Person
• Following call is erroneous
pPtr->GetRollNo();
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 22/25
“IS A” Relationship
• We can use a reference ofderived object where the
reference of base object is
required
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 23/25
Example
int main(){Person p;
Student s;
Person & refp = s;
cout << refp.GetName();
cout << refp.GetRollNo(); //Errorreturn 0;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 24/25
Examplevoid Play(const Person& p){
cout << p.GetName()
<< “ is playing”;
}void Study(const Student& s){
cout << s.GetRollNo()
<< “ is Studying”;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-23 25/25
Example
int main(){Person p;
Student s;
Play(p);
Play(s);
return 0;
}