Object Oriented Programming

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

Programming in C# Inheritance and Polymorphism

Lecture 5: Inheritance

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

Data Structures (INE2011)

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

C++ (classes) Hwansoo Han

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

CSI33 Data Structures

COP 3530 Discussion Session #2 Ferhat Ay

Inheritance (with C++)

Inheritance Basics. Inheritance (with C++) Base class example: Employee. Inheritance begets hierarchies. Writing derived classes

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

Data Abstraction. Hwansoo Han

ECE 3574: Dynamic Polymorphism using Inheritance

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

Abstraction and Encapsulation. Benefits of Abstraction & Encapsulation. Concrete Types. Using Typedefs to streamline classes.

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

Polymorphism Part 1 1

VALLIAMMAI ENGINEERING COLLEGE

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

CS105 C++ Lecture 7. More on Classes, Inheritance

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

Chapter 15. Object-Oriented Programming

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

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

Computer Programming Inheritance 10 th Lecture

Object-Oriented Programming (OOP) Fundamental Principles of OOP

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

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

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Inheritance, and Polymorphism.

What are the characteristics of Object Oriented programming language?

Inheritance (Deitel chapter 9)

VIRTUAL FUNCTIONS Chapter 10

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

COEN244: Class & function templates

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 in C++

INHERITANCE - Part 1. CSC 330 OO Software Design 1

Object Oriented Programming. Solved MCQs - Part 2

Introduction to Object-Oriented Programming

Babaria Institute of Technology Computer Science and Engineering Department Practical List of Object Oriented Programming with C

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

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

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

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

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Inheritance Motivation

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

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

Object-Oriented Programming

COMP6771 Advanced C++ Programming

Object Oriented Software Design II

Inheritance and Polymorphism

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Object Oriented Software Design II

Chapter 7. Inheritance

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

CPS 506 Comparative Programming Languages. Programming Language

2 ADT Programming User-defined abstract data types

IS0020 Program Design and Software Tools Summer, 2004 August 2, 2004 in Class

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

CS304 Object Oriented Programming Final Term

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

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

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

Object Oriented Paradigm

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

Inheritance, Polymorphism and the Object Memory Model

Problem: Given a vector of pointers to Shape objects, print the area of each of the shape.

COSC252: Programming Languages: Abstraction and OOP. Jeremy Bolton, PhD Asst Teaching Professor. Copyright 2015 Pearson. All rights reserved.

CLASSES AND OBJECTS IN JAVA

Object Oriented Programming with c++ Question Bank

Polymorphism. Contents. Assignment to Derived Class Object. Assignment to Base Class Object

Programming, numerics and optimization


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

Fast Introduction to Object Oriented Programming and C++

C++ Important Questions with Answers

CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS. MC

Short Notes of CS201

Object Oriented Programming CS250

CS304 Object Oriented Programming

CS11 Introduction to C++ Fall Lecture 7

Financial computing with C++

CS-202 Introduction to Object Oriented Programming

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

Object-Oriented Concept

CS201 - Introduction to Programming Glossary By

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

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Comp 249 Programming Methodology

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

Ch 14. Inheritance. May 14, Prof. Young-Tak Kim

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

Object-Oriented Programming, Iouliia Skliarova

Transcription:

OOP Object Oriented Programming Object 2 Object 1 Object 3 For : COP 3330. Object oriented Programming (Using C++) Object 4 http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar Objects: State (fields), Behavior (member functions), Identity Class : Blue print of an object. Data and behavior are strongly linked in OOP. Objects are responsible for their behavior. Example: Complex numbers, Rational numbers, Floating point numbers, all understand addition. OOP components Recap: ADTs Data Abstraction Information Hiding, ADTs Encapsulation Type Extensibility Operator Overloading Code Reuse Polymorphism Specify the meaning of the operations independent of any implementation/definition. Least common denominator of all possible implementations. Information Hiding: Do not expose unnecessary information. Two example classes Class Employee Two example classes Class Manager class Employee { Employee(string thename, float PayRate); string Name() const; float PayRate() const; float compute_pay(float hoursworked) const; string name; float payrate; class Manager { Manager(string thename, float PayRate); void set_manages(int n); string Name() const; float PayRate() const; float compute_pay(float hoursworked) const; string name; float payrate; int manages_n_employees; 1

Reuse Manager We have done unnecessary work to create Manager, which is similar to (and really is a is a") Employee. We can fix this using the OO concept of inheritance. We let a manager inherit from an employee. A manager gets all the data and functionality of an employee after inheritance. We can then add any new data and methods needed for a manager and redefine any methods that differ for a manager. class Manager : public Employee { // is a relationship Manager(string thename, float PayRate, int n); void set_manages(int n); int manages_n_employees; Methods of Manager have access to name, payrate because they were declared in Employee as "protected. More on : Access privileges. In a public inheritance: Public members are accessible to derived class. Protected members are accessible to derived class. These members are not accessible to the users of the base class. Private members are not accessible to derived class. Derive a new class (subclass) from an existing class (base class). Syntax: class classname : access-label base-class { } Access-labels = { public, private, protected } creates a hierarchy of related classes (types) which share code and interface. More Examples More Examples Base Class Derived Classes Person Student Shape Loan GradStudent UnderGradStudent Circle Triangle Rectangle Tetrahedron CarLoan HomeImprovementLoan MortgageLoan GradStudent Student UnderGradStudent Employee Non-Faculty Faculty Tenure Teaching Is a relationships. 2

: Subclass Code reuse derive GradStudent from Student (also adds fields/methods) Specialization: Customization derive bounded-stack from stack (by overriding/redefining push) Generalization: Factoring Commonality Avoid code-duplications (why?) Derived classes contain their base classes as subobjects. Manager object. string name; float payrate; int manages_n_employees; Employee object Functions in the derived may use members from the base. There is no requirement that the compiler lay out the base and derived parts of an object contiguously. Open-Closed principle in OOP A class must be defined to be used as a base class. A derived class can be used as a baseclass. Forward declarations are same for baseclasses as well as derived classes. class Manager; class Employee; class Manager: public employee; // Error The open/closed principle states that a class must be both open and closed. Open: means it has the ability to be extended Closed: means it cannot be modified other than by extension. An interesting paper: http://www.craiglarman.com/articles/the%20importance%20of%20being%20closed%20-%20larman%20-%20ieee%20software.pdf Open-Closed principle in OOP Once a class has been approved for use after having gone through code reviews, unit tests, and other qualifying procedures, you don't want to change the class very much, just extend it. Changing base class can complicate all derived classes. Example : Open-Closed Pr. Queue TestQueue Client (Composition) Subclass () DeltaBoundedQueue TestBoundedQueue 3

More on Virtual Methods A pointer to a derived class can always be used as a pointer to a base class when public inheritance is used. (But not vice-versa) Private base classes are different STL Containers which need to contain both base/derived classes should be made of pointers to base classes. Otherwise : Slicing problem. A base class must indicate which of its member functions it intends its derived classes to redefine. These member functions are defined as virtual in the base class. Example Base *bp; Derived d; bp = &d; bp->print(); // invokes class Base { int i; virtual void print() { cout << "i value is " << i << " inside object of type Base\n\n"; } class Derived: public Base { virtual void print() { cout << "i value is " << i << " inside object of type Derived\n\n"; } Dynamic Binding Allows invocation of general methods using a base class pointer. The fact that a reference or pointer might refer to either a base or derivedclass object is the key to dynamic binding. Allows easy extensibility. Dynamic Vs Static Binding Dynamic Vs Static Binding Static Binding: The compiler uses the type of the pointer to find out which method to call. Dynamic Binding: The decision is made at runtime. (uses virtual keyword) Static Binding Less time and space overhead. Inlining possible Dynamic Binding Extensibility Better code-reuse. 4

Dynamic Vs Static Binding Efficiency Vs Flexibility Static Binding More efficient Less time and space overhead, can use inlining. Dynamic Binding Flexible: Enables extension of behavior of a system easily. Virtual Functions Have a fixed interface. Derived implementations can change. Dispatched using object s dynamic type to select the appropriate method. Once Virtual, always virtual rule. Once a base-class defines a function as virtual, it remains virtual through out the inheritance hierarchy. An example base class Scoping rules // Item sold at an undiscounted price // derived classes will define various discount strategies class Item_base { friend std::istream& operator>>(std::istream&, Item_base&); friend std::ostream& operator<<(std::ostream&, const Item_base&); virtual Item_base* clone() const { return new Item_base(*this); } Item_base(const std::string &book = "", double sales_price = 0.0): isbn(book), price(sales_price) { } std::string book() const { return isbn; } // returns total sales price for a specified number of items // derived classes will override and apply different discount algorithms virtual double net_price(std::size_t n) const { return n * price; } // no work, but virtual destructor needed // if base pointer that points to a derived object is ever deleted virtual ~Item_base() { } // Always virtual. Why? (Hint: Static Vs Dynamic Binding) private: std::string isbn; // identifier for the item double price; // normal, undiscounted price In a public base class, public and protected members of the base class remain public and protected members of the derived class. Example: class circle: public point { circle c; //can call point::move(int,int) c.move(1,2); Scoping Rules. Out of scope of this class. Do not use. Is a Vs Has a Private derivation: public base class members are private in derived class. Example: class stack: private linkedlist { stack s; s.insert(1,2); // cannot call linkedlist::insert(int,int) where insert is public Protected derivation: public base class members are protected in derived class. Considered an Is a class relationship e.g.: An HourlyEmployee is a Employee A Convertible is a Automobile A class contains objects of another class as it s member data Considered a Has a class relationship e.g.: One class has a object of another class as it s data 5

OOP Shape Example Abstract Base class: Shape. rectangle shape triangle circle class Shape { Shape ( Point2d& position, Color& c) : center_(position), color_ (c) { virtual void rotate( double radians ) = 0; virtual bool draw(screen &) = 0; virtual ~Shape(void) = 0; void move(point2d& p) { _center = p; private: Point2d center_; Color color_; Public inheritance: Is A relationships C++ Shape example class Triangle: public Shape { Triangle( Point2d& p[3] ); virtual void rotate ( double radians ){ } virtual bool draw(screen &s) { virtual ~Triangle(void) { private: Point2d vertices[3]; Color color_; 6