Object-Oriented Concept

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

Inheritance, and Polymorphism.

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

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

Inheritance and object compatibility

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

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

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

Java Object Oriented Design. CSC207 Fall 2014

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

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

Polymorphism. Zimmer CSCI 330

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

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

Data Structures and Other Objects Using C++

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

Midterm Exam 5 April 20, 2015

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

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

C++ Inheritance and Encapsulation

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

Polymorphism and Interfaces. CGS 3416 Spring 2018

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

Polymorphism Part 1 1

COMP 249: Object Oriented Programming II. Tutorial 2: Intro to Inheritance

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

CS11 Introduction to C++ Fall Lecture 7

CS250 Final Review Questions

Patterns for polymorphic operations

C++ Important Questions with Answers

VIRTUAL FUNCTIONS Chapter 10

Framework Fundamentals

ECE 3574: Dynamic Polymorphism using Inheritance

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

Reusable and Extendable Code. Composition Example (1) Composition Example (2) CS183-Su'02-Lecture 8 17 June by Eric A. Durant, Ph.D.

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

Inheritance, Polymorphism and the Object Memory Model

Object Oriented Software Design II

Chapter 15. Polymorphism and Virtual Functions. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Object Oriented Software Design II

Chapter 1: Object-Oriented Programming Using C++

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

Object Oriented Programming with c++ Question Bank

CMSC202 Computer Science II for Majors

Inheritance and Polymorphism

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

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

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

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

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

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:

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

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

Function Overloading and Overriding this keyword static members inline functions Passing arguments by values and reference

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

Advanced C++ Topics. Alexander Warg, 2017

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

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


Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Concepts of Object Oriented Programming

Financial computing with C++

Object Oriented Programming. Solved MCQs - Part 2

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

COMP6771 Advanced C++ Programming

QUESTIONS FOR AVERAGE BLOOMERS

Data Abstraction. Hwansoo Han

Inheritance, Polymorphism, and Interfaces

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Chapter 5 Object-Oriented Programming

AN OVERVIEW OF C++ 1

OVERRIDING. 7/11/2015 Budditha Hettige 82

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

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

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

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

CMSC 132: Object-Oriented Programming II

C++ (classes) Hwansoo Han

INHERITANCE: EXTENDING CLASSES

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

Inheritance. Transitivity

Inheritance. A mechanism for specialization A mechanism for reuse. Fundamental to supporting polymorphism

Absolute C++ Walter Savitch

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

C++ Quick Guide. Advertisements

Programming in C++: Assignment Week 6

15: Polymorphism & Virtual Functions

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

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

CSS 343 Data Structures, Algorithms, and Discrete Math II. Polymorphism. Yusuf Pisan

Object-Oriented Programming

Static and Dynamic Behavior לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

VALLIAMMAI ENGINEERING COLLEGE


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

Instance Members and Static Members

index.pdf January 21,

Polymorphism. Arizona State University 1

Transcription:

Object-Oriented Concept Encapsulation ADT, Object Inheritance Derived object Polymorphism Each object knows what it is

Polymorphism noun, the quality or state of being able to assume different forms - Webster An essential feature of an OO Language It builds upon Inheritance

Polymorphism class Dog { virtual void bark() = 0; class 불독 : public Dog { void bark() {std::cout << 왈왈 << endl;} class 진돗개 : public Dog { void bark() {std::cout << 멍멍 << endl;} class 치와와 : public Dog { void bark() {std::cout << 깽깽 << endl;} CLIENT CODE void do_bark(dog *dog) { dog->bark(); } int main() { 불독 Bulldog; 진돗개 Jindog; 치와와 Chiwawa; } do_bark(&bulldog); do_bark(&jindog); do_bark(&chiwawa); OUTPUT 왈왈멍멍깽깽

Static Binding When the type of a formal parameter is a parent class, the argument used can be: the same type as the formal parameter, or, any derived class type. Static binding is the compile-time determination of which function to call for a particular object based on the type of the formal parameter When pass-by-value is used, static binding occurs

Dynamic Binding Is the run-time determination of which function to call for a particular object of a derived class based on the type of the argument Declaring a member function to be virtual instructs the compiler to generate code that guarantees dynamic binding Dynamic binding requires pass-by-reference

Virtual Functions Virtual Functions overcome the problem of run time object determination Keyword virtual instructs the compiler to use late binding and delay the object interpretation How? Define a virtual function in the base class. The word virtual appears only in the base class If a base class declares a virtual function, it must implement that function, even if the body is empty Virtual function in base class stays virtual in all the derived classes It can be overridden in the derived classes But, a derived class is not required to re-implement a virtual function. If it does not, the base class version is used

Pure Virtual Function class Dog { virtual void bark() = 0; void bark() {std::cout <<?? << endl;} class 불독 : public Dog { void bark() {std::cout << 왈왈 << endl;} class 진돗개 : public Dog { void bark() {std::cout << 멍멍 << endl;} class 치와와 : public Dog { void bark() {std::cout << 깽깽 << endl;} Overriding

Virtual Function class Dog { virtual void bark() = 0; class 불독 : public Dog { void bark() {std::cout << 왈왈 << endl;} class 진돗개 : public Dog { void bark() {std::cout << 멍멍 << endl;} class 치와와 : public Dog { void bark() {std::cout << 깽깽 << endl;} CLIENT CODE int main() { Dog *dog = new Jindog; dog->bark(); } 치와와 Chiwawa; dog = &Chiwawa; dog->bark(); OUTPUT 멍멍왈왈 Dynamic Binding

Virtual Destructor class Dog { virtual void bark() = 0; class 불독 : public Dog { void bark() {std::cout << 왈왈 << endl;} class 진돗개 : public Dog { void bark() {std::cout << 멍멍 << endl;} class 치와와 : public Dog { void bark() {std::cout << 깽깽 << endl;} CLIENT CODE int main() { Dog *a = new 진돗개 ; 진돗개 *b = new 진돗개 ; } delete a; delete b; delete a : Call only 진돗개 s destructor delete b : Call 진돗개 s destructor & Dog s destructor

Virtual Destructor class Dog { virtual void bark() = 0; ~Dog( ) { class 불독 : public Dog { void bark() {std::cout << 왈왈 << endl;} class 진돗개 : public Dog { void bark() {std::cout << 멍멍 << endl;} class 치와와 : public Dog { void bark() {std::cout << 깽깽 << endl;} CLIENT CODE int main() { Dog *a = new 진돗개 ; 진돗개 *b = new 진돗개 ; } delete a; delete b;

Summary When you use virtual functions, compiler store additional information about the types of object available and created Polymorphism is supported at this additional overhead Important : virtual functions work only with pointers/references Not with objects even if the function is virtual If a class declares any virtual methods, the destructor of the class should be declared as virtual as well.

[Lab Practice #1] Area of the figure Input Type of figure (triangle or rectangle) Point Output: area for Rectangle $./figure Figure: triangle Point1: 0,0 Point2: 2,0 Point3: 2,2 Area: 2