INHERITANCE IN C++ 3 CSC1201: PROGRAMMING LANGUAGE 2 ASEEL ALHADLAQ_KSU 1.

Slides:



Advertisements
Similar presentations
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)
Advertisements

Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
I NHERITANCE Chapter 7 Department of CSE, BUET 1.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
Learners Support Publications Inheritance: Extending Classes.
CS 222 Object Oriented Programming Using C++ Inheritance.
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.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
1 Inheritance: constructors Constructors, copy constructors and destructors are NOT inherited. Each subclass has its own constructors and destructor to.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
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.
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.
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.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
CS212: Object Oriented Analysis and Design Lecture 14: Reusing classes in C++
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
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.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
 A constructor is a special member function whose task is to initialize the objects of its class.  It is special because its name is same as the class.
Constructor in Inheritance. 2 Constructors are used to initialized object. In inheritance the base class contains default constructor then, the base class.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Inheritance in C++ 2 CSC1201: Programming Language 2 1Aseel AlHadlaq_KSU.
Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits.
1 CSC241: Object Oriented Programming Lecture No 03.
Overview of C++ Polymorphism
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Learners Support Publications Constructors and Destructors.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
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.
1.  The following class, called road_vehicle, very broadly defines vehicles, that travel on the road. It stores the number of wheels a vehicle has and.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Motivation for Generic Programming in C++
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Static data members Constructors and Destructors
Polymorphism &Virtual Functions
Constructors & Destructors.
Polymorphism & Virtual Functions
Polymorphism Lec
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
CS1201: Programming Language 2
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Name: Rubaisha Rajpoot
Virtual Functions Department of CSE, BUET Chapter 10.
Packages and Interfaces
Inheritance.
Virtual Base Classes By:Nouf Aljaffan Edited by : Nouf Almunyif.
Constructors and Destructors
Multiple Base Classes Inheritance
Overview of C++ Polymorphism
Inheriting Multiple Base Classes
Presentation transcript:

INHERITANCE IN C++ 3 CSC1201: PROGRAMMING LANGUAGE 2 ASEEL ALHADLAQ_KSU 1

OBJECTIVES Inheriting Multiple Base Classes Passing Parameters to Base Class Constructors Virtual Base Classes Difference Between Normal Base Class and Virtual Base Classes ASEEL ALHADLAQ_KSU 2

PASSING PARAMETERS TO BASE CLASS CONSTRUCTORS  The general form of this expanded declaration is shown here:  Notice that a colon separates the constructor function declaration of the derived class from the base class. ASEEL ALHADLAQ_KSU 3 derived-constructor(arg-list) : base1 (arg-list) { body of derived constructor }

class base{ protected: int i ; public: base( int x ) { i = x; cout << "Constructing base\n";} ~ base() { cout << "Destructing base\n"; } }; class derived: public base { int j; public: //derived uses x;" y is passed along to base. derived(int x, int y): base(y) { j = x; cout << "Constructing derived\n"; } ~derived(){cout << "Destructing derived\n"; } void show() {cout << i <<“—”<<j<< "\n"; } }; int main() { derived ob(3,4); ob.show(); return 0;} ASEEL ALHADLAQ_KSU 4 EXAMPLE

CONT. EXAMPLE ASEEL ALHADLAQ_KSU 5 derived's constructor is declared as taking two parameters, x and y. However, derived( ) uses only x; y is passed along to base( ). In general, the constructor of the derived class must declare the parameter(s) that its class requires, as well as any required by the base class.

INHERITING MULTIPLE BASE CLASSES  It is possible for a derived class to inherit two or more base classes.  General definition : class derivedName: access1 base1, access2 base2 { The member list of derivedName class }; ASEEL ALHADLAQ_KSU 6

INHERITING MULTIPLE BASE CLASSES ASEEL ALHADLAQ_KSU 7

#include using namespace std; class base1 { protected: int x; public: void showx() { cout << x <<"\n";} }; class base2 { protected: int y; public: void showy() {cout<< y << "\n"; }; class derived: public base1, public base2 { public: void set(int i, int j) { x= i; y =j; }}; int main(){ derived ob; ob.set(10, 20);//from derived ob.showx();//from basel ob.showy();//from base2 return 0; } ASEEL ALHADLAQ_KSU 8 AN EXAMPLE OF MULTIPLE BASE CLASSES

ASEEL ALHADLAQ_KSU 9 MULTIPLE BASE CLASSES CONSTRUCTING EXAMPLE Constructors are called in order of derivation, left to right, as specified in derived's inheritance list. Destructors are called in reverse order, right to left.

PASSING PARAMETERS TO BASE CLASS CONSTRUCTORS  The general form of this expanded declaration is shown here:  base1 through baseN are the names of the base classes inherited by the derived class.  Notice that a colon separates the constructor function declaration of the derived class from the base classes, and that the base classes are separated from each other by commas, in the case of multiple base classes. ASEEL ALHADLAQ_KSU 10 derived-constructor(arg-list) : base1 (arg-list), base2(arg-list), ….baseN(arg-list) { body of derived constructor }

EXAMPLE #1 ASEEL ALHADLAQ_KSU 11 class base1{ public: base1() { cout << "Constructing base1\n";} ~ base1() { cout << "Destructing base1\n"; } }; class base2{ public: base2() { cout << "Constructing base2\n"; } ~ base2() { cout << "Destructing base2\n"; } }; class derived: public base1, public base2 { public: derived() { cout << "Constructing derived\n";} ~derived() { cout << "Destructing derived\n";} }; int main() { derived2 ob; // construct and destruct ob return 0;}

ASEEL ALHADLAQ_KSU 12 EXAMPLE #2 class base1{ public: base1() { cout << "Constructing base1\n";} ~ base1() { cout << "Destructing base1\n"; } }; class base2{ public: base2() { cout << "Constructing base2\n"; } ~ base2() { cout << "Destructing base2\n"; } }; class derived: public base2, public base1{ public: derived() { cout << "Constructing derived\n";} ~derived() { cout << "Destructing derived\n";} }; int main() { derived ob; // construct and destruct ob return 0;}

class base1{ protected: int i ; public: base1( int x ) { i = x; cout <<“CONS base1\n";} ~ base1() { cout << “DES base1\n"; } }; class base2{ protected: int k; public: base2( int x ) { k = x; cout << “CONS base2\n";} ~base2() { cout << "DES base2\n"; } }; ASEEL ALHADLAQ_KSU 13 EXAMPLE MULTIPLE BASE CLASSES class derived : public base1, public base2 { int j; public: derived(int x, int y, int z ): base1(y), base2(z) { j = x; cout << "Constructing derived\n"; } ~derived(){ cout <<"Destructing derived\n";} void show(){cout << i <<"--" <<j<<"--" << k << "\n"; } }; int main() { derived ob(3,4,5); ob.show(); return 0; }

ASEEL ALHADLAQ_KSU 14

class base1{ protected: int i ; public: base1( int x ) { i = x; cout << "Constructing base1\n";} ~ base1() { cout << "Destructing base1\n"; } }; class base2{ protected: int k; public: base2( int x ) { k = x; cout << "Constructing base2\n";} ~ base2() { cout << "Destructing base2\n"; } }; ASEEL ALHADLAQ_KSU 15 EXAMPLE MULTIPLE BASE CLASSES class derived: public base1, public base2{ int j; public: derived(int x, int y): base1(x), base2(y) { cout << "Constructing derived\n"; } ~ derived(){ cout << "Destructing derived\n";} void show(){cout << i <<"--"<<j<<"--"<< k << "\n"; } }; int main() { derived ob(3,4); ob.show(); return 0; }

ASEEL ALHADLAQ_KSU 16

NOTICE The constructor function of a derived class is free to use any and all parameters that it is declared as taken, whether or not one or more are passed along to a base class. For example, this fragment is perfectly valid: ASEEL ALHADLAQ_KSU 17 class derived: public base{ int j; public: // derived uses both x and y and then passes them to base. derived(int x, int y): base(x, y) { j = x*y; cout << "Constructing derived\n"; } };

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

#include using namespace std; class base { public: int i; }; //derived1 inherits base. class derived1 : public base { public: int j; }; //derived2 inherits base. class derived2 : public base { public: 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;}; 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; } ASEEL ALHADLAQ_KSU 19 INCORRECT PROGRAM: BaseDerived1 Derived 3 Derived2

VIRTUAL BASE CLASSES 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.i’s! As you can see, the statement is inherently ambiguous. ASEEL ALHADLAQ_KSU 20

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 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? ASEEL ALHADLAQ_KSU 21 // scope resolved ob.sum = ob.derived1:: i + ob.j + ob.k; // scope resolved cout << ob.derived1:: i << « » cout << ob.j <<“ “ << ob.k <<“ “ ; cout << ob.sum; return 0; }

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 ASEEL ALHADLAQ_KSU 22 //derived1 inherits base class derived1 :virtual public base { public: int j; }; //derived2 inherits base. class derived2 : virtual public base { public: int k; };

// This program uses virtual base classes #include using namespace std; class base { public: int i; }; //derived1 inherits base. class derived1 :virtual public base { public: int j; }; //derived2 inherits base. class derived2 : virtual public base { public: 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; // unambiguous cout << ob. i << « » cout << ob.j <<“ “ << ob.k <<“ “ ; cout << ob.sum; return 0; } ASEEL ALHADLAQ_KSU 23 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 NORMAL BASE CLASS AND VIRTUAL BASE CLASSES 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. ASEEL ALHADLAQ_KSU 24