Object-Oriented Programming (OOP) Lecture No. 23

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
1 CSC241: Object Oriented Programming Lecture No 21.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Copyright  Hannu Laine C++-programming Part 7 Hannu Laine Static members Different uses of const specifier.
EC-241 Object-Oriented Programming
Web Application Development Slides Credit Umair Javed LUMS.
C++ Classes & Data Abstraction
Win32 Programming Lesson 4: Classes and Structures.
OOP Etgar 2008 – Recitation 61 Object Oriented Programming Etgar 2008 Recitation 6.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Inline Functions, Operator Overloading and Inheritance COSC 102: Programming in C++
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
Object Oriented Programming (OOP) Lecture No. 11.
Object Oriented Programming (OOP) Lecture No. 10.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 7.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Object-Oriented Programming (OOP) Lecture No. 24.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Classes (Part 1) Lecture 3
Object-Oriented Programming (OOP) Lecture No. 15
Inheritance CMSC 202, Version 4/02.
Objects as a programming concept
Object-Oriented Programming (OOP) Lecture No. 45
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
Review: Two Programming Paradigms
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Object-Oriented Programming (OOP) Lecture No. 32
Object Oriented Analysis and Design
Object-Oriented Programming (OOP) Lecture No. 36
Object-Oriented Programming (OOP) Lecture No. 39
Object Oriented Programming (OOP) Lecture No. 9
Programming -2 برمجة -2 المحاضرة-7 Lecture-7.
Object-Oriented Programming (OOP) Lecture No. 40
Object-Oriented Programming (OOP) Lecture No. 25
Object Oriented Programming (OOP) Lecture No. 13
Introduction of Programming
Lecture 16 Oct 30, 02.
Object-Oriented Programming (OOP) Lecture No. 22
Object Oriented Programming
Object-Oriented Programming (OOP) Lecture No. 38
Object oriented programming (OOP) Lecture No. 7
Introduction to Programming
By Rajanikanth B OOP Concepts By Rajanikanth B
Web Design & Development Lecture 4
Object Oriented Programming (OOP) Lecture No. 11
Object-Oriented Programming (OOP) Lecture No. 27
How Dynamic Memory Works with Memory Diagram
Inheritance in C++ Inheritance Protected Section
Object Oriented Programming (OOP) Lecture No. 12
Object Oriented Programming (OOP) Lecture No. 10
CSG2H3 Object Oriented Programming
Presentation transcript:

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 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 class’s implementation as well as derived class’s implementation

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

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

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 ... base member1 base member2 s pPtr

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