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

Similar documents
International Journal of Advance Research in Computer Science and Management Studies

Inheritance, and Polymorphism.

Object-Oriented Programming (OOP) Fundamental Principles of OOP

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.

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

Polymorphism Part 1 1

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

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

Partha Sarathi Mandal

Syllabus for Bachelor of Technology. Computer Engineering. Subject Code: 01CE1303. B.Tech. Year - II

CHAPTER 9 INHERITANCE. 9.1 Introduction

KINEMATIC MODELLING AND ANALYSIS OF 5 DOF ROBOTIC ARM

Data Structures using OOP C++ Lecture 3

Comp151. Inheritance: Initialization & Substitution Principle

Programming in C# Inheritance and Polymorphism

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

PROGRAMMING IN C++ COURSE CONTENT

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

Friend Functions, Inheritance

Java Object Oriented Design. CSC207 Fall 2014

Jaipur National University, Jaipur Dr. Rajendra Takale Prof. and Head Academics SBPIM, Pune

Sri Vidya College of Engineering & Technology

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

Object Oriented Programming

Sahaj Computer Solutions OOPS WITH C++

Lecture 5: Inheritance

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

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

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Developed By Strawberry

Data Structures (INE2011)

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

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

CS-202 Introduction to Object Oriented Programming

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

ITI Introduction to Computing II

An Object Oriented Programming with C

CS OBJECT ORIENTED PROGRAMMING

ITI Introduction to Computing II

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

Casting and polymorphism Enumeration

(11-1) OOP: Inheritance in C++ D & D Chapter 11. Instructor - Andrew S. O Fallon CptS 122 (October 29, 2018) Washington State University

Inheritance and Interfaces

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

Chapter 1: Object-Oriented Programming Using C++

Comp151. Inheritance: Initialization & Substitution Principle

POLYMORPHISM. Phone : (02668) , URL :

C++ Important Questions with Answers

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

PSAV MODEL: AGILE METHOD TO DESIGN HUMAN MACHINE INTERACTION FOR USER EXPERIENCE ACROSS THE PLATFORMS

Comp 249 Programming Methodology

Dr. Md. Humayun Kabir CSE Department, BUET

STRUCTURING OF PROGRAM

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

CHAPTER 6 Class-Advanced Concepts - Inheritance

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. COURSE CURRICULUM COURSE TITLE: OBJECT ORIENTED PROGRAMMING (Code: )

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

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Computer Programming Inheritance 10 th Lecture

ECE 3574: Dynamic Polymorphism using Inheritance

Constructor - example

AN OVERVIEW OF C++ 1

Use the template below and fill in the areas in Red to complete it.

STUDY ON INHERITANCE OF CLASSES

OBJECT ORIENTED PROGRAMMING USING C++

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. COURSE CURRICULUM COURSE TITLE: OBJECT ORINTED PROGRAMMING (Code: )

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

SSE2034: System Software Experiment 3 Spring 2016

CHAPTER16 INHERITANCE: REUSABILITY AND EXTENDABILITY CLASS INHERITANCE:GENERAL SYNTAX

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

Syllabus of C++ Software for Hands-on Learning: This course offers the following modules: Module 1: Getting Started with C++ Programming

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

CSC 330 Object-Oriented Software Design REUSABILITY

Object Oriented Programming. C++ 6 th Sem, A Div Ms. Mouna M. Naravani

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

CS11 Introduction to C++ Fall Lecture 7

DATABASE MIGRATION ANALYSIS FOR DATA ASSURANCE USING D2LOADER

ICT TOOL - TO CALCULATE MORPHOLOGICAL CHARACTERISTICS OF WATERSHED

Object Oriented Programming

B.Sc. (Hons.) Computer Science I B.Sc. (Hons.) Electronics. (i) Runtime polymorphism and compile time polymorphism

Basics of Object Oriented Programming. Visit for more.

CSE 303 Lecture 23. Inheritance in C++ slides created by Marty Stepp

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Class CSE F. *slides are from CSE S at SKKU & at MIT

Inheritance in Programming Languages

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design

OOP. Unit:3.3 Inheritance

A SHORT COURSE ON C++

Content. Inheritance 6. Object Oriented Programming

END TERM EXAMINATION

ECE 462 Object-Oriented Programming using C++ and Java Design Issues and Multiple Inheritance in C++

Lecture 6. Inheritance

What is Inheritance?

7. C++ Class and Object

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

A NOVEL ALGORITHM FOR CLEANICAL ROUTINE USING SYSTEM INTEGRATION OF PACS AND CBIR

Object Oriented Programming: Inheritance Polymorphism

SARDAR RAJA COLLEGE OF ENGINEERING

Overview. OOP: model, map, reuse, extend. Examples of objects. Introduction to Object Oriented Design

Transcription:

International Journal of Computer Science Engineering and Information Technology Research (IJCSEITR) ISSN(P): 2249-6831; ISSN(E): 2249-7943 Vol. 7, Issue 1, Feb 2017, 21-34 TJPRC Pvt. Ltd. INHERITANCE IN OBJECT ORIENTED PROGRAMMING EASIEST WAY TO TEACH AND LEARN INHERITANCE IN C++ ABSTRACT TWINKLE PATEL Department of Computer Science and Engineering, ITM Universe, Vadodara, Gujarat, India When we are talking about Object-Oriented programming, the first strike of everyone s mind is object and class. Object and Class are core concepts to build Object-Oriented programming. C++ have some basic features: simple, easy to use, portable, syntax based, object based, robust, platform dependent and much built in libraries. There are some other basic features of Object Oriented Programming: Data abstraction, Encapsulation, Dynamic Binding, Inheritance and Polymorphism. Here we will see how to teach inheritance in C++ by different examples. KEYWORDS: Inheritance, Object, Base Class, Derived Class, Private, Protected, Public & Construdtor Received: Dec 20, 2016; Accepted: Jan 20, 2017; Published: Jan 25, 2017; Paper Id.: IJCSEITRFEB20174 INTRODUCTION Inheritance is a basic feature of Object Oriented Programming language. It contains different classes; in which object of one class acquiring some or all property of another class. It is nothing but a hierarchical classification. In it we are creating one class that is base class, as we all know C++ supports reusability concept, so reusing the properties of that class to another class, which is derived class, or child class. You can also called base class as old class. And derived class is also known as sub-class [1]. Original Article For example, a parrot is a type of a flying bird; also an eagle is a type of a flying bird; a kiwi and penguin are type of a flightless bird. And these two flying and flightless birds are type of Birds. So here Bird is a base class as shown in figure 1. Figure 1: Basic Structure of Inheritance Flying Birds and Flightless Birds are child class or derived class of base class Bird. The Parrot and Eagle both are of grandchild class of class Bird. But flying bird is a base class of class Parrot and Eagle. Kiwi and penguin are child class of base class flightless bird. www.tjprc.org editor@tjprc.org

22 Twinkle Patel Syntax of Inheritance class Derived Class Name : access-specifier Base Class Name //Body of Derived Class We have different access-specifiers with different role: public, private and protected. If you want to match with our example, then you can see it works like below syntax. Assume only two classes are there, Bird and Flying Bird then syntax should be like this: class Bird class Flying : public Bird To better understand of Inheritance we have different types of it. It will add additional features and helpful in many arenas. TYPES OF INHERITANCE Inheritance has various forms, which could be helping you to build much extensible and huge application [5]. You can apply any type of inheritance based on your requisites. Different forms of inheritance: Single Inheritance Multiple Inheritance Multilevel Inheritance Hierarchical Inheritance Hybrid Inheritance In this paper we will see all types of inheritance with syntax and example. Single Inheritance The derived class with single base class is Single Inheritance from figure 2. In single inheritance, child class can acquire features of its parent class based on access-specifier. In this figure you can see two classes: shape and circle. Shape is base class of deriving class circle. Syntax of it: class Base Class Name Impact Factor (JCC): 7.1293 NAAS Rating: 3.76

Inheritance in Object Oriented Programming Easiest Way to Teach and Learn Inheritance in C++ 23 class Derived Class Name: access-specifier Base Class Name Figure 2: Single Inheritance #include<iostream> using namespace std; class Shape Shape() cout<<"shape Called ; cout<<endl; class Circle: public Shape Circle() cout<<"circle called"; cout<<endl; int main() www.tjprc.org editor@tjprc.org

24 Twinkle Patel Circle cobj; return 0; You will get output after compiling and executing this program: Shape Called Circle Called As we know when we create object of any class; constructor of that class automatically executes. As here the concept of inheritance means area of relationship between different classes at different level classes. Although the object of class Circle is created, we can able to execute constructor of parent class of it also. But you have to remember the order of it. Even though when you create object of derived class, it will first execute the constructor of its base class, then itself constructor print. Here shape is parent class, even though we create object of class circle it will execute shape first followed by circle. Multiple Inheritance The derived class with several base classes is multiple inheritances. A derived class contains not only feature of single class but more than one class. For example, child has some features from his father and some features from his mother. So he acquires properties of both. As in figure 3 Syntax of It class BaseClassName1 class BaseClassName2 class BaseClassName3 class Derived Class Name: access-specifier BaseClassName1, access-specifier Base-ClassName2, access-specifier BaseClass-Name3 Impact Factor (JCC): 7.1293 NAAS Rating: 3.76

Inheritance in Object Oriented Programming Easiest Way to Teach and Learn Inheritance in C++ 25 Figure 3: Multiple Inheritance #include<iostream> using namespace std; class Circle Circle() cout << "Circle's constructor called" << endl; class Square Square() cout << "Square's constructor called" << endl; class Rectangle Rectangle() cout << "Rectangle's constructor called" << endl; www.tjprc.org editor@tjprc.org

26 Twinkle Patel class Color: public Circle, public Square, public Rectangle Color() cout << "Color's constructor called" << endl; int main() Color cobj; return 0; You will get output after compiling and executing this program: Circle's constructor called Square s constructor called Rectangle s constructor called Color s constructor called As we can see here we derivate all classes by public access Specifiers. The Base classes Circle, Square and Rectangle have a constructor in their own classes and also in derived class. When we create object of derived class Color it prints output in a specific order as you can see. So class Color inherits the property from its parent class. Multilevel Inheritance The deriving class inherits the property from another derived class this feature is known as multilevel inheritance. The Syntax of It class BaseClass1 Impact Factor (JCC): 7.1293 NAAS Rating: 3.76

Inheritance in Object Oriented Programming Easiest Way to Teach and Learn Inheritance in C++ 27 class DerivedClass1: access-specifier BaseClass1 class DerivedClass2: access-specifier DerivedClass1 The Shape class is a base class; Circle is also a base class of class Color but child class of class Shape. Color is derived class of class Shape and class Circle as shown in figure 4 #include<iostream> using namespace std; class Shape Figure 4: Multilevel Inheritance Shape() cout << "Shape's constructor called" << endl; class Circle: public Shape Circle() cout << "Circle's constructor called" << endl; www.tjprc.org editor@tjprc.org

28 Twinkle Patel class Color: public Circle Color() cout << "Color's constructor called" << endl; int main() Color cobj; return 0; You will get output after compiling and executing this program: Shape's constructor called Circle's constructor called Color s constructor called Hierarchical Inheritance The feature of one class inherited by more than one class. There are the possibilities in which you can find feature of base class in more than one derived class. Syntax of it is class BaseClass1 class DerivedClass1: access-specifier BaseClass1 Impact Factor (JCC): 7.1293 NAAS Rating: 3.76

Inheritance in Object Oriented Programming Easiest Way to Teach and Learn Inheritance in C++ 29 class DerivedClass2: access-specifier BaseClass1 class DerivedClass3: access-specifier BaseClass1 This Concept is More Helpful in Projects of IT Sectors Figure 5: Hierarchical Inheritance The Circle class, Square class and Rectangle class are derived class from base class Shape as in figure 5 #include<iostream> using namespace std; class Shape Shape() cout << "Shape's constructor called" << endl; class Circle: public Shape Circle() www.tjprc.org editor@tjprc.org

30 Twinkle Patel cout << "Circle's constructor called" << endl; class Square: public Shape Square() cout << "Square's constructor called" << endl; class Rectangle: public Shape Rectangle() cout << "Rectangle's constructor called" << endl; int main() Rectangle robj;//print first Square sobj; //print second after rectangle Circle cobj; //print third after square return 0; You will get output after compiling and executing this program: Impact Factor (JCC): 7.1293 NAAS Rating: 3.76

Inheritance in Object Oriented Programming Easiest Way to Teach and Learn Inheritance in C++ 31 Shape's constructor called Rectangle's constructor called Shape's constructor called Square's constructor called Shape's constructor called Circle's constructor called In this example we can see there is no dependency among derived classes. Hybrid Inheritance This mechanism provides all flexibility means it is the mixture of two or more previous forms of inheritance Figure 6: Hybrid Inheritance #include<iostream> using namespace std; class Shape Shape() cout << "Shape's constructor called" << endl; class Square: public Shape www.tjprc.org editor@tjprc.org

32 Twinkle Patel Square() cout << "Square's constructor called" << endl; class Rectangle: public Shape Rectangle() cout << "Rectangle's constructor called" << endl; class Color: public Square, public Rectangle Color() cout << "Color's constructor called"; cout << endl; int main() Color cobj; return 0; You will get output after compiling and executing this program: Impact Factor (JCC): 7.1293 NAAS Rating: 3.76

Inheritance in Object Oriented Programming Easiest Way to Teach and Learn Inheritance in C++ 33 Shape's constructor called Square's constructor called Shape's constructor called Rectangle's constructor called Color's constructor called CONCLUSIONS This paper presents an idea about inheritance and how to teach inheritance in classroom. It is also useful to learn for students by own self. Here I also covered idea about different types of forms of Inheritance with example. So that every one can understand easily. REFERENCES 1. E Balagurusamy, Object Oriented Programming with C++, 6 th Ed.,New Delhi, TATA McGraw-Hill. 2. Krishnaprasad Thirunarayan, Inheritance in Programming Languages 3. Budd, T. (2002) Introduction to Object-Oriented Programming, Third Edition, Addison-Wesley. 4. Markku Sakkinen, Inheritance and other main pinciples of C++ and other object oriented language,1992 5. Robert Lafore, Object Oriented Programming in Turbo C++, 4 th Ed www.tjprc.org editor@tjprc.org