Object Oriented Design

Similar documents
Chapter 1 Introduction to Computers and C++ Programming

by Pearson Education, Inc. All Rights Reserved. 2

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Introduction to Programming

by Pearson Education, Inc. All Rights Reserved. 2

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

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

by Pearson Education, Inc. All Rights Reserved.

Computer Programming C++ Classes and Objects 6 th Lecture

Another Simple Program: Adding Two Integers

Introduction to C++ (Extensions to C)

CS242 COMPUTER PROGRAMMING

How to engineer a class to separate its interface from its implementation and encourage reuse.

IS 0020 Program Design and Software Tools

Programming with C++ as a Second Language

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Introduction to C++ Systems Programming

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

This watermark does not appear in the registered version - Slide 1

EP241 Computer Programming

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Variables and Constants

Chapter 1 INTRODUCTION

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Chapter 1 Introduction to Computers and C++ Programming

Chapter 2. Outline. Simple C++ Programs

C++ Spring Break Packet 11 The Java Programming Language

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

Lab # 02. Basic Elements of C++ _ Part1

CSCI 1061U Programming Workshop 2. C++ Basics

Unit 1 : Principles of object oriented programming

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Fundamentals of Structured Programming

Scientific Computing

Basic Computer Programming for ISNE. Santi Phithakkitnukoon ผศ.ดร.ส นต พ ท กษ ก จน ก ร

Computer Programming : C++

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Introduction to C++ Introduction to C++ 1

VARIABLES & ASSIGNMENTS

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Chapter 15 - C++ As A "Better C"

Chapter 1: An Overview of Computers and Programming Languages. Objectives. Objectives (cont d.) Introduction

1a Computers, Problem Solving!

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Ch 1. Algorithms and Basic C++ Programming

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

Your First C++ Program. September 1, 2010

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Programming for Engineers Introduction to C

Full file at C How to Program, 6/e Multiple Choice Test Bank

Chapter 1 Introduction to Computers and Programming

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

A SHORT COURSE ON C++

C++ For Science and Engineering Lecture 2

Cours de C++ Introduction

Starting to Program in C++ (Basics & I/O)

Understanding main() function Input/Output Streams

Introduction to Computers and Java

CSE 303: Concepts and Tools for Software Development

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

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

Computer Programming

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 1: Why Program? Computers and Programming. Why Program?

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Tutorial-2a: First steps with C++ programming

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Chapter 2: Overview of C++

C++ Programming Language Lecture 1 Introduction

Introduction to Computers and Java

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Engineering Problem Solving with C++, Etter/Ingber

Introduction to Computers and Java. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Introduction to Computers and Java

assembler Machine Code Object Files linker Executable File

download instant at Introduction to C++

C++ basics Getting started with, and Data Types.

Introduction to Computers and Java

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

Introduction to Computers and Java

The C++ Language. Arizona State University 1

Chapter 2 Basic Elements of C++

Binomial pricer (1.1)

Object Oriented Design

Programmazione. Prof. Marco Bertini

Streams. Ali Malik

Working with Strings. Lecture 2. Hartmut Kaiser. hkaiser/spring_2015/csc1254.html

Chapter 1 Introduction to Computers and C++ Programming

BITG 1113: Introduction To Computers And Programming Language LECTURE 1 LECTURE 1 1

Computer Science II Lecture 1 Introduction and Background

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

Why Is Repetition Needed?

Transcription:

Object Oriented Design Lecture 2: Introduction to C++ Class and Object Objects are essentially reusable software components. There are date objects, time objects, audio objects, video objects, automobile objects, people objects, etc. Almost any noun can be reasonably represented as a software object in terms of attributes (e.g., name, color and size) and behaviors (e.g., calculating, moving and communicating). 1

Class & Object The Automobile A car typically begins as engineering drawings, similar to the blueprints that describe the design of a house Real cars (object) will be built according to the drawing (class) Object Enables people with little or no knowledge of how engines, braking and steering mechanisms work to drive a car easily. Before you can drive a car, it must be built from the engineering drawings that describe it. A completed car has an actual accelerator pedal to make the car go faster, but even that s not enough the car won t accelerate on its own (hopefully!), so the driver must press the pedal to accelerate the car. 2

Class and Member Function Classes and Member Functions Performing a task in a program requires a member function In C++, we create a program unit called a class to house the set of member functions that perform the class s tasks. A class is similar in concept to a car s engineering drawings, which house the design of an accelerator pedal, steering wheel, and so on. From Class to Object Instantiation Someone has to build a car from its engineering drawings before you can actually drive a car, You must build an object of a class before a program can perform the tasks that the class s member functions define. An object is then referred to as an instance of its class. 3

Objects Benefit #1 Reusable Just as a car s engineering drawings can be reused many times to build many cars, you can reuse a class many times to build many objects. Reuse of existing classes when building new classes and programs saves time and effort. Reuse also helps you build more reliable and effective systems, because existing classes and components often have gone through extensive testing, debugging and performance tuning. Provides building reliable blocks 4

Interface to Objects Member functions provide interfaces to objects When you drive a car, pressing its gas pedal sends a message to the car to perform a task that is, to go faster. Similarly, you send messages to an object through calling member functions Member function tells the object how to perform its task. Data Members Data Members A car has attributes: Color, its number of doors, the amount of gas in its tank, its current speed and its record of total miles driven (i.e., its odometer reading). The car s attributes are represented as part of its design in its engineering diagrams. Every car maintains its own attributes. Each car knows how much gas is in its own gas tank, but not how much is in the tanks of other cars. A class has data members and each object keep their own copy of the data members 5

Benefit #2 Inheritance A new class of objects can be created quickly and conveniently by inheritance the new class absorbs the characteristics of an existing class, possibly customizing them and adding unique characteristics of its own. In our car analogy, an object of class convertible certainly is an object of the more general class automobile, but more specifically, the roof can be raised or lowered. Design Object-Oriented Program Object-Oriented Analysis and Design (OOAD) How will you create the code (i.e., the program instructions) for your programs? Follow a detailed analysis process for determining your project s requirements (i.e., defining what the system is supposed to do) Develop a design that satisfies them (i.e., deciding how the system should do it). Carefully review the design (and have your design reviewed by other software professionals) before writing any code. 6

Analyzing and designing your system from an object-oriented point of view, it s called an objectoriented analysis and design (OOAD) process. Languages like C++ are object oriented. Object-oriented programming (OOP), allows you to implement an object-oriented design as a working system. UML The UML (Unified Modeling Language) The Unified Modeling Language (UML), is now the most widely used graphical scheme for modeling object-oriented systems. 7

Environment and Support C++ Standard Library C++ programs consist of pieces called classes and functions. Most C++ programmers take advantage of the rich collections of classes and functions in the C++ Standard Library. Two parts to learning the C++ world. The C++ language itself, and How to use the classes and functions in the C++ Standard Library. Many special-purpose class libraries are supplied by independent software vendors. Building blocks from C++ Standard Library Building blocks from third party library Environment C++ systems generally consist of three parts: a program development environment, the language and the C++ Standard Library. C++ programs typically go through six phases: edit, preprocess, compile, link, load and execute. 8

Environment Phase 1 consists of editing a file with an editor program, normally known simply as an editor. Type a C++ program (source code) using the editor. Make any necessary corrections. Save the program. C++ source code filenames often end with the.cpp,.cxx,.cc or.c extensions (note that C is in uppercase) which indicate that a file contains C++ source code. 9

Environment Linux editors: vi and emacs. C++ software packages for Microsoft Windows such as Microsoft Visual C++ (microsoft.com/express) have editors integrated into the programming environment. You can also use a simple text editor, such as Notepad in Windows, to write your C++ code. integrated development environments (IDEs) Provide tools that support the software-development process, including editors for writing and editing programs and debuggers for locating logic errors errors that cause programs to execute incorrectly. Environment Popular IDEs Microsoft Visual Studio 2010 Express Edition Dev C++ NetBeans Eclipse CodeLite 10

Environment In phase 2, you give the command to compile the program. A preprocessor program executes automatically before the compiler s translation phase begins (so we call preprocessing Phase 2 and compiling Phase 3). The C++ preprocessor obeys commands called preprocessor directives, which indicate that certain manipulations are to be performed on the program before compilation. These manipulations usually include other text files to be compiled, and perform various text replacements. The most common preprocessor directives are discussed in the early chapters; a detailed discussion of preprocessor features appears in Appendix E, Preprocessor. 11

Environment In Phase 3, the compiler translates the C++ program into machine-language code also referred to as object code. 12

Environment Phase 4 is called linking. The object code produced by the C++ compiler typically contains holes due to these missing parts. A linker links the object code with the code for the missing functions to produce an executable program. If the program compiles and links correctly, an executable image is produced. 13

Environment Phase 5 is called loading. Before a program can be executed, it must first be placed in memory. This is done by the loader, which takes the executable image from disk and transfers it to memory. Additional components from shared libraries that support the program are also loaded. 14

Environment Phase 6: Execution Finally, the computer, under the control of its CPU, executes the program one instruction at a time. Some modern computer architectures can execute several instructions in parallel. 15

Environment Problems That May Occur at Execution Time Programs might not work on the first try. Each of the preceding phases can fail because of various errors that we ll discuss throughout this book. If this occurred, you d have to return to the edit phase, make the necessary corrections and proceed through the remaining phases again to determine that the corrections fixed the problem(s). Most programs in C++ input or output data. Environment Certain C++ functions take their input from cin (the standard input stream; pronounced see-in ), which is normally the keyboard, but cin can be redirected to another device. Data is often output to cout (the standard output stream; pronounced see-out ), which is normally the computer screen, but cout can be redirected to another device. When we say that a program prints a result, we normally mean that the result is displayed on a screen. 16

Environment Data may be output to other devices, such as disks and hardcopy printers. There is also a standard error stream referred to as cerr. The cerr stream is used for displaying error messages. Example Code 17

Comments // indicates that the remainder of each line is a comment. You insert comments to document your programs and to help other people read and understand them. Comments are ignored by the C++ compiler and do not cause any machine-language object code to be generated. A comment beginning with // is called a singleline comment because it terminates at the end of the current line. You also may use C s style in which a comment possibly containing many lines begins with /* and ends with */. Example Code Every program should begin with a comment that describes the purpose of the program Comments are in green 18

Preprocessor Directive A preprocessor directive is a message to the C++ preprocessor. Lines that begin with # are processed by the preprocessor before the program is compiled. #include <iostream> notifies the preprocessor to include in the program the contents of the input/output stream header file <iostream>. Must be included for any program that outputs data to the screen or inputs data from the keyboard using C++-style stream input/output. Example Code Include the IO stream header file 19

main The keyword int to the left of main indicates that main returns an integer (whole number) value. For now, simply include the keyword int to the left of main in each of your programs. Example Code main function 20

IO When a cout statement executes, it sends a stream of characters to the standard output stream object std::cout which is normally connected to the screen. The std:: before cout is required when we use names that we ve brought into the program by the preprocessor directive #include <iostream>. The notation std::cout specifies that we are using a name, in this case cout, that belongs to namespace std. The names cin (the standard input stream) and cerr (the standard error stream) also belong to namespace std. The << operator is referred to as the stream insertion operator. The value to the operator s right, the right operand, is inserted in the output stream. Example Code Output 21

Example Code Output format Print Two lines 22

Input A prompt it directs the user to take a specific action. A cin statement uses the input stream object cin (of namespace std) and the stream extraction operator, >>, to obtain a value from the keyboard. Using the stream extraction operator with std::cin takes character input from the standard input stream, which is usually the keyboard. 23

Input Prompt When the computer executes an input statement that places a value in an int variable, it waits for the user to enter a value for variable number1. The user responds by typing the number (as characters) then pressing the Enter key (sometimes called the Return key) to send the characters to the computer. The computer converts the character representation of the number to an integer and assigns (i.e., copies) this number (or value) to the variable number1. Any subsequent references to number1 in this program will use this same value. 24

End Line std::endl is a so-called stream manipulator. The name endl is an abbreviation for end line and belongs to namespace std. The std::endl stream manipulator outputs a newline, then flushes the output buffer. This simply means that, on some systems where outputs accumulate in the machine until there are enough to make it worthwhile to display them on the screen, std::endl forces any accumulated outputs to be displayed at that moment. This can be important when the outputs are prompting the user for an action, such as entering data. End line 25

Using multiple stream insertion operators (<<) in a single statement is referred to as concatenating, chaining or cascading stream insertion operations std::cout << sum is << sum << std::endl; Calculations can also be performed in output statements std::cout << sum is << number1+number2 << std::endl; Memory Concept Variable names such as number1, number2 and sum actually correspond to locations in the computer s memory. Every variable has a name, a type, a size and a value. When a value is placed in a memory location, the value overwrites the previous value in that location; thus, placing a new value into a memory location is said to be destructive. When a value is read out of a memory location, the process is nondestructive. 26

Compile, Link, and Execute In U/Linux Compile g++ -c test.cc Link g++ -o test test.o or g++ test.o -o test Execute./test 27

End 28