CS 222 Object Oriented Programming Using C++ Inheritance.

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
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.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Learners Support Publications Inheritance: Extending Classes.
C++ Inheritance Systems Programming.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
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.
C++ for the MFE class at UC Berkeley Session # Yuli Kaplunovsky MFE, MBA, MS-Tax, CFA (408)
Slide 1 Inheritance: Part I. Slide 2 Structuring ‘classes’ class Employee { string firstName; string familyName; … } class Manager { Employee e; list.
1 Inheritance: constructors Constructors, copy constructors and destructors are NOT inherited. Each subclass has its own constructors and destructor to.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
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.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 CSC241: Object Oriented Programming Lecture No 13.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
CLASS INHERITANCE WEEK 7 Chapter 20 CS SENEM KUMOVA METIN1.
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.
OOP, Virtual Functions and Inheritance
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Object Oriented Programming in C++ Chapter 6 Inheritance.
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.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
Inheritance in C++ 2 CSC1201: Programming Language 2 1Aseel AlHadlaq_KSU.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
1 Chapter 4: Another way to define a class Inheritance..!!
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
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.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Constructors and Destructors
7. Inheritance and Polymorphism
Polymorphism &Virtual Functions
Polymorphism & Virtual Functions
Polymorphism Lec
CS1201: Programming Language 2
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance.
Constructors and Destructors
CS1201: Programming Language 2
CS1201: Programming Language 2
CS1201: Programming Language 2
Object-Oriented Programming (OOP) Lecture No. 22
Multiple Base Classes Inheritance
Inheritance.
Inheriting Multiple Base Classes
Inheritance in C++ Inheritance Protected Section
Presentation transcript:

CS 222 Object Oriented Programming Using C++ Inheritance

Inheritance is one of the advantages of OOP because it enables software reusability. With inheritance, it is possible to create a general class that defines common attributes for a set of related items. This class may then be inherited by other, more specific classes, each adding only those things that are unique to the inheriting class. A class that is inherited is referred to as a base class. The class that does the inheriting is called the derived class. Further, a derived class can be used as a base class for another derived class.

Inheritance A direct base class is the base class from which a derived class explicitly inherits. An indirect base class is inherited from two or more levels up in the class hierarchy. In the case of single inheritance, a class is derived from one base class. C++ also supports multiple inheritance, in which a derived class inherits from multiple (possibly unrelated) base classes.  We discuss multiple inheritance in Chapter 24, Other Topics.

Inheritance Base Class Derived Class1Derived Class 2 road_vehicle truck Patient In PatientOut Patient automobile

Inheritance

Inheritance Example class road_vehicle { int wheels; int passengers; public: void set_wheels(int num) { wheels = num; } int get_wheels() { return wheels; } void set_pass(int num) { passengers = num; } int get_pass() { return passengers; } }; class truck : public road_vehicle { int cargo; public: void set_cargo(int size) { cargo = size; } int get_cargo() { return cargo; } void show(); }; class derived-class : access base-class { body of new class } Access may be either public, private, or protected. Using public means that all the public members of the base class will also be public members of the derived class. However the inherited class will not have access to private members

Inheritance Example class road_vehicle { int wheels; int passengers; public: void set_wheels(int num) { wheels = num; } int get_wheels() { return wheels; } void set_pass(int num) { passengers = num; } int get_pass() { return passengers; } }; class truck : public road_vehicle { int cargo; public: void set_cargo(int size) { cargo = size; } int get_cargo() { return cargo; } void show(); }; enum type {car, van, wagon}; class automobile : public road_vehicle { enum type car_type; public: void set_type(type t) { car_type = t; } enum type get_type() { return car_type; } void show(); }; void truck::show() {cout<<"wheels: "<<get_wheels() << "\n"; cout<<"passengers: " <<get_pass() << "\n"; cout<<"cargo capacity in cubic feet:"<<cargo; } void automobile::show() {cout<<"wheels: "<<get_wheels()<<"\n"; cout<<"passengers: "<< get_pass()<<"\n"; cout<<"type: "; switch(get_type()) { case van: cout << "van\n"; break; case car: cout << "car\n"; break; case wagon: cout << "wagon\n"; }} int main() { truck t1, t2; automobile c; t1.set_wheels(18); t1.set_pass(2); t1.set_cargo(3200); t2.set_wheels(6); t2.set_pass(3); t2.set_cargo(1200); t1.show(); t2.show(); c.set_wheels(4); c.set_pass(6); c.set_type(van); c.show(); return 0; }

Base Class Access Control The base class access specifier must be public, private or protected The access specifier is optional If the access specifier is not used, then it is private by default.

Inheritance as Public [Public Base Class Access Specifier] When a base class is inherited as public, its public members become public members of the derived class, thus they are accessible by other parts of the program. Private members of a base class always remain private to that class, and are not inherited.

Inheritance As Public Example class base { int i, j; public: void set(int a, int b) { i = a; j = b; } void show() { cout << i << " " << j << "\n"; } }; class derived : public base { int k; public: derived(int x) { k = x; } void showk() { cout << k << "\n"; } }; int main() { derived ob(3); ob.set(1, 2); // access member of base ob.show(); // access member of base ob.showk(); // uses member of derived class return 0; }

Inheritance as Private [Private Base Class Access Specifier] When a base class is inherited as private its public members become private members of the derived class, thus they are only accessible to the members of the derived class and not accessible by other parts of the program. Private members of a base class always remain private to that class, and are not inherited.

Inheritance As Private Example //This program won't compile. #include using namespace std; class base { int i, j; public: void set(int a, int b) { i = a; j = b; } void show() { cout << i << " " << j << "\n"; } }; // Public elements of base are private in derived. class derived : private base { int k; public: derived(int x) { k = x; } void showk() { cout << k << "\n"; } }; int main() { derived ob(3); ob.set(1, 2); // Error, can't access set() ob.show(); // Error, can't access show() return 0; } Error: Because both set() and show() are now private members of derived, and thus cannot be called from main()

Using Protected Members As you know:  A class member can be declared as protected  And a base class can be inherited as protected When a member class is declared as protected, that member is not accessible to other non-member elements of the program. Access to protected member is the same as access to private member, it can be accessed only by other members of the same class. A private member of a base class is not accessible by any other part of your program including any derived class. However protected members can be inherited and accessed by derived classes.

Protected Members Example include using namespace std; class base { protected: int i, j; // private to base, but accessible to derived public: void set(int a, int b) { i = a; j = b; } void show() { cout << i << " " << j << "\n"; } }; class derived : public base { int k; public: // derived may access base's i and j void setk() { k = i*j; } void showk() { cout << k << "\n"; } }; int main() { derived ob; ob.set(2, 3); // OK, known to derived ob.show(); // OK, known to derived ob.setk(); ob.showk(); return 0; }

Inheriting Protected Members Example class base { protected: int i, j; public: void set(int a, int b) { i = a; j = b; } void show() { cout << i << " " << j << "\n"; }}; class derived1 : public base { int k; public: void setk() { k = i*j; } // legal void showk() { cout << k << "\n"; }}; class derived2 : public derived1 { int m; public: void setm() { m = i-j; } // legal void showm() { cout << m << "\n"; }}; int main() { derived1 ob1; derived2 ob2; ob1.set(2, 3); ob1.show(); ob1.setk(); ob1.showk(); ob2.set(3, 4); ob2.show(); ob2.setk(); ob2.setm(); ob2.showk(); ob2.showm(); return 0; } When a derived class is used as a base class for another derived class, then any protected member of the initial base class that is inherited (as public) by the first derived class will be a protected member of the derived class and can be inherited again, as a protected member, by a second derived class.

Inheriting Protected Members Example // This program won't compile. class base { protected: int i, j; public: void set(int a, int b) { i = a; j = b; } void show() { cout << i << " " << j << "\n"; } }; // Now, all elements of base are private in derived1. class derived1 : private base { int k; public: // This is legal because i and j are private to derived1. void setk() { k = i*j; } // OK void showk() { cout << k << "\n"; } }; // Access to i, j, set(), and show() not inherited. class derived2 : public derived1 { int m; public: // Illegal because i and j are private to derived1. void setm() { m = i-j; } // error void showm() { cout << m << "\n"; } }; int main() { derived1 ob1; derived2 ob2; ob1.set(1, 2); // Error, can't use set() ob1.show(); // Error, can't use show() ob2.set(3, 4); // Error, can't use set() ob2.show(); // Error, can't use show() return 0; } When a base class is inherited as private, protected members of the base class become private members of the derived class.

Inheritance as Protected [Protected Base Class Access Specifier] When a base class is inherited as protected its public members and protected members become protected members of the derived class, thus they are only accessible to the members of the derived class and any classes derived from it and not accessible by other parts of the program. Private members of a base class always remain private to that class, and are not inherited.

Inheriting as Protected Example class base { int i; protected: int j; public: int k; void seti(int a) { i = a; } int geti() { return i; } }; // Inherit base as protected. class derived : protected base { public: void setj(int a) { j = a; } // j is protected here void setk(int a) { k = a; } // k is also protected int getj() { return j; } int getk() { return k; } }; int main() { derived ob; /* This next line is illegal because seti() is protected */ // ob.seti(10); // cout << ob.geti(); // illegal -- geti() is protected // ob.k = 10; // also illegal because k is protected // these next statements are OK ob.setk(10); cout << ob.getk() << ' '; ob.setj(12); cout << ob.getj() << ' '; return 0; } When a base class is inherited as protected, all public and protected members of the base class become protected members of the derived

Reviewing public, private and protected Member Access Specifiers  When a class member is declared as public, it can be accessed by any other part of a program.  When a member is declared as private, it can be accessed only by members of its class. Derived classes do not have access to private base class members.  When a member is declared as protected, it can be accessed only by members of its class, or by derived classes.

Reviewing public, private and protected Base Class Access Specifiers Base Class Access Specifier Private Members Protected Members Public Members Private Never inherited they remain private to the base class Become private members of the derived class Protected Never inherited they remain private to the base class Become protected members of the derived class Public Never inherited they remain private to the base class Become protected members of the derived class Become public members of the derived class

Inheriting Multiple Base Classes 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 base1 ob.showy(); // from base2 return 0; }

Constructors Destructors And Inheritance When Constructor An Destructor Functions Are Executed? #include using namespace std; class base { public: base() { cout << "Constructing base\n"; } ~base() { cout << "Destructing base\n"; } }; class derived: public base { public: derived() { cout << "Constructing derived\n"; } ~derived() { cout << "Destructing derived\n"; } }; int main() { derived ob; // do nothing but construct and destruct ob return 0; } Constructors are executed in the order of their derivation. Destructors are executed in reverse order of derivation. Output: Constructing base Constructing derived Destructing derived Destructing base

Constructors Destructors And Inheritance When Constructor An Destructor Functions Are Executed? With large class hierarchy 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; } Constructors are executed in the order of their derivation. Destructors are executed in reverse order of derivation. Output: Constructing base Constructing derived1 Constructing derived2 Destructing derived2 Destructing derived1 Destructing base

Constructors Destructors And Inheritance When Constructor An Destructor Functions Are Executed? With multiple base classes 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() { derived ob; // construct and destruct ob return 0; } Constructors are executed in the order of their derivation. Destructors are executed in reverse order of derivation. Output: Constructing base1 Constructing base2 Constructing derived Destructing derived Destructing base2 Destructing base1

Constructors Destructors And Inheritance When Constructor An Destructor Functions Are Executed? With multiple base classes Output: Constructing base2 Constructing base1 Constructing derived Destructing derived Destructing base1 Destructing base2 Class derived: public base2, public base1{