Inheritance, and Polymorphism.

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

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

CSC1322 Object-Oriented Programming Concepts

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

C++ Inheritance and Encapsulation

Friend Functions, Inheritance

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

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Java Object Oriented Design. CSC207 Fall 2014

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

Polymorphism Part 1 1

What are the characteristics of Object Oriented programming language?

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

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

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Data Abstraction. Hwansoo Han

Chapter 1: Object-Oriented Programming Using C++

Get Unique study materials from

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

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

Data Structures (INE2011)

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

Object-Oriented Concept

Inheritance

Polymorphism CSCI 201 Principles of Software Development

Introduction Of Classes ( OOPS )

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

8. Object-oriented Programming. June 15, 2010

Midterm Exam 5 April 20, 2015

Object-Oriented Programming (OOP) Fundamental Principles of OOP

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

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

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 Design Final Exam (From 3:30 pm to 4:45 pm) Name:

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

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

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Module 10 Inheritance, Virtual Functions, and Polymorphism

INHERITANCE - Part 1. CSC 330 OO Software Design 1

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Lecture 5: Inheritance

CIS 190: C/C++ Programming. Lecture 11 Polymorphism

JAVA MOCK TEST JAVA MOCK TEST II

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

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

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Chapter 13. Object Oriented Programming

Written by John Bell for CS 342, Spring 2018

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

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

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Inheritance and Polymorphism

ITI Introduction to Computing II

CS-202 Introduction to Object Oriented Programming

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

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

This examination has 11 pages. Check that you have a complete paper.

Data Structures and Other Objects Using C++

Inheritance and Encapsulation. Amit Gupta

CS11 Introduction to C++ Fall Lecture 7

Passing arguments to functions by. const member functions const arguments to a function. Function overloading and overriding Templates

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:

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance?

ITI Introduction to Computing II

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

CLASSES AND OBJECTS IN JAVA

CPSC 427: Object-Oriented Programming

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

Object Oriented Software Design II

Computer Science II CSci 1200 Lecture 24 C++ Inheritance and Polymorphism

CGS 2405 Advanced Programming with C++ Course Justification

CS 251 Intermediate Programming Inheritance

PROGRAMMING LANGUAGE 2

Fast Introduction to Object Oriented Programming and C++

Object Oriented Software Design II

10. Object-oriented Programming. 7. Juli 2011

CMSC 132: Object-Oriented Programming II

Constructor - example

G Programming Languages - Fall 2012

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Chapter 14 Abstract Classes and Interfaces

Ministry of Higher Education Colleges of Applied Sciences Final Exam Academic Year 2009/2010. Student s Name

9/21/2010. Based on Chapter 2 in Advanced Programming Using Visual Basic.NET by Bradley and Millspaugh

Namespaces and Class Hierarchies

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

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

ECE 462 Midterm Exam 1. 10:30-11:20AM, September 21, 2007

CS1150 Principles of Computer Science Objects and Classes

CS304 Object Oriented Programming Final Term

C++ Modern and Lucid C++ for Professional Programmers


VALLIAMMAI ENGINEERING COLLEGE

C++ (classes) Hwansoo Han

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Transcription:

Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to things found in the real world. This program paradigm provides three important mechanisms: Encapsulation, Inheritance, and Polymorphism. 1 Encapsulation Encapsulation hides the members of a class from the users. One of the advantages of this information hiding is to minimize the impact on the users code whenever the modifications on the class have to be made. 2 Inheritance 2.1 Definition A mechanism by which one class acquires the properties both data and functions of another class. The following names are used to describe inheritance. Base class Parent class Super class Derived class Child class Subclass 2.2 Member variables Child class inherits all member variables and member functions of its parent class/classes plus its own members. Therefore, an object of child class requires more memory space than an object of its parent class. If a member variable of a parent class is defined as private (it is true in most cases), its child classes cannot directly access the member, even though the child classes possess the asset. They have to use the public functions defined in the parent class to access or manipulate parent s member variable. Sometimes, protected is used for the member variables in a parent class to allow child classes to access them. 2.3 Functions Both parent and child classes can define two functions with the exactly same names and parameter list (or prototypes). In this case, we say the function defined in the child class overrides the function defined in the parent class.

2.4 Constructors When a child object is being created, one of the constructors in its parent class will be called first through an initializer in its child constructors, then followed by the child constructor. For example: class P P() cout << "Default constructor P" << endl; P(int n) cout << "Parameterized constructor P" << endl; ~P() cout << "Destructor P" << endl; class C:public P C(int m):p(m) cout << "Parameterized constructor C" << endl; ~C() cout << "Destructor C" << endl; int main() C o(24); return 0; The output of the program would be: Parameterized constructor P Parameterized constructor C Destructor C Destructor P If a child class has a number of member variables that are of types of other classes, the constructor of its parent class will be called first, then followed by the constructors of the member classes in the order the member variables appear in the child class. The order of initializers doesn t matter. However, which constructors will be called depends on how the initializers are specified. If no initializers are specified for some members, the default constructors for the members will be called. For example, Page 2

class P; class A; class B; class C; class X:public P X(const A & a, const B & b ): cc( ), aa(a) bb = b; cout << "X constructor" << endl; ~X() cout << "X destructor" << endl; private: A aa; B bb; C cc; In the example above, class P is a parent class of class X. Classes A, B, and C are the types for member variables aa, bb, cc. When an object of class X is being created, its parent class P s default constructor will be called first, then A s copy constructor, B s default constructor, C s default constructor, finally the body of X s constructor. The order of the initializers doesn t matter at all, but the visible initializers determine which constructors should be called. 3 Polymorphism 3.1 Definition Polymorphism means multiple forms. In programming languages, it means a variable may have a different type in a certain context. 3.2 Usage To use polymorphism, the parent type variable must be declared as either pointer or reference. Also, virtual keyword must be used in the member function of the parent class. The member function is also overridden by child classes. For example, class Person Page 3

virtual void print(); class Student void print(); int main() Person * p; Person * ps = new Person(); Student * st = new Student(); p = ps; p->print(); p = st; p->print(); //Person s print() will be called. //Student s print() will be called. delete ps; delete st; return 0; 3.3 Virtual Destructor Virtual destructor is used to trigger a call to its parent s destructor when a child object is being destroyed. Without the keywork virtual in parent destructor, only the child destructor will be called, which is not desired. 4 Abstract Class 4.1 Definition A class is said to be an abstract class if it has one or more pure virtual functions. A pure virtual function is one that doen t have a function body and must be overridden by child class. It has the syntax =0 in the member function declaration. An abstract class is intended to be inherited by child classes only. It cannot be used to create concrete objects. If a class has pure member functions exclusively, without any data member variables, the class is called pure abstract class. Pure abstract class in C++ is Page 4

similar to interface in Java programming language. It is a way to enforce all child class that inherit from the same parent class to implement the same set of member functions declared in its parent class. That is, all child classes that inherit from the same parent have the same behaviors. For example below, all concrete shapes such as rectangles, triangles, circle, etc should have the same behavior that calculates their areas, even though the formulas for calculating areas are different. #include <iostream> using namespace std; //Abstract class class Shape Shape() //Pure virtual function virtual double calculatearea() = 0; //Concrete class class Rectangle : public Shape Rectangle(double h, double w) height = h; width = w; double calculatearea() return height * width; private: double height; double width; int main() Shape * s; Rectangle * r = new Rectangle(3, 4); s = r; cout << "The area is " << r->calculatearea() << endl; cout << "The area is " << s->calculatearea() << endl; return 0; //Output: //The area is 12 Page 5

//The area is 12 //Press any key to continue... Page 6