Object-Oriented Programming (OOP) Lecture No. 27

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

Introduction to Java 2 Programming
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.
EC-241 Object-Oriented Programming
INHERITANCE BASICS Reusability is achieved by INHERITANCE
C++ Classes & Data Abstraction
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues.
OOP Etgar 2008 – Recitation 61 Object Oriented Programming Etgar 2008 Recitation 6.
C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
 By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
CSC241 Object-Oriented Programming (OOP) Lecture No. 19.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
Chapter 11 More about classes and OOP. Relationships Between Classes Possible relationships – Access ("uses-a") – Ownership/Composition ("has-a") – Inheritance.
Object Oriented Programming in C++ Chapter 6 Inheritance.
Object Oriented Programming (OOP) Lecture No. 10.
Inheritance ITK 169 Fall 2003 Base Class Also called the Parent Class This is the class that later classes will be build on. Suppose the Red Bird Rec.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Order of Constructor Call. Base class constructors are always called in the derived class constructors. Whenever you create derived class object, first.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Inheritance and Composition Reusing the code and functionality Unit - 04.
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
CITA 342 Section 1 Object Oriented Programming (OOP)
1 CSC241: Object Oriented Programming Lecture No 17.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Yan Shi CS/SE 2630 Lecture Notes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming (OOP) Lecture No. 4
Modern Programming Tools And Techniques-I
Object-Oriented Programming (OOP) Lecture No. 15
Static data members Constructors and Destructors
By Muhammad Waris Zargar
7. Inheritance and Polymorphism
Object-Oriented Programming (OOP) Lecture No. 45
Final and Abstract Classes
Inheritance II CMSC 202.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object Oriented Analysis and Design
Understanding Inheritance
Object-Oriented Programming (OOP) Lecture No. 36
Object-Oriented Programming (OOP) Lecture No. 39
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Object-Oriented Programming (OOP) Lecture No. 25
Inheritance: Polymorphism and Virtual Functions
Object-Oriented Programming (OOP) Lecture No. 22
Object-Oriented Programming (OOP) Lecture No. 38
Eighth step for Learning C++ Programming
C++ Programming CLASS This pointer Static Class Friend Class
Final and Abstract Classes
Object-Oriented Programming (OOP) Lecture No. 23
ENERGY 211 / CME 211 Lecture 22 November 10, 2008.
C++ data types.
CMSC 202 Lesson 17 Inheritance II.
Presentation transcript:

Object-Oriented Programming (OOP) Lecture No. 27

Class Collection class Collection { ... public: void AddElement(int); bool SearchElement(int); bool SearchElementAgain(int); bool DeleteElement(int); };

Class Set class Set: private Collection { private: ... public: void AddMember(int); bool IsMember(int); bool DeleteMember(int); };

Specialization (Restriction) the derived class is behaviourally incompatible with the base class Behaviourally incompatible means that base class can’t always be replaced by the derived class

Specialization (Restriction) Specialization (Restriction) can be implemented using private and protected inheritance

Example – Specialization (Restriction) Person age : [0..125] age = a setAge( a ) If age < 18 then error else age = a Adult age : [18..125] setAge( a )

Example class Person{ … protected: int age; public: bool SetAge(int _age){ if (_age >=0 && _age <= 125) { age = _age; return true; } return false; };

Example class Adult : private Person { public: bool SetAge(int _age){ if (_age >=18 && _age <= 125) { age = _age; return true; } return false; };

Private Inheritance Only member functions and friend functions of a derived class can convert pointer or reference of derived object to that of parent object

Example class Parent{ }; class Child : private Parent{ int main(){ Child cobj; Parent *pptr = & cobj; //Error return 0; }

Example void DoSomething(const Parent &); Child::Child(){ Parent & pPtr = static_cast<Parent &>(*this); DoSomething(pPtr); // DoSomething(*this); }

Private Inheritance The child class object has an anonymous object of parent class object The default constructor and copy constructor of parent class are called when needed

Example class Parent{ public: Parent(){ cout << “Parent Constructor”; } Parent(const Parent & prhs){ cout << “Parent Copy Constructor”; };

Example class Child: private Parent{ public: Child(){ cout << “Child Constructor”; } Child(const Child & crhs) :Parent(crhs){ cout << “Child Copy Constructor”; };

Example int main() { Child cobj1; Child cobj2 = cobj1; return 0; }

Example Output: Parent Constructor Child Constructor Parent Copy Constructor Child Copy Constructor

Private Inheritance The base class that is more then one level down the hierarchy cannot access the member function of parent class, if we are using private inheritance

Class Hierarchy class GrandParent{ public : void DoSomething(); }; class Parent: private GrandParent{ void SomeFunction(){ DoSomething(); }

Example class Child: private Parent { public: Child() { DoSomething(); //Error } };

Private Inheritance The base class that is more then one level down the hierarchy cannot convert the pointer or reference to child object to that of parent, if we are using private inheritance

Class Hierarchy void DoSomething(GrandParent&); class GrandParent{ }; class Parent: private GrandParent{ public: Parent() {DoSomething(*this);}

Example class Child: private Parent { public: Child() { DoSomething(*this); //Error } };

Protected Inheritance Use protected inheritance if you want to build class hierarchy using “implemented in terms of”

Protected Inheritance If B is a protected base and D is derived class then public and protected members of B can be used by member functions and friends of classes derived from D

Class Hierarchy class GrandParent{ public : void DoSomething(); }; class Parent: protected GrandParent{ void SomeFunction(){ DoSomething(); }

Example class Child: protected Parent { public: Child() DoSomething(); } };

Protected Inheritance If B is a protected base and D is derived class then only friends and members of D and friends and members of class derived from D can convert D* to B* or D& to B&

Class Hierarchy void DoSomething(GrandParent&); class GrandParent{ }; class Parent: protected GrandParent{

Example class Child: protected Parent { public: Child() { DoSomething(*this); } };