Introduction to Object- Oriented Programming

Similar documents
Fortran classes and data visibility

Reusing this material

Overloading, abstract classes, and inheritance

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

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

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

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

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

CMSC 132: Object-Oriented Programming II

Chapter 11 Object and Object- Relational Databases

Software Development. Modular Design and Algorithm Analysis

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

Advanced Programming Using Visual Basic 2008

Testing Object-Oriented Software. COMP 4004 Fall Notes Adapted from Dr. A. Williams

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


CPS 506 Comparative Programming Languages. Programming Language

OBJECT ORİENTATİON ENCAPSULATİON

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Data Abstraction. Hwansoo Han

CS11 Introduction to C++ Fall Lecture 7

What is an algorithm?

What are the characteristics of Object Oriented programming language?

CSE 307: Principles of Programming Languages

Introduction to Object-Oriented Programming

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Chapter 5 Object-Oriented Programming

Object-Oriented Programming Concepts

Message-Passing Programming with MPI. Message-Passing Concepts

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

Chapter 1: Object-Oriented Programming Using C++

Chapter 12. OOP: Creating Object- Oriented Programs. McGraw-Hill. Copyright 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.

PROGRAMMING LANGUAGE 2

1. Write two major differences between Object-oriented programming and procedural programming?

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

C++ Inheritance and Encapsulation

Object-Oriented Design

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

Concepts of Programming Languages

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

9 Working with the Java Class Library

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

C++ Inheritance and Encapsulation

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

CS260 Intro to Java & Android 03.Java Language Basics

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

Lecture 7: Data Abstractions

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Object Orientated Analysis and Design. Benjamin Kenwright

The major elements of the object-oriented model

Today s lecture. CS 314 fall 01 C++ 1, page 1

Data Structures (list, dictionary, tuples, sets, strings)

Object oriented programming Concepts

CHAPTER 5 GENERAL OOP CONCEPTS

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology


Dot and Scope Resolution Operator

CS260 Intro to Java & Android 02.Java Technology

Chapter 2: Java OOP I

Programming, numerics and optimization

Java Object Oriented Design. CSC207 Fall 2014

COURSE 2 DESIGN PATTERNS

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

SEEM4570 System Design and Implementation. Lecture 11 From Design to Implementation

Message Passing Programming. Modes, Tags and Communicators

INHERITANCE: EXTENDING CLASSES

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

EMBEDDED SYSTEMS PROGRAMMING OO Basics

Non-Blocking Communications

Compilation of Object Oriented Languages Tik Compilers Seminar

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Chapter 2. Building Multitier Programs with Classes The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

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

CSI33 Data Structures

Programming II (CS300)

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370

#include <iostream> #include <cstdlib>

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

Outline. Outline. 1 Chapter 2: Data Abstraction

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

ITI Introduction to Computing II

Object Oriented Programming. Solved MCQs - Part 2

Classes, subclasses, subtyping

Concepts of Object Oriented Programming

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

Building Multitier Programs with Classes

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

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

JAVA GUI PROGRAMMING REVISION TOUR III

JAVA MOCK TEST JAVA MOCK TEST II

Reusing this material

Programmazione. Prof. Marco Bertini

Chapter 8: Class and Method Design

Advanced OpenMP. Memory model, flush and atomics

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Unit - IV CHAPTER - 13 INTRODUCTION TO OOP WITH C++ Part 1 Choose the best answer


Example: Fibonacci Numbers

Transcription:

Introduction to Object- Oriented Programming Reusing this material This work is licensed under a Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International License. http://creativecommons.org/licenses/by-nc-sa/4.0/deed.en_us This means you are free to copy and redistribute the material and adapt and build on the material under the following terms: You must give appropriate credit, provide a link to the license and indicate if changes were made. If you adapt or build on the material you must distribute your work under the same license as the original. Note that this presentation may contain images owned by others. Please seek their permission before reusing these images. 1

Procedural programming Traditional scientific programming languages (i.e. C, Fortran) are procedural Programs constructed from functions/subroutines/procedures Modularity and re-use of operations achieved through grouping in functions Data scoping generally based on function scope Single function required (main), better practise to have as many functions as match distinct operations in the program Generally no explicit link between data and functions Data accessible and modifiable by functions at will Object-oriented programming Object-oriented programming (OOP) Large programs often become hard to maintain and extend Complex interdependencies makes development and maintenance difficult Packaging functionality and data into groups and only exposing the minimum amount of this to other parts of the program can help address this issue Abstract datatypes (ADTs) are attractive programming idea Group code and data together Hide data and only access through associated code Provide defined interfaces and access mechanisms Hide users of data from details 2

OOP Can implement ADTs in procedural lanugages Derived datatypes/structures Doesn t force hiding of data Don t allow easy re-use for different datatypes Object concept designed to allow fully functional implementation of ADTs Allow better control of visibility and access Allow better re-use of common code and extension for different data types or functionality OOP - Encapsulation A class is a specification of an ADT Blueprint of the ADT, definition of data and implementation of procedures An instance is a runtime instantiation of the ADT Actual ADT with data in it Can have as many instances as the program requires Instance also known as an Object Class name = Bob Smith officenumber = 8 name = Sarah Wilson officenumber = 9 name = Andy Paul officenumber = 254 name = Mia Patton officenumber = 50 Runtime Instances/Objects 3

Object Creation Object creation at runtime name = Bob Smith officenumber = 8 p = new (); name = Andy Paul officenumber = 254 name = Sarah Wilson officenumber = 9 name = Mia Patton officenumber = 50 7 Methods - Encapsulation Objects used by calling methods methods in OO => functions/subroutines in procedural programming they can take arguments and return results Cannot be called in isolation need an instance (object) (not strictly true, many languages have static methods) p name = Bob Smith officenumber = 8 p.setofficenumber(2) p.moveoffice(); setofficenumber(2); 8 4

Methods - Encapsulation Method implementation specified in class Classes can have method specifications but not implementations (abstract class) Abstract classes need to be implemented in other classes to be used Methods can access class data i.e. officenumber not visible outside class // public void setofficenumber(integer number) { officenumber = number; return; } Class hierarchy and relationships Individual class functionality not particularly useful Power of OOP comes from the relationships between multiple classes Controlling code and data re-use Defining hierarchy/relationships between data Composition: has-a Objects can contain other objects within them Inheritance: is-a-type-of Objects can be built on other objects (extend) Allows for multiple version of some functionality, only changing or adding what is required for the different version Abstract classes can define object to be implemented with no actual implementation provided 5

Composition Class containing an object can use the objects methods through that object Building corridors:array of Corridor numberofcorridors: Integer addcorridor(corridor): Boolean removecorridor(corridor): Boolean getnumberofcorridors(): Integer getcorridor(integer i): Corridor getnumberofrooms(): Integer Corridor rooms:array of numberofrooms: Integer add(): Boolean remove(): Boolean getnumberofrooms(): Integer get(integer i): Inheritance Subclass inherits (can use) superclass functions and data Can add new functions and data Manager is a subclass of is a superclass of Manager Manager add() remove() move() Building corridors:array of Corridor numberofcorridors: Integer addcorridor(corridor): Boolean removecorridor(corridor): Boolean getnumberofcorridors(): Integer getcorridor(integer i): Corridor getnumberofrooms(): Integer Corridor rooms:array of numberofrooms: Integer add(): Boolean remove(): Boolean getnumberofrooms(): Integer get(integer i): 6

Polymorphism Can re-define superclass functions (override/subtyping) // public void print() { write( :, name); return; } //Manager public void print() { write( Manager:, name); return; } Mixing objects OO languages allow mixing of subclasses: arr[] = new [2]; arr[0] = new ( Adrian Jackson ); arr[1] = new Manager( David Henty ); for (int i = 0; i < numpeople; i++) { arr[i].print(); } 7

Construction/Destruction Special function/method to setup an object (constructor) Called on object creation Ensures that object is created in desired state Likewise, possible to provide a method that is called when an object is destroyed (destructor) Enable cleaning up when object is no longer needed Summary Object-oriented programming groups data and functionality together Safety of data can be ensured by controlling how data is accessed and updated (encapsulation) Definition and instance of data separated into class and object Class defines the data and functions that can operate on that data Object is a specific instance of the class Can have many, separate and distinct, objects from the same class Functionality can be re-used when creating new objects Composition and inheritance Enable specification/provision of interfaces for others to use or implement 8

Exercise This is purely a thought exercise Have a look at the percolate code. Think about how you could split that into classes Where could different functionality go? How would it then be used? What needs to know/access what? Where could things be likely to change? What things need to hidden for safety? 9