Object Orientation. A Crash Course Intro

Similar documents
C++ Code Structure. Cooperating with the Compiler

A First Object. We still have another problem. How can we actually make use of the class s data?

Error Handling in C++

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CS24 Week 3 Lecture 1

Inheritance, and Polymorphism.

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

Programming II (CS300)

Object Oriented Programming

CSCE 156 Computer Science II

Object Oriented Programming

CS112 Lecture: Exceptions and Assertions

Chapter 1: Object-Oriented Programming Using C++

Chapter 4 Defining Classes I

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

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

Exceptions Programming 1 C# Programming. Rob Miles

Exceptions. Exceptions. Exceptional Circumstances 11/25/2013

CS112 Lecture: Exceptions. Objectives: 1. Introduce the concepts of program robustness and reliability 2. Introduce exceptions

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

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

CSCI 104 Inheritance. Mark Redekopp David Kempe

EL2310 Scientific Programming

Objectives. Introduce static keyword examine syntax describe common uses

Ch. 17: Linked Lists. Introduction to Linked Lists

Pointers. Developed By Ms. K.M.Sanghavi

1. Introduction to Concurrent Programming

Assumptions. History

memory_resource_ptr: A Limited Smart Pointer for memory_resource Correctness

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

University of Swaziland Department OfComputer Science Main Examination December 2016

Course Content. Objectives of Lecture 18 Black box testing and planned debugging. Outline of Lecture 18

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

Memory Leak. C++: Memory Problems. Memory Leak. Memory Leak. Pointer Ownership. Memory Leak

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Object Reference and Memory Allocation. Questions:

Introducing C++ to Java Programmers


CS1004: Intro to CS in Java, Spring 2005

Laboratorio di Tecnologie dell'informazione

COMP 2355 Introduction to Systems Programming

Programming Abstractions

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

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

Design Patterns and Frameworks Command

More on Objects in JAVA TM

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

OBJECT ORIENTED PROGRAMMING

INHERITANCE: EXTENDING CLASSES

EE219 Object Oriented Programming I Repeat Solutions for 2005/2006

10. Abstract Data Types

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

An Introduction to C++

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

9/19/2018 Programming Data Structures. Polymorphism And Abstract

Programming Assignment 2

Objects, Subclassing, Subtyping, and Inheritance

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Singly linked lists in C.

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CMSC 202 Midterm Exam 1 Fall 2015

Verification and Validation. Assuring that a software system meets a user s needs. Verification vs Validation. The V & V Process

Cpt S 122 Data Structures. Introduction to C++ Part II

Lab 12 Object Oriented Programming Dr. John Abraham

Automatic Testing of Sequential and Concurrent Substitutability

Structured bindings with polymorphic lambas

Chapter 7. Inheritance

ECE 462 Fall 2011, Third Exam

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

CS4411 Intro. to Operating Systems Exam 1 Fall points 9 pages

Chapter 7 Applets. Answers

Fundamentals of Programming. Lecture 19 Hamed Rasifard

CA341 - Comparative Programming Languages

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

JAVA MOCK TEST JAVA MOCK TEST II

User Defined Types. Babes-Bolyai University Lecture 06. Lect Phd. Arthur Molnar. User defined types. Python scope and namespace

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

Array Based Lists. Collections

Objects and Classes. Chapter 8

VIRTUAL FUNCTIONS Chapter 10

16-Dec-10. Consider the following method:

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Subclassing for ADTs Implementation

// constructor: takes a String as parameter and creates a public Cat (String x) { name = x; age = 0; lives = 9; // cats start out with 9 lives }

Using the Xcode Debugger

Practicum 5 Maps and Closures

C-style casts Static and Dynamic Casts reinterpret cast const cast PVM. Casts

Lecture 2, September 4

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

COMP-202 Unit 9: Exceptions

Chapter 12: How to Create and Use Classes

COMP-202 Unit 9: Exceptions

Transcription:

Object Orientation A Crash Course Intro

What is an Object? An object, in the context of objectoriented programming, is the association of a state with a set of behaviors. State: its fields, or member variables Behaviors: its associated methods, or member functions.

A First Object using namespace std; // a very basic C++ object class Person { public: string name; int age; }

A First Object using namespace std; // a very basic C++ object class Person { public: string name; int age; } Note this is not a properlydesigned class according to objectorientation principles.

Analysis

Analysis Object-orientation is all about recognizing the different actors at work in the system being modeled. First, the different data available within the system are organized appropriately. Secondly, functionalities relating to the state of data and its management are bound to that data.

Analysis Object-orientation is all about recognizing the different actors at work in the system being modeled. These objects ( actors ) may then interact with other objects through wellformed, bounded relationships.

Analysis For now, let s examine how we should look at individual objects the actors in a program. Each object should be composed of a set of related data that represents some logical unit within the program. In this case, this would be our Person class.

Analysis 1.Inputs: what does our object need in order to be properly formed? Both from outside, and for internal representation? 2.Outputs: what parts of our object are needed by the outside world? What might some other part of the program request from it?

Analysis 1.Constraints: should our object have limitations imposed on it, beyond those implied by the language we re using? Some of our internal state variables (fields) may allow values which make no sense in the context of what our object represents.

Analysis 1.Assumptions: Are we assuming something in our construction of the class which might have to change later? We wish to minimize these (in the long run at least) as much as possible.

A First Object // a very basic C++ object class Person { public: string name; int age; } What is bad about the design of our current Person class?

Encapsulation Encapsulation refers to the idea that an object should protect and manage its own state information. In a way, each object should behave like its own entity. Data security is enforced by the object definition itself. This allows a programmer to make ensure that the data being represented is always in a consistent form.

Encapsulation Generally speaking, objects should never make their fields public. A public field can be accessed and modified at any time from code that has a reference to the object, which can invalidate its internal state.

Encapsulation Note that encapsulation is motivated by the desire to enforce constraints on our object. How can we make sure our object is always in a proper, well-formed state if we can t limit how others modify it?

Encapsulation In object-oriented languages, objects may set their fields to be inaccessible outside of the class. To do this, one may use the access modifier private. This restricts access to the private field or method to only code in the class in which said field or method is defined.

A First Object // a very basic C++ object class Person { public: string name; int age; } So, instead of marking the fields as public

A First Object // a very basic C++ object class Person { private: string name; int age; } We want to mark them as private.

A First Object // a very basic C++ object class Person { private: string name; int age; } This creates a new problem, though. How can we initialize our object?

Initialization By default, when no constructor exists for a class, C++ creates a default constructor with no internal code. Note: this default constructor will initalize nothing within the class. Java s default constructor acts differently, setting values to zeros and nulls. Typically, we will need to create our own constructors to ensure the class is properly initialized.

A First Object // a very basic C++ object class Person { public: Person(string name, int age); } private: string name; int age;

A First Object Person::Person(string name, int age) { this->name = name; this->age = age; } Something interesting here: note the use of ->. this is a pointer, and it refers to the instance of the class upon which the constructor/function has been called.

Initialization Once one constructor has been coded for a class, the default constructor no longer exists unless it is manually coded. A fully constructed and initialized class object can be called an instance of its class.