Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
81 views

Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23

The document discusses object-oriented programming concepts like inheritance, encapsulation, and polymorphism. It shows how to create a Date class and SpecialDate derived class, and modify access specifiers to protected. Examples demonstrate how base class pointers can point to derived class objects while only accessing members defined in the base class.

Uploaded by

Sameer Hane
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 23

The document discusses object-oriented programming concepts like inheritance, encapsulation, and polymorphism. It shows how to create a Date class and SpecialDate derived class, and modify access specifiers to protected. Examples demonstrate how base class pointers can point to derived class objects while only accessing members defined in the base class.

Uploaded by

Sameer Hane
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Object-Oriented Programming (OOP) Lecture No.

23

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);

Date Class
... private: bool IsLeapYear(); };

int main(){ Date aDate; aDate.IsLeapYear(); //Error return 0; }

Creating SpecialDate Class


Date Special Date

Special Date

AddSpecialYear ...

Creating SpecialDate Class


class SpecialDate: public Date{ public: void AddSpecialYear(int i){ ... if(day == 29 && month == 2 && !IsLeapyear(year+i)){ //ERROR! ... } } };

Modify Access Specifier


We can modify access specifier IsLeapYear from private to public

Modified Date Class


class Date{ public: ... bool IsLeapYear(); };

Modified AddSpecialYear
void SpecialDate :: AddSpecialYear (int i){ ... if(day == 29 && month == 2 && !IsLeapyear(year+i)){ ... } }

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

Modified Date Class


class Date{ protected: bool IsLeapYear(); }; int main(){ Date aDate; aDate.IsLeapYear(); //Error return 0; }

Modified AddSpecialYear
void SpecialDate :: AddSpecialYear (int i){ ... if(day == 29 && month == 2 && !IsLeapyear(year+i)){ ... } }

Disadvantages
Breaks encapsulation
The protected member is part of base classs implementation as well as derived classs implementation

IS A Relationship
Public inheritance models the IS A relationship Derived object IS A kind of base object

class Person { char * name; public: ... const char * GetName(); }; class Student: public Person{ int rollNo; public: ... int GetRollNo(); };

Example

Example
int main() { Student sobj; cout << sobj.GetName(); cout << sobj.GetRollNo(); return 0; }

IS A Relationship
The base class pointer can point towards an object of derived class

Example
int main(){ Person * pPtr = 0; Student s; pPtr = &s; cout << pPtr->GetName(); return 0; }

Example
pPtr = &s;

s
pPtr
base member1 base member2 ... derived member1 derived member2 ...

Example
int main(){ Person * pPtr = 0; Student s; pPtr = &s; //Error cout << pPtr->GetRollNo(); return 0; }

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

Member Access
The access to members is determined by static type The static type of pPtr is Person Following call is erroneous pPtr->GetRollNo();

IS A Relationship
We can use a reference of derived object where the reference of base object is required

Example
int main(){ Person p; Student s; Person & refp = s; cout << refp.GetName(); cout << refp.GetRollNo(); //Error return 0; }

Example
void Play(const Person& p){ cout << p.GetName() << is playing; } void Study(const Student& s){ cout << s.GetRollNo() << is Studying; }

Example
int main(){ Person p; Student s; Play(p); Play(s); return 0; }

You might also like