Inheritance in C++ Multiple Base Classes Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.

Slides:



Advertisements
Similar presentations
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
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)
Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
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.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
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.
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.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
Functions Modules in C++ are called functions and classes
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.
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.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
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.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
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.
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.
Object-Oriented Programming in C++ More examples of Association.
Order of Constructor Call. Base class constructors are always called in the derived class constructors. Whenever you create derived class object, first.
 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.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Inheritance in C++ 2 CSC1201: Programming Language 2 1Aseel AlHadlaq_KSU.
1 CSC241: Object Oriented Programming Lecture No 03.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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.
Constructor Operation tMyn1 Constructor Operation in a Derived Class So far the default base class constructor has been called automatically. We can arrange.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
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.
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.
Constructors and Destructors
Polymorphism &Virtual Functions
Polymorphism & Virtual Functions
group work #hifiTeam
Polymorphism Lec
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
CS1201: Programming Language 2
Name: Rubaisha Rajpoot
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance.
Virtual Base Classes By:Nouf Aljaffan Edited by : Nouf Almunyif.
Constructors and Destructors
CS1201: Programming Language 2
CS1201: Programming Language 2
CS1201: Programming Language 2
Chapter 6: User-Defined Functions I
Multiple Base Classes Inheritance
Inheriting Multiple Base Classes
Inheritance in C++ Inheritance Protected Section
Presentation transcript:

Inheritance in C++ Multiple Base Classes Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif

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 };

An example of multiple base classes // An example of multiple base classes. #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"; } }; // Inherit multiple base classes. 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);//provided by derived ob.showx();//from basel ob.showy();//from base2 return 0; }

Constructors, Destructors, and Inheritance 1.when are base class and derived class constructor and destructor functions called? 2.how can parameters be passed to base class constructor functions?

When Constructor and Destructor Functions Are Executed? It is possible for a base class, a derived class, or both, to contain constructor and/or destructor functions. It is important to understand the order in which these functions are executed ▫when an object of a derived class comes into existence ▫when it goes out of existence.

Example 1 1.#include 2.using namespace std; 3.class base { 4. public: 5. base(){ cout << "Constructing base\n";} 6. ~base(){ cout << "Destructing base\n"; } 7.}; 8.class derived: public base { 9. public: 10. derived() { cout << "Constructing derived\n";} 11. ~derived() { cout << "Destructing derived\n";} 12.}; 13.int main() 14.{ 15.derived ob; 16.// do nothing but construct and destruct ob 17.return 0; 18.} General Rule constructor functions are executed in the order of their derivation. Destructor functions are executed in reverse order of derivation.

Example 2 #include using namespace std; class base { public: base() { cout << "Constructing base\n"; } ~ base() { cout << "Destructing base\n"; } }; class derived1: public base{ public: derived1() {cout << "Constructing derived1\n"; } ~ derived1() {cout << "Destructing derived1\n"; } }; class derived2: public derived1 { public: derived2{ cout << "Constructing derived2\n"; } ~ derived2{ cout << "Destructing derived2\n";} }; int main() { derived2 ob; // construct and destruct ob return 0;}

The same general rule applies in situations involving multiple base classes Example 1 #include using namespace std; 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;} 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.

Example 2 1.#include 2.using namespace std; 3.class base1{ 4. public: 5. base1() { cout << "Constructing base1\n";} 6. ~ base1() { cout << "Destructing base1\n"; } 7.}; 8.class base2{ 9. public: 10. base2() { cout << "Constructing base2\n"; } 11. ~ base2() { cout << "Destructing base2\n"; } 12.}; 13.class derived: public base2, public base1{ 14. public: 15. derived() { cout << "Constructing derived\n";} 16. ~derived() { cout << "Destructing derived\n";} 17.}; 1.int main() { 2.derived ob; 3.// construct and destruct ob 4.return 0;}

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. derived-constructor(arg-list) : base1 (arg-list), base2(arg-list), ….baseN(arg-list) { body of derived constructor }

Example 1 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(); //displays 4 3 return 0;} 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.

Example 2 uses multiple base classes #include using namespace std; 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"; } }; 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(); //displays return 0; }

Example 3: Derived take no arguments but base1() and base2() #include using namespace std; 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"; } }; class derived: public base1, public base2{ int j; public: /* Derived constructor uses no parameters, but still must be declared as taking them to pass them along to base classes.*/ 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(); //displays 3 4 return 0; }

Notice The constructor function of a derived class is free to use any and all parameters that it is declared as taking, whether or not one or more are passed along to a base class. Put differently, just because an argument is passed along to a base class does not produce its use by the derived class as well. For example, this fragment is perfectly valid: 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"; } };