HYBRID INHERITANCE : AMBIGUITY REMOVAL

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Object Oriented Programming
EC-241 Object-Oriented Programming
Inheritance Inheritance-2. Inheritance Rewriting point and circle classes class Point { protected: int x,y; public: Point(int,int); void display(void);
CS 222 Object Oriented Programming Using C++ Inheritance.
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
Inheritance, Polymorphism, and Virtual Functions
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
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.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
INHERITANCE IN C++ 3 CSC1201: PROGRAMMING LANGUAGE 2 ASEEL ALHADLAQ_KSU 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
C++ Lecture 7 Thursday, 21 July Chapter 9 Inheritance l Creating new classes from the existing classes l The notions of base classes and derived.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
OOP, Virtual Functions and Inheritance
CIS 3301 C# Lesson 7 Introduction to Classes. CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 16.
Chapter 10 Inheritance and Polymorphism
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Inheritance in C++ 2 CSC1201: Programming Language 2 1Aseel AlHadlaq_KSU.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
CSC 143 O 1 CSC 143 Inheritance and Object Oriented Design.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
OOP, Virtual Functions and Inheritance
CSC241: Object Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Polymorphism & Virtual Functions
This technique is Called “Divide and Conquer”.
Control structures Chapter 3.
Polymorphism Lec
Object-Oriented Programming (OOP) Lecture No. 28
CS1201: Programming Language 2
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Virtual Base Classes By:Nouf Aljaffan Edited by : Nouf Almunyif.
Inheritance: Polymorphism and Virtual Functions
Programming II Polymorphism A.AlOsaimi.
By Muhammad Waris Zargar
Inheritance: Polymorphism and Virtual Functions
Inheritance:Concept of Re-usability
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Inheriting Multiple Base Classes
Inheritance: Polymorphism and Virtual Functions
Inheritance in C++ Inheritance Protected Section
C++ Object Oriented 1.
Lecture 6: Polymorphism
Presentation transcript:

HYBRID INHERITANCE : AMBIGUITY REMOVAL OOMP Remedy…..? MANUAL SELECTION : Scope resolution operator virtual base class

MANUAL SELECTION This is done by using Scope resolution Operator: OOP MANUAL SELECTION This is done by using Scope resolution Operator: int main ( ) { derivedclass3 d3; d3.derivedclass1:: a = 10; d3.derivedclass2:: a = 20; cout<< d3.derivedclass1:: a<<“\n”; cout<< d3.derivedclass2:: a<<“\n”; return 0; } OUTPUT : baseclass1 derived1 derived2 derived3 10 20

For this use VIRTUAL BASE CLASS OOP d3.derivedclass1 :: a and d3.derivedclass2::a In the above statements , use of scope resolution operator resolves the problem of ambiguity But it is not efficient way, How to make only one copy of ‘a’ available in object ‘d3’ which consumes less memory and easy to access without :: operator For this use VIRTUAL BASE CLASS

OOP Virtual Base Classes Used to prevent multiple copies of the base class from being present in an object derived from those objects by declaring the base class as virtual when it is inherited. Syntax: class derived : virtual public base { . . . };

EXAMPLE : VIRTUAL BASE CLASS OOP EXAMPLE : VIRTUAL BASE CLASS class baseclass1 {     public:        int a;                   baseclass1() cout<<“baseclass1\n”;                } };

EXAMPLE :VIRTUAL BASE CLASS OOP EXAMPLE :VIRTUAL BASE CLASS class derivedclass1 : virtual public baseclass { public: int a1; derivedclass1() cout<<“derived1\n”; } }; class derivedclass2 : virtual public baseclass {     public:        int a2;                   derivedclass2() cout<<“derived2\n”;                } };

EXAMPLE :VIRTUAL BASE CLASS OOP EXAMPLE :VIRTUAL BASE CLASS class derivedclass3 : public derivedclass1, derivedclass2 {    private:        int a3;                       public: derivedclass3() a3 = 6; cout<<“derived2\n”;                } }; int main ( ) derivedclass3 d3; return 0; } OUTPUT : Baseclass1 derivedclass1 derivedclass2 derivedclass3 Only one copy maintained , no ambiguity

Motivation: Virtual Functions OOP Motivation: Virtual Functions a hierarchy of geometric shape classes draws circles, ellipses, rectangles etc just use method draw throughout the hierarchy Line draw()‏ Rectangle Circle draw()‏ draw()‏ Square Ellipse draw()‏ draw()‏

Pointers to Derived Classes OOP Pointers to Derived Classes C++ allows base class pointers to point to derived class objects. Let we have class base { … }; class derived : public base { … }; Then we can write: base *p1; derived d_obj; p1 = &d_obj;

class Base { public: void show() { cout << “base\n”; } }; class Derv1 : public base { cout << “derived1\n”; class Derv2 : public base { cout << “derived2\n”; void main() { Derv1 dv1; Derv2 dv2; Base *ptr ptr = &dv1; ptr->show(); ptr = &dv2; ptr ->show(); return 0; } OUTPUT : base

Non-Virtual Pointer Access OOP

OUTPUT : derived1 derived2 class Base { public: virtual void show() { cout << “base\n”; } }; class Derv1 : public base { void show() { cout << “derived1\n”; class Derv2 : public base { cout << “derived2\n”; void main() { Derv1 dv1; Derv2 dv2; Base *ptr ptr = &dv1; ptr->show(); ptr = &dv2; ptr ->show(); return 0; } OUTPUT : derived1 derived2