Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
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.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Inheritance Inheritance – most important and a useful feature of OOPs supported by C++ | Website for Students | VTU -NOTES -Question Papers.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
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.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Learners Support Publications Classes and Objects.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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.
Object Oriented Programming in C++ Chapter 6 Inheritance.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Object Oriented Software Development
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Function Overloading and References
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance and Polymorphism
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
A First Book of C++ Chapter 12 Extending Your Classes.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
7. Inheritance and Polymorphism
Introduction to Classes
Lecture 22 Inheritance Richard Gesick.
Classes and Objects.
Fundaments of Game Design
Java Programming Language
C++ Object Oriented 1.
Presentation transcript:

Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41

Session Objectives Describe Single Inheritance Describe Base class and Derived class Access Base class members and use pointers in classes Describe types of inheritance Describe Constructors and Destructors under inheritance Describe how to call Member Functions of the Base Class and Derived Class Describe Container Classes Object Oriented Programming with C++/ Session 5 / 2 of 41

Single Inheritance To maintain and reuse class objects easily, we need to be able to relate classes of similar nature to another. Single inheritance is the process of creating new classes from an existing base class. For example let us consider a program in which we are dealing with people employed in an organisation. Object Oriented Programming with C++/ Session 5 / 3 of 41

Single Inheritance (Contd.) Employee Director Manager Secretary Clerk Each of the subclasses is considered to be derived from the class Employee. The class Employee is called the base class and the newly created class is called the derived class. Object Oriented Programming with C++/ Session 5 / 4 of 41

Single Inheritance (Contd.) In a class hierarchy, the derived classes inherit the methods and variables of the base class. They can also have properties and methods of their own. Class: Manager m_name, m_age, m_Emp_id, m_salary m_department perks, no_of_employees reporting Class: Employee Name, Age, Emp_id Salary, Department Object Oriented Programming with C++/ Session 5 / 5 of 41

Advantages Most important advantage: reusability of code. Once a base class has been created it can be adapted to work in different situations. Result of reusability of code is the development of class libraries. A class library consists of data and methods encapsulated in a class. Deriving a class from an existing one allows redefining a member function of the base class and also adding new members to the derived class. The base class remains unchanged in the process. Object Oriented Programming with C++/ Session 5 / 6 of 41

Base Class and Derived Class Derivation can be represented graphically with an arrow from the derived class to the base class. The arrow pointing towards the base class signifies that the derived class refers to the functions and data in the base class, while the base class has no access to the derived class. Employee Manager Base class Derived class Derived from Object Oriented Programming with C++/ Session 5 / 7 of 41

Base Class and Derived Class Declaration of a singly derived class is similar to that of any ordinary class. We also have to give the name of the base class. For example, class Manager : public Employee Any class can be used as a base class. A base class can be classified into two types: direct base indirect base Object Oriented Programming with C++/ Session 5 / 8 of 41

Direct and Indirect Base A base class is called direct if it is mentioned in the base list. For example: class A { }; class B : public A { }; // where class A is a direct class. An indirect class can be written as: class C : public B { }; //Can be extended to an arbitrary number of levels. Object Oriented Programming with C++/ Session 5 / 9 of 41

Accessibility Accessibility: Knowing when a member function or data member of a base class can be used by objects of the derived class. Class members can always be accessed by member functions within their own class, whether the members are private or public. Objects defined outside the class can access class members only if the members are public. Object Oriented Programming with C++/ Session 5 / 10 of 41

Accessing Base Class Members With inheritance: Derived class members can access members of the base class if its members are public. Derived class members cannot access the private members of the base class. For example, if emp1 is an instance of class Employee, and display() is a member function of Employee, then in main() the statement emp1.display(); is valid if display() is public. The object emp1 cannot access private members of the class Employee. Object Oriented Programming with C++/ Session 5 / 11 of 41

Protected Access Specifier The protected section is like the private section in terms of scope and access. Protected members can be accessed only by members of that class. Protected members cannot be accessed by objects or functions from outside the class, such as main(). The difference between private and protected appears only in derived classes. Object Oriented Programming with C++/ Session 5 / 12 of 41

Accessing Base Class members (Contd) Members of the derived class can access public and protected members; they cannot access the private members of the base class. In conformance with the object-oriented concept of information hiding. No access to some of the class members. Those members can be put in the private section. Allow controlled access by providing some protected members. Inheritance does not work in reverse. Object Oriented Programming with C++/ Session 5 / 13 of 41

Access rules for Base class members Object Oriented Programming with C++/ Session 5 / 14 of 41

Example class Employee{ //base class private: int privA; protected: int protA; public: int pubA; }; Object Oriented Programming with C++/ Session 5 / 15 of 41

Example (Contd.) //derived class class Manager : public Employee{ void fn() { int a; a = privA; //error:not accessible a = protA; //valid a = pubA; //valid } }; Object Oriented Programming with C++/ Session 5 / 16 of 41

Example (Contd.) void main() { Employee emp; //base class object emp.privA = 1; //error:not accessible emp.protA = 1; //error:not accessible emp.pubA = 1; //valid Manager mgr; //derived class object mgr.privA = 1; //error:not accessible mgr.protA = 1; //error:not accessible mgr.pubA = 1; //valid } Object Oriented Programming with C++/ Session 5 / 17 of 41

Pointers in classes We can use a pointer, which has been declared to point to one class, to actually refer to another class. If a derived class has a public base class, then a pointer to the derived class can be assigned to a variable of type pointer to the base. For example, because a Manager is an Employee, a Manager* can be used as an Employee*. However, an Employee* cannot be used as a Manager*. Object Oriented Programming with C++/ Session 5 / 18 of 41

Example of pointers void main() { Manager mgr; Employee* emp = &mgr; //valid:every Manager is an Employee Employee eml; Manager* man = &eml; //error: not every Employee is a Manager } An object of a derived class can be treated as an object of its base class when manipulated through pointers. However, the opposite is not true. Object Oriented Programming with C++/ Session 5 / 19 of 41

Types of Inheritance A derived class can be declared with one of the specifiers i.e., public, private and protected. The keyword public in the class declaration of the derived class specifies that objects of the derived class are able to access public member functions of the base class. With the keyword private in the derived class declaration, objects of the derived class in main() cannot even access public member functions of the base class. Object Oriented Programming with C++/ Session 5 / 20 of 41

Example of inheritance types class A{ //base class private: int privA; protected: int protA; public: int pubA; }; class B : public A{ //publicly derived class public: void fn(){ int a; a = privA; //error:not accessible a = protA; //valid a = pubA; //valid } Object Oriented Programming with C++/ Session 5 / 21 of 41

Example (Contd.) class C : private A{ //privately derived class public: void fn() { int a; a = privA; //error:not accessible a = protA; //valid a = pubA; //valid } }; void main() { int m; B obj1; //object of publicly derived class Object Oriented Programming with C++/ Session 5 / 22 of 41

Example (Contd.)  If no access specifier is given while creating m = obj1.privA; //error:not accessible m = obj1.protA; //error:not accessible m = obj1.pubA; //valid: B is publicly //derived from class A C obj2; //object of privately derived class m = obj2.privA; //error:not accessible m = obj2.protA; //error:not accessible m = obj2.pubA; //error:not accessible: //C is privately derived from class A }  If no access specifier is given while creating the class, private is assumed. Object Oriented Programming with C++/ Session 5 / 23 of 41

Types of Inheritance (contd.) Functions in the derived classes can access protected and public members in the base class. Objects of the derived classes outside the class or in main() cannot access private or protected members of the base class. The difference is between publicly and privately derived classes. Objects of the class B, which is publicly derived from A can access public members of the base class. However, objects of the class C, which is privately derived from A, cannot access any of the members of the base class. Object Oriented Programming with C++/ Session 5 / 24 of 41

Types of Inheritance (contd.) The functions in a protected derived class can access protected and public members of the base class. However, objects of the derived class (in main or outside the class) cannot access any of the members of the base class. Object Oriented Programming with C++/ Session 5 / 25 of 41

Accessibility for Derived classes There is an easy way to remember the table. First of all, derived classes have no access to private members of a base class. Secondly, inheriting the base class publicly does not change the access levels of the members inherited by the derived class from the base. The other two access levels of the base class cause all inherited members to be of the same access level, as the base class (private for private base, protected for protected base). Object Oriented Programming with C++/ Session 5 / 26 of 41

Multi-level inheritance Public, private or protected will affect the access the derived class functions have over the members of the base classes in a multi-level inheritance. Object Oriented Programming with C++/ Session 5 / 27 of 41

Example for multi-level inheritance In the following code the class B derives privately from class A and class C in turn derives publicly from class B. class A{ public : int a; }; class B : private A{ int b; void func_b() { int x,y; x=a; // valid y=b; // valid } Object Oriented Programming with C++/ Session 5 / 28 of 41

Example (Contd.) class C : public B { public : void func_c() {int x,y; x=a; // not valid y=b; // valid } }; Object Oriented Programming with C++/ Session 5 / 29 of 41

Constructors under inheritance The constructor of the base part of an object is first called, then the appropriate constructor of the derived class is called. class Base{ protected: int a; public: Base(){a = 0;} //default constructor Base(int c){ a = c;}//one-arg constructor }; class Derived{ Derived(): Base(){}//default constructor Derived(int c): Base(c){} //constructor with one-arg Object Oriented Programming with C++/ Session 5 / 30 of 41

Constructors (Contd.) When you declare an object of the derived class, with the statement Derived obj; it will cause the constructor of the base class to be called first and then the constructor of the derived class The base class constructor is given after the derived class constructor, separated by a colon, as in, Derived(): Base(){} Object Oriented Programming with C++/ Session 5 / 31 of 41

Constructors (Contd.) Can explicitly select which constructor of the base class should be called during a call of the constructor of a derived class. Derived obj1(20); uses the one-argument constructor in Derived. This constructor also calls the corresponding constructor in the base class. Derived(int c): Base(c); //argument c is passed to Base Object Oriented Programming with C++/ Session 5 / 32 of 41

Destructors Destructors are called in the reverse order to constructors. Destructors will be called for the derived class first, then for the base class. A destructor for the derived class is to be defined only if its constructor allocates any memory through dynamic memory management. If the constructor of the derived class does not do anything or no additional data members are added in the derived class, then the destructor for the derived class can be an empty function. Object Oriented Programming with C++/ Session 5 / 33 of 41

Calling Member Functions Member functions in a derived class can have the same name as those in the base class. When the function is invoked with the object of the base class, the function from the base class is called. When you use the name of the derived class object, the function from the derived class is invoked. If a member function from the derived class wants to call the base class function of the same name, it must use the scope resolution operator. Object Oriented Programming with C++/ Session 5 / 34 of 41

Calling Member Functions (Contd.) A function in the base class can be invoked using the objects of the base class as well as the derived class. If a function exists in the derived class and not in the base class, it can be invoked only with the objects of the derived class. Object Oriented Programming with C++/ Session 5 / 35 of 41

Calling Member Functions (Contd.) class Base{ protected: int ss; public: int func() {return ss;} }; class Derived: public Base{ {return Base::func();} Object Oriented Programming with C++/ Session 5 / 36 of 41

Calling Member Functions (Contd.) void main() { Base b1; //base class object b1.func(); //calls base class func Derived a1; //derived class object a1.func(); //calls derived class func } Object Oriented Programming with C++/ Session 5 / 37 of 41

Container classes Inheritance can be termed as an "is a" relationship. Example of the employee: The manager is an employee or the secretary is an employee etc. In contrast, a class X that has a member of another class Y is said to have a Y or X contains Y. This relationship is called a membership or a "has a" relationship. class X{ //X contains Y public: Y abc; }; Object Oriented Programming with C++/ Session 5 / 38 of 41

Container classes (Contd.) When a class contains an object of another class as its member it is called a container class. Example of a jet plane: Consider deriving a class for a jet plane from a class called engine. A jet plane is not an engine but has an engine. Whether to use inheritance or membership: Ask whether a jet plane has more than one engine. If that is a possibility, it is most likely that the relationship will be "has a" rather than "is a". Object Oriented Programming with C++/ Session 5 / 39 of 41

Constructors in container classes class engine{ private: int num; public: engine(int s) {num = s;} }; class jet{ int jt; engine eobj; //declaring an object here jet(int x, int y): eobj(y) {jt = x; } Object Oriented Programming with C++/ Session 5 / 40 of 41

Constructors (Contd.) In the constructor of class jet the name of the object of the class engine is written after the colon. It tells the compiler to initialise the eobj data member of class jet with the value in y. Similar to declaring an object of class engine with the statement, engine eobj(y); Variables of any data type can be initialised like this. Object Oriented Programming with C++/ Session 5 / 41 of 41