CSC241 Object-Oriented Programming (OOP) Lecture No. 19.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
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.
I NHERITANCE Chapter 7 Department of CSE, BUET 1.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Object-Oriented Programming in C++ Lecture 7 Polymorphism.
Copyright 2006 Oxford Consulting, Ltd1 February C++ Inheritance Data Abstraction and Abstract Data Types Abstract Data Type Encapsulated data.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Object-Oriented Programming (OOP) Lecture No. 5 Downloaded From:
Inheritance and Polymorphism CS351 – Programming Paradigms.
1 CSE 303 Lecture 24 Inheritance in C++, continued slides created by Marty Stepp
OBJECT ORIENTED PROGRAMMING
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Mastering Object-Oriented Analysis and Design with UML Appendix: UML to C++ Mapping Mastering Object-Oriented Analysis and Design with UML Appendix: UML.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Object-Oriented Programming (OOP) Lecture No. 5. Multiple Inheritance ► We may want to reuse characteristics of more than one parent class.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Inheritance and Composition Reusing the code and functionality Unit - 04.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
CSCI 171 Presentation 15 Introduction to Object–Oriented Programming (OOP) in C++
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
C# Interfaces and RTTI CNS 3260 C#.NET Software Development.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
CSC241 Object-Oriented Programming (OOP) Lecture No. 22.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
 2006 Pearson Education, Inc. All rights reserved Templates.
The Slicing Problem CS240 Computer Science II. Some relationship between base and derived classes: data members Derived class inherits data members of.
Design issues for Object-Oriented Languages
CS 2304: Multiple Inheritance
Lecture 12 Inheritance.
Inheritance CMSC 202, Version 4/02.
Object-Oriented Programming (OOP) Lecture No. 45
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Contents Introduction to Constructor Characteristics of Constructor
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
By Muhammad Waris Zargar
Inheritance: Polymorphism and Virtual Functions
CISC/CMPE320 - Prof. McLeod
Lecture 10 Concepts of Programming Languages
COP 3330 Object-oriented Programming in C++
Object-Oriented Programming (OOP) Lecture No. 23
Object-Oriented Programming (OOP) Lecture No. 27
Inheritance in C++ Inheritance Protected Section
Computer Science II for Majors
Presentation transcript:

CSC241 Object-Oriented Programming (OOP) Lecture No. 19

Multiple Inheritance  A class can inherit from more then one class

Multiple Inheritance Transmitter... Transmit() Receiver... Receive() Phone

Example class Phone : public Transmitter, public Receiver {... };

Multiple Inheritance  Derived class can inherit from public base class as well as private and protected base classes class Mermaid : private Woman, private Fish

Multiple Inheritance  The derived class inherits data members and functions form all the base classes  Object of derived class can perform all the tasks that an object of base class can perform

Example int main(){ Phone obj; obj.Transmit(); obj.Receive(); return 0; }

Multiple Inheritance  When using public multiple inheritance, the object of derived class can replace the objects of all the base classes

Example int main(){ Phone obj; Transmitter * tPtr = &obj; Receiver * rPtr = &obj; return 0; }

Multiple Inheritance  The pointer of one base class cannot be used to call the function of another base class  The functions are called based on static type

Example int main(){ Phone obj; Transmitter * tPtr = &obj; tPtr->Transmit(); tPtr->Receive(); //Error return 0; }

Example int main(){ Phone obj; Receiver * rPtr = &obj; rPtr->Receive(); rPtr->Transmit(); //Error return 0; }

Multiple Inheritance  If more than one base class have a function with same signature then the child will have two copies of that function  Calling such function will result in ambiguity

Multiple Inheritance Amphibious Vehicle Land VehicleWater Vehicle CarBoat

Example class LandVehicle { public: int GetMaxLoad(); }; class WaterVehicle { public: int GetMaxLoad(); };

Example class AmphibiousVehicle : public LandVehicle, public WaterVehicle{ }; int main(){ AmphibiousVehicle obj; obj.GetMaxLoad(); // Error return 0; }

Multiple Inheritance  Programmer must explicitly specify the class name when calling ambiguous function

Example int main(){ AmphibiousVehicle obj; obj.LandVehicle::GetMaxLoad(); obj.WaterVehicle::GetMaxLoad(); return 0; }

Multiple Inheritance  The ambiguous call problem can arise when dealing with multiple level of multiple inheritance

Multiple Inheritance Amphibious Vehicle Land VehicleWater Vehicle Vehicle CarBoat

Example class Vehicle { public: int GetMaxLoad(); }; class LandVehicle : public Vehicle{ }; class WaterVehicle : public Vehicle{ };

Example class AmphibiousVehicle : public LandVehicle, public WaterVehicle{ }; int main(){ AmphibiousVehicle obj; obj.GetMaxLoad(); // Error return 0; }

Example int main() { AmphibiousVehicle obj; obj.Vehicle::GetMaxLoad(); //Error return 0; } Vehicle is accessible through two paths

Multiple Inheritance Amphibious Vehicle Land VehicleWater Vehicle Vehicle CarBoat Vehicle

Example int main() { AmphibiousVehicle obj; obj.LandVehicle::GetMaxLoad(); obj.WaterVehicle::GetMaxLoad(); return 0; }

Multiple Inheritance  Data member must be used with care when dealing with more then one level on inheritance

Example class Vehicle { protected: int weight; }; class LandVehicle : public Vehicle{ }; class WaterVehicle : public Vehicle{ };

Example class AmphibiousVehicle : public LandVehicle, public WaterVehicle{ public: AmphibiousVehicle(){ LandVehicle::weight = 10; WaterVehicle::weight = 10; } };  There are multiple copies of data member weight

Memory View Data Members of Vehicle Data Members of LandVehicle Data Members of AmphibiousVehicle Data Members of Vehicle Data Members of WaterVehicle

Virtual Inheritance  In virtual inheritance there is exactly one copy of the anonymous base class object

Example class Vehicle { protected: int weight; }; class LandVehicle : public virtual Vehicle{ }; class WaterVehicle : public virtual Vehicle{ };

Example class AmphibiousVehicle : public LandVehicle, public WaterVehicle { public: AmphibiousVehicle(){ weight = 10; } };

Memory View Data Members of Vehicle Data Members of LandVehicle Data Members of AmphibiousVehicle Data Members of WaterVehicle

Virtual Inheritance  Virtual inheritance must be used when necessary  There are situation when programmer would want to use two distinct data members inherited from base class rather then one

Example Student BS StudentMS StudentPhD Student MS/PhD Student GPA