The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

Similar documents
C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.

A A B U n i v e r s i t y

Developed By Strawberry

Sahaj Computer Solutions OOPS WITH C++

Inheritance, and Polymorphism.

OOP. Unit:3.3 Inheritance

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Friend Functions, Inheritance

PROGRAMMING LANGUAGE 2

6.096 Introduction to C++ January (IAP) 2009

Midterm Exam 5 April 20, 2015

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Lecture 5: Inheritance

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class

Polymorphism Part 1 1

Computer Programming Inheritance 10 th Lecture

Object-Oriented Programming (OOP) Fundamental Principles of OOP

CS OBJECT ORIENTED PROGRAMMING

Unit-1/Lecture-1 Object-oriented programming (OOP) [RGPV/June2014(7),June2013(8), June2011(10), Feb2010(10),June2009 (10)]

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

7. C++ Class and Object

Class (Inheritance) SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

CS 162, Lecture 25: Exam II Review. 30 May 2018

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

OBJECT ORIENTED PROGRAMMING USING C++

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

Introduction Of Classes ( OOPS )

Chapter 1: Object-Oriented Programming Using C++

Extending Classes (contd.) (Chapter 15) Questions:

IUE Faculty of Engineering and Computer Sciences Spring Semester

Inheritance and Overloading. Week 11

C++ (classes) Hwansoo Han

Inheritance

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

L4: Inheritance. Inheritance. Chapter 8 and 10 of Budd.

Base class or Super class. Subclass or Derived class

Extending Classes (contd.) (Chapter 15) Questions:

Programming in C# Inheritance and Polymorphism

Object-Oriented Concept

SSE2034: System Software Experiment 3 Spring 2016

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

Java Object Oriented Design. CSC207 Fall 2014

Cpt S 122 Data Structures. Inheritance

Object Oriented Programming. A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

18. Polymorphism. Object Oriented Programming: Pointers to base class // pointers to base class #include <iostream> using namespace std;

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

Simula. Where Would We Be Without It? By Joe Casey

Comp 249 Programming Methodology

Module 10 Inheritance, Virtual Functions, and Polymorphism

INHERITANCE PART 2. Constructors and Destructors under. Multiple Inheritance. Common Programming Errors. CSC 330 OO Software Design 1

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

OBJ. ORI.& MULT. PROG., M.C.Q. BANK, FOR UNIT -2, SECOND YEAR COMP. ENGG. SEM-4, 2012 PATTERN, U.O.P. UNIT-2

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Constructor - example

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Polymorphism CSCI 201 Principles of Software Development

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

CS313D: ADVANCED PROGRAMMING LANGUAGE

22316 Course Title : Object Oriented Programming using C++ Max. Marks : 70 Time: 3 Hrs.

Object Oriented Programming in C++ Basics of OOP

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Programming in C++: Assignment Week 5

ECE 3574: Dynamic Polymorphism using Inheritance

Inheritance, Polymorphism, and Interfaces

CHAPTER 9 INHERITANCE. 9.1 Introduction

Introduction to Classes

INHERITANCE IN OBJECT ORIENTED PROGRAMMING EASIEST WAY TO TEACH AND LEARN INHERITANCE IN C++ TWINKLE PATEL

Object Oriented Programming(OOP).

Constructors for classes

More C++ : Vectors, Classes, Inheritance, Templates

1. the base class s (default) constructor is executed first, 2. followed by the derived class s constructor

Distributed Real-Time Control Systems. Lecture 14 Intro to C++ Part III

Chapter 3 Function Overloading

Comp151. Inheritance: Initialization & Substitution Principle

Object Oriented Programming. Java-Lecture 11 Polymorphism

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Programming in C++: Assignment Week 4

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

G52CPP C++ Programming Lecture 13

Comp151. Inheritance: Initialization & Substitution Principle

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

C++ Important Questions with Answers

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I.

Object Oriented Software Design II

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Exercise1. // classes first example. #include <iostream> using namespace std; class Rectangle. int width, height; public: void set_values (int,int);

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

Unit 3 INFORMATION HIDING & REUSABILITY. -Inheritance basics -Using super -Method Overriding -Constructor call -Dynamic method

Object Oriented Software Design II

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

C++ 8. Constructors and Destructors

Transcription:

Class : BCA 3rd Semester Course Code: BCA-S3-03 Course Title: Object Oriented Programming Concepts in C++ Unit III Inheritance The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance. Inheritance lets you create new classes from existing class. Any new class that you create from an existing class is called derived class; existing class is called base class. The inheritance relationship enables a derived class to inherit features from its base class. Furthermore, the derived class can add new features of its own. Therefore, rather than create completely new classes from scratch, you can take advantage of inheritance and reduce software complexity. Forms of Inheritance Single Inheritance: It is the inheritance hierarchy wherein one derived class inherits from one base class. Multiple Inheritance: It is the inheritance hierarchy wherein one derived class inherits from multiple base class(es) Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple subclasses inherit from one base class. Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class for other classes. Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination of other four types of inheritance.

In order to derive a class from another, we use a colon (:) in the declaration of the derived class using the following format : class derived_class: memberaccessspecifier base_class... ; Where derived_class is the name of the derived class and base_class is the name of the class on which it is based. The member Access Specifier may be public, protected or private. This accesss specifier describes the access level for the members that are inherited from the base class. Member Access Specifier Private How Members of the Base Class Appear in the Derived Class Private members of the base class are inaccessible to the class. derived Protected members of the base class become private members of the derived class. Public members of the base class become private members of the derived class. Protected Private members of the base class are inaccessible to the derived class.

Protected members of the base class become protected members of the derived class. Public members of the base class become protected members of the derived class. Public Private members of the base class are inaccessible to the class. derived Protected members of the base class become protected members of the derived class. Public members of the base class become public members of the derived class. In principle, a derived class inherits every member of a base class except constructor and destructor. It means private members are also become members of derived class. But they are inaccessible by the members of derived class. Following example further explains concept of inheritance : class Shape protected: float width, height; void set_data (float a, float b) width = a; height = b; ; class Rectangle: public Shape float area ()

; return (width * height); class Triangle: public Shape float area () return (width * height / 2); ; int main () Rectangle rect; Triangle tri; rect.set_data (5,3); tri.set_data (2,5); cout << rect.area() << endl; cout << tri.area() << endl; return 0; output : 15 5 The object of the class Rectangle contains : width, height inherited from Shape becomes the protected member of Rectangle. set_data() inherited from Shape becomes the public member of Rectangle area is Rectangle s own public member The object of the class Triangle contains : width, height inherited from Shape becomes the protected member of Triangle. set_data() inherited from Shape becomes the public member of Triangle area is Triangle s own public member set_data () and area() are public members of derived class and can be accessed from outside class i.e. from main()

Constructor and inheritance The compiler automatically calls a base class constructor before executing the derived class constructor. The compiler s default action is to call the default constructor in the base class. You can specify which of several base class constructors should be called during the creation of a derived class object. This is done by specifying the arguments to the selected base class constructor in the definition of the derived class constructor. class Rectangle private : float length; float width; Rectangle () length = 0; width = 0; Rectangle (float len, float wid) length = len; width = wid; ; float area() return length * width ; class Box : public Rectangle private : float height; Box ()

height = 0; Box (float len, float wid, float ht) : Rectangle(len, wid) height = ht; ; float volume() return area() * height; int main () Box bx; Box cx(4,8,5); cout << bx.volume() << endl; cout << cx.volume() << endl; return 0; output : 0 160 Overriding Base Class Functions A derived class can override a member function of its base class by defining a derived class member function with the same name and parameter list. It is often useful for a derived class to define its own version of a member function inherited from its base class. This may be done to specialize the member function to the needs of the derived class. When this happens, the base class member function is said to be overridden by the derived class. class mother void display () cout << "mother: display function\n"; ; class daughter : public mother

void display () cout << "daughter: display function\n\n"; ; int main () daughter rita; rita.display(); return 0; output: daughter: display function Gaining Access to an Overridden Function It is occasionally useful to be able to call the overridden version. This is done by using the scope resolution operator to specify the class of the overridden member function being accessed. class daughter : public mother void display () cout << "daughter: display function\n\n"; mother::display(); ; output: daughter: display function mother: display function Virtual Base Class Multipath inheritance may lead to duplication of inherited members from a grandparent base class. This

may be avoided by making the common base class a virtual base class. When a class is made a virtual base class, C++ takes necessary care to see that only one copy of that class is inherited. class A ; class B1 : virtual public A ; class B2 : virtual public A ; class C : public B1, public B2 // only one copy of A // will be inherited ; Ambiguity in multiple inheritance Ambiguity in multiple inheritance: In multiple inheritance, there may be possibility that a class may inherit member functions with same name from two or more base classes and the derived class may not have functions with same name as those of its base classes. If the object of the derivedd class need to

access one of the same named member function of the base classes then it result in ambiguity as it is not clear to the compiler which base s class member function should be invoked. The ambiguity simply means the state when the compiler gets confused. Example: class a private: void abc() ; class b private: void abc() ; class c : public a, public b private: ; void main() c obj; obj.abc();//error. In this example, the two base classes a and b have function of same name abc() which are inherited to the derived class c. When the object of the class c is created and calls the function abc() then the compiler is confused which base class function to call.

Solution of ambiguity in multiple inheritance: The ambiguity can be resolved by using the scope resolution operator to specify the class in which the member function lies as given below: obj.a :: abc(); This statement invoke the function name abc() which are lies in base class a.