Virtual Base Classes By:Nouf Aljaffan Edited by : Nouf Almunyif.

Slides:



Advertisements
Similar presentations
Brown Bag #3 Return of the C++. Topics  Common C++ “Gotchas”  Polymorphism  Best Practices  Useful Titbits.
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Inheritance in C++ Multiple Base Classes Inheritance By Nouf Aljaffan Reference: Learn C++ from the master, Schildt, second Ed.
Inheritance in C++ Multiple Base Classes Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
INHERITANCE IN C++ 3 CSC1201: PROGRAMMING LANGUAGE 2 ASEEL ALHADLAQ_KSU 1.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Pointer Data Type and Pointer Variables II By: Nouf Aljaffan Edited by : Nouf Almunyif.
Multiple Inheritance Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Topic 1 11/18/2015. Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas.
Chapter 2 part #1 C++ Program Structure
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Inheritance in C++ 2 CSC1201: Programming Language 2 1Aseel AlHadlaq_KSU.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
Learners Support Publications Constructors and Destructors.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Motivation for Generic Programming in C++
Constructors and Destructors
C++ Template.
C ++ MULTIPLE CHOICE QUESTION
Great way to learn is by example so fire up Visual Studios C (at home make sure you custom install with C++ - no longer default) by Deborah R. Fowler.
Polymorphism &Virtual Functions
Chapter 5 Function Basics
Polymorphism & Virtual Functions
Pointers and Pointer-Based Strings
Pointer Data Type and Pointer Variables II
Polymorphism Lec
Pointer Data Type and Pointer Variables
Chapter 2 Elementary Programming
Polymorphism.
CS1201: Programming Language 2
CS1201: Programming Language 2
HYBRID INHERITANCE : AMBIGUITY REMOVAL
CS1201: Programming Language 2
Name: Rubaisha Rajpoot
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance.
Counting Loops.
Programming 1 (CS112) Dr. Mohamed Mostafa Zayed 1.
Constructors and Destructors
Programming II Polymorphism A.AlOsaimi.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
CS1201: Programming Language 2
CS1201: Programming Language 2
CS1201: Programming Language 2
Java – Inheritance.
Static in Classes CSCE 121 J. Michael Moore.
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Multiple Base Classes Inheritance
CS1201: Programming Language 2
CS1201: Programming Language 2
Pointers and Pointer-Based Strings
Inheriting Multiple Base Classes
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
CS1201: Programming Language 2
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

Virtual Base Classes By:Nouf Aljaffan Edited by : Nouf Almunyif

An element of ambiguity can be introduced into a C++ program when multiple base classes are inherited. Consider the following incorrect program:

incorrect program: int main(){ derived3 ob; ob.i = 10;//this is ambiguous; which i??? ob.j = 20; ob.k = 30; // i ambiguous here, too ob.sum = ob.i + ob.j + ob.k; // also ambiguous, which i? cout << ob.i << « » cout << ob.j <<“ “ << ob.k <<“ “ ; cout << ob.sum; return 0; } // This program contains an error and will not compile. #include <iostream> using namespace std; class base { public: int i; }; //derived1 inherits base. class derived1 : public base { int j; }; //derived2 inherits base. class derived2 : public base { int k; }; /* derived3 inherits both derived1 and derived2. This means that there are two copies of base in derived3! */ class derived3 :public derived1, public derived2 { public: int sum;}; Base Derived1 Derived 3 Derived2

As the comments in this program indicate Both derivedl and derived2 inherit base. However, derived3 inherits both derivedl and derived2. As a result, there are two copies of base present in an object of type derived3, so that in an expression like ob.i = 20; which i is being referred to? The one in derivedl or the one in derived2? Since there are two copies of base present in object ob, there are two ob.is! As you can see, the statement is inherently ambiguous.

Solution There are two ways to remedy the preceding program The first is to apply the scope resolution operator to i and manually select one i. For example, the following version of the program will compile and run as expected

Resolve the incorrect program using 1stapproach: class base { public: int i; }; class derived1 : public base { int j; }; class derived2 : public base { int k; }; class derived3 :public derived1, public derived2 { public: int sum;}; int main(){ derived3 ob; ob.derived1:: i = 10; //scope resolved, uses derived1’s I ob.j = 20; ob.k = 30; // scope resolved ob.sum = ob.derived1:: i + ob.j + ob.k; cout << ob.derived1:: i << « » cout << ob.j <<“ “ << ob.k <<“ “ ; cout << ob.sum; return 0; } By applying the ::, the program manually selects derivedl's version of base. However, this solution raises a deeper issue: What if only one copy of base is actually required? Is there some way to prevent two copies from being included in derived3?

virtual base classes The second is to declare the base class as virtual The solution is achieved with virtual base classes. virtual base classes ensures that only one copy of it will be present in any derived class

2’nd approach using virtual base classes // This program uses virtual base classes #include <iostream> using namespace std; class base { public: int i; }; //derived1 inherits base. class derived1 :virtual public base { int j; }; //derived2 inherits base. class derived2 : virtual public base { int k; }; /* derived3 inherits both derived1 and derived2. This means that there is only one copy of base in derived3! */ class derived3 :public derived1, public derived2 { public: int sum;}; int main(){ derived3 ob; ob. i = 10; //scope resolved, uses derived1’s I ob.j = 20; ob.k = 30; // unambiguous ob.sum = ob.i + ob.j + ob.k; cout << ob. i << « » cout << ob.j <<“ “ << ob.k <<“ “ ; cout << ob.sum; return 0; } Now that both derivedl and derived2 have inherited base as virtual, any multiple inheritance involving them will cause only one copy of base to be present. Therefore, in derived3 there is only one copy of base, and ob.i = 10 is perfectly valid and unambiguous.

difference between a normal base class and a virtual The only difference between a normal base class and a virtual one becomes evident when an object inherits the base class more than once. If the base class has been declared as virtual, then only one instance of it will be present in the inheriting object. Otherwise, multiple copies will be found.