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

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

Introduction to Programming

Chapter 1 Introduction to Computers and C++ Programming

IS 0020 Program Design and Software Tools

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

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

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

C: How to Program. Week /Mar/05

Chapter 2 - Introduction to C Programming

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Chapter 1 & 2 Introduction to C Language

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Object Oriented Design

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

Introduction to Programming

C++ Programming Language Lecture 1 Introduction

by Pearson Education, Inc. All Rights Reserved. 2

Programming for Engineers Introduction to C

0 Introduction: Computer systems and program development

Chapter 1 Introduction to Computers and C++ Programming

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

Lecture 3 Tao Wang 1

download instant at Introduction to C++

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

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

IT 374 C# and Applications/ IT695 C# Data Structures

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Chapter 2, Part I Introduction to C Programming

2 nd Week Lecture Notes

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Your First C++ Program. September 1, 2010

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

BITG 1233: Introduction to C++

Programming with C++ as a Second Language

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

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

Another Simple Program: Adding Two Integers

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

Introduction to C# Applications

A First Program - Greeting.cpp

Chapter 2: Basic Elements of C++

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

Chapter 2: Introduction to C++

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

IS 0020 Program Design and Software Tools

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

CHAPTER 3 BASIC INSTRUCTION OF C++

The C++ Language. Arizona State University 1

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

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Fundamentals of Programming Session 4

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

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

6.096 Introduction to C++ January (IAP) 2009

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Fundamentals of Programming. Lecture 1: Introduction to C Programming

Chapter 1 INTRODUCTION

CS242 COMPUTER PROGRAMMING

Introduction to Python Programming

VARIABLES & ASSIGNMENTS

1 EEE1008 C Programming

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Basics of C++ C++ Overview Elements I / O Assignment and Comments Formatted Output. Dr.Hamed Bdour

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

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

Engineering Problem Solving with C++, Etter/Ingber

Computer Programming C++ Classes and Objects 6 th Lecture

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

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

Creating a C++ Program

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

Chapter 2. C++ Basics

LECTURE 02 INTRODUCTION TO C++

Computer Programming : C++

Instructor. Mehmet Zeki COSKUN Assistant Professor at the Geodesy & Photogrammetry, Civil Eng. (212)

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

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

CSCI 1061U Programming Workshop 2. C++ Basics

Objectives. In this chapter, you will:

Data types, variables, constants

by Pearson Education, Inc. All Rights Reserved.

Chapter 2. Outline. Simple C++ Programs

Introductionto C++ Programming

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Introduction to OOP Using Java Pearson Education, Inc. All rights reserved.

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

UNIT- 3 Introduction to C++

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

download instant at Introduction to C++

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

Chapter Two MULTIPLE CHOICE

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Chapter 2: Overview of C++

Copyright 1999 by Deitel & Associates, Inc. All Rights Reserved.

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Fundamentals of Structured Programming

Introduction to Computers, the Internet and the Web Pearson Education, Inc. All rights reserved.

Transcription:

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

Machine Languages, Assembly Languages, and High-level Languages Three types of programming languages Machine languages Strings of numbers giving machine specific instructions Example: +1300042774 +1400593419 +1200274027 Assembly languages English-like abbreviations representing elementary computer operations (translated via assemblers) Example: LOAD BASEPAY ADD OVERPAY STORE GROSSPAY

Machine Languages, Assembly Languages, and High-level Languages High-level languages Similar to everyday English, use mathematical notations (translated via compilers) Example: grosspay = basepay + overtimepay

History of C and C++ C++ evolved from C C evolved from two other programming languages, BCPL and B ANSI C Established worldwide standards for C programming C++ spruces up C Provides capabilities for object-oriented programming Objects are reusable software components that model things in the real world Object-oriented programs are easy to understand, correct and modify

C++ Standard Library C++ programs Built from pieces called classes and functions C++ standard library Provides rich collections of existing classes and functions for all programmers to use

Structured Programming Structured programming Disciplined approach to writing programs Clear, easy to test and debug, and easy to modify Multitasking Many activities to run in parallel

The Key Software Trend: Object Technology Objects Reusable software components that model real world items Meaningful software units Date objects, time objects, paycheck objects, invoice objects, audio objects, video objects, file objects, record objects, etc. Any noun can be represented as an object More understandable, better organized and easier to maintain than procedural programming Favor modularity

Basics of a Typical C++ Environment Phases of C++ Programs: 1. Edit 2. Preprocess 3. Compile Editor Preprocessor Compiler Disk Disk Disk Program is created in the editor and stored on disk. Preprocessor program processes the code. Compiler creates object stores it on disk. code and 4. Link 5. Load 6. Execute Linker Loader Disk Linker links the object code with the libraries, creates a.out and stores it on disk Loader puts program in memory. Disk Primary Memory... CPU CPU takes each instruction and executes it, possibly storing new data values as the program executes. Primary Memory......

Introduction to C++ Programming C++ language Facilitates a structured and disciplined approach to computer program design Following are several examples The examples illustrate many important features of C++ Each example is analyzed one statement at a time

A Simple C++ Program: Printing a Line of Text 1. Comments 2. Load <iostream> 3. main 3.1 Print "Welcome to C++\n" 3.2 exit (return 0) Program Output

A Simple C++ Program: Printing a Line of Text 1 2 3 4 5 6 7 8 9 10 11 12 // Fig. 1.2: fig01_02.cpp // A first program in C++ #include <iostream> // function main begins program execution int main() { std::cout << "Welcome to C++!\n"; return 0; // indicate that program ended successfully } // end function main 1. Comments 2. Load <iostream> 3. main 3.1 Print "Welcome to C++\n" 3.2 exit (return 0) Program Output

A Simple C++ Program: Printing a Line of Text 1 2 3 4 5 6 7 8 9 10 11 12 // Fig. 1.2: fig01_02.cpp // A first program in C++ #include <iostream> Improve program readability and do not cause the // function main begins program execution computer to perform any action. int main() { std::cout << "Welcome to C++!\n"; return 0; // indicate that program ended successfully } // end function main Comments Written between /* and */ or following a //.

A Simple C++ Program: Printing a Line of Text 1 2 3 4 5 6 7 8 9 10 11 12 // Fig. 1.2: fig01_02.cpp // A first program in C++ #include <iostream> // function main begins program execution int main() { std::cout << "Welcome to C++!\n"; preprocessor directive return 0; // indicate that program ended successfully Message to the C++ preprocessor. } // end function main Lines beginning with # are preprocessor directives. #include <iostream> tells the preprocessor to include the contents of the file <iostream>, which includes input/output operations (such as printing to the screen).

A Simple C++ Program: Printing a Line of Text 1 2 3 4 5 6 7 8 9 10 11 12 // Fig. 1.2: fig01_02.cpp // A first program in C++ #include <iostream> // function main begins program execution int main() { std::cout << "Welcome to C++!\n"; return 0; // indicate that program ended successfully } // end function main C++ programs contain one or more functions, one of which must be main Parenthesis are used to indicate a function int means that main "returns" an integer value. More in Chapter 3.

A Simple C++ Program: Printing a Line of Text 1 2 3 4 5 6 7 8 9 10 11 12 // Fig. 1.2: fig01_02.cpp // A first program in C++ #include <iostream> // function main begins program execution int main() { std::cout << "Welcome to C++!\n"; return 0; // indicate that program ended successfully } // end function main A left brace { begins the body of every function and a right brace } ends it.

A Simple C++ Program: Printing a Line of Text 1 2 3 4 5 6 7 8 9 10 11 12 // Fig. 1.2: fig01_02.cpp // A first program in C++ #include <iostream> // function main begins program execution int main() { std::cout << "Welcome to C++!\n"; return 0; // indicate that program ended successfully } // end function main Prints the string of characters contained between the quotation marks. The entire line, including std::cout, the << operator, the string "Welcome to C++!\n" and the semicolon (;), is called a statement. All statements must end with a semicolon.

A Simple C++ Program: Printing a Line of Text 1 2 3 4 5 6 7 8 9 10 11 12 // Fig. 1.2: fig01_02.cpp // A first program in C++ #include <iostream> // function main begins program execution int main() { std::cout << "Welcome to C++!\n"; return 0; // indicate that program ended successfully } // end function main return is a way to exit a function from a function. return 0, in this case, means that the program terminated normally.

A Simple C++ Program: Printing a Line of Text 1 2 3 4 5 6 7 8 9 10 11 12 // Fig. 1.2: fig01_02.cpp // A first program in C++ #include <iostream> // function main begins program execution int main() { std::cout << "Welcome to C++!\n"; return 0; // indicate that program ended successfully } // end function main 1. Comments 2. Load <iostream> 3. main 3.1 Print "Welcome to C++\n" 3.2 exit (return 0) Program Output Welcome to C++!

A Simple Program: Printing a Line of Text std::cout << \ Standard output stream object Connected to the screen std:: specifies the "namespace" which cout belongs to std:: can be removed through the use of using statements Stream insertion operator Value to the right of the operator (right operand) inserted into output stream (which is connected to the screen) std::cout << Welcome to C++!\n ; Escape character Indicates that a special character is to be output

A Simple Program: Printing a Line of Text Escape Sequence Description \n Newline. Position the screen cursor to the beginning of the next line. \t Horizontal tab. Move the screen cursor to the next tab stop. \r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. \a Alert. Sound the system bell. \\ Backslash. Used to print a backslash character. \" Double quote. Used to print a double quote character. There are multiple ways to print text Following are more examples

A Simple C++ Program: Printing a Line of Text 1 2 3 4 5 6 7 8 9 10 11 12 // Fig. 1.2: fig01_04.cpp // A first program in C++ #include <iostream> // function main begins program execution int main() { std::cout << "Welcome "; std::cout << "to C++!\n"; return 0; // indicate that program ended successfully } // end function main Welcome to C++! 1. Load <iostream> 2. main 2.1 Print "Welcome" 2.2 Print "to C++!" 2.3 newline 2.4 exit (return 0) Program Output Unless new line '\n' is specified, the text continues on the same line.

A Simple C++ Program: Printing a Line of Text 1 2 3 4 5 6 7 8 9 10 11 12 Welcome to C++! // Fig. 1.2: fig01_05.cpp // A first program in C++ #include <iostream> // function main begins program execution int main() { std::cout << "Welcome\nto\n\nC++!\n"; return 0; // indicate that program ended successfully } // end function main 1. Load <iostream> 2. main 2.1 Print "Welcome" 2.2 newline 2.3 Print "to" 2.4 newline 2.5 newline 2.6 Print "C++!" 2.7 newline 2.8 exit (return 0) Multiple lines can be printed with one statement. Program Output

Another Simple Program: Adding Two Integers Variables Location in memory where a value can be stored for use by a program Must be declared with a name and a data type before they can be used Some common data types are: int - integer numbers char - characters double - floating point numbers Example: int myvariable; Declares a variable named myvariable of type int Example: int variable1, variable2; Declares two variables, each of type int

Another Simple Program: Adding Two Integers >> (stream extraction operator) When used with std::cin, waits for the user to input a value and stores the value in the variable to the right of the operator The user types a value, then presses the Enter (Return) key to send the data to the computer Example: int myvariable; std::cin >> myvariable; Waits for user input, then stores input in myvariable = (assignment operator) Assigns value to a variable Binary operator (has two operands) Example: sum = variable1 + variable2;

Another Simple Program: Adding Two Integers Load <iostream> 2. main 2.1 Initialize variables integer1, integer2, and sum 2.2 Print "Enter first integer" 2.2.1 Get input 2.3 Print "Enter second integer" 2.3.1 Get input 2.4 Add variables and put result into sum 2.5 Print "Sum is" 2.5.1 Output sum 2.6 exit (return 0) Program Output

Another Simple Program: Adding Two Integers 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Fig. 1.6: fig01_06.cpp // Addition program. #include <iostream> // function main begins program execution int main() { int integer1; // first number to be input by user int integer2; // second number to be input by user int sum; // variable in which sum will be stored std::cout << "Enter first integer\n"; // prompt std::cin >> integer1; // read an integer std::cout << "Enter second integer\n"; // prompt std::cin >> integer2; // read an integer sum = integer1 + integer2; // assignment result to sum std::cout << "Sum is " << sum << std::endl; // print sum return 0; // indicate that program ended successfully } // end function main

Another Simple Program: Adding Two Integers 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Fig. 1.6: fig01_06.cpp // Addition program. Variables declaration #include <iostream> // function main begins program execution int main() { int integer1; // first number to be input by user int integer2; // second number to be input by user int sum; // variable in which sum will be stored std::cout << "Enter first integer\n"; // prompt std::cin >> integer1; // read an integer std::cout << "Enter second integer\n"; // prompt std::cin >> integer2; // read an integer sum = integer1 + integer2; // assignment result to sum std::cout << "Sum is " << sum << std::endl; // print sum return 0; // indicate that program ended successfully } // end function main

Another Simple Program: Adding Two Integers 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Fig. 1.6: fig01_06.cpp // Addition program. #include <iostream> // function main begins program execution int main() { int integer1; // first number to be input by user int integer2; // second number to be input by user int sum; // variable in which sum will be stored std::cout << "Enter first integer\n"; // prompt std::cin >> integer1; // read an integer std::cout << "Enter second integer\n"; // prompt std::cin >> integer2; // read an integer sum = integer1 + integer2; // assignment result to sum std::cout << "Sum is " << sum << std::endl; // print sum return 0; // indicate that program ended successfully } // end function main Notice how std::cin is used to get user input.

Another Simple Program: Adding Two Integers 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Fig. 1.6: fig01_06.cpp // Addition program. #include <iostream> // function main begins program execution int main() { int integer1; // first number to be input by user int integer2; // second number to be input by user int sum; // variable in which sum will be stored std::cout << "Enter first integer\n"; // prompt std::cin >> integer1; // read an integer std::cout << "Enter second integer\n"; // prompt std::cin >> integer2; // read an integer sum = integer1 + integer2; // assignment result to sum std::cout << "Sum is " << sum << std::endl; // print sum return 0; // indicate that program ended successfully } // end function main Variables can be output using std::cout << variablename.

Another Simple Program: Adding Two Integers 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Fig. 1.6: fig01_06.cpp // Addition program. #include <iostream> // function main begins program execution int main() { int integer1; // first number to be input by user int integer2; // second number to be input by user int sum; // variable in which sum will be stored std::cout << "Enter first integer\n"; // prompt std::cin >> integer1; // read an integer std::cout << "Enter second integer\n"; // prompt std::cin >> integer2; // read an integer sum = integer1 + integer2; // assignment result to sum std::cout << "Sum is " << sum << std::endl; // print sum return 0; // indicate that program ended successfully } // end function main std::endl flushes the buffer and prints a newline. Variables can be output using std::cout << variablename.

Another Simple Program: Adding Two Integers 1 // Fig. 1.6: fig01_06.cpp 2 // Addition program. 3 #include <iostream> 4 // function main begins program execution 5 int main() 6 { 7 int integer1; // first number to be input by user 8 int integer2; // second number to be input by user 9 int sum; // variable in which sum will be stored 10 11 std::cout << "Enter first integer\n"; // prompt 12 std::cin >> integer1; // read an integer 13 14 std::cout << "Enter second integer\n"; // prompt 15 std::cin >> integer2; // read an integer 16 17 sum = integer1 + integer2; // assignment result to sum 18 19 Enter first std::cout integer << "Sum is " << sum << std::endl; // print sum 20 45 21 Enter second return integer 0; // indicate that program ended successfully 22 72 23 Sum } is // 117 end function main

Memory Concepts Variable names Correspond to locations in the computer's memory Every variable has a name, a type, a size and a value Whenever a new value is placed into a variable, it replaces the previous value - it is destroyed Reading variables from memory does not change them A visual representation integer1 45

Arithmetic Arithmetic calculations Use * for multiplication and / for division Integer division truncates remainder 7 / 5 evaluates to 1 Modulus operator returns the remainder 7 % 5 evaluates to 2 Operator precedence Some arithmetic operators act before others (i.e., multiplication before addition) Be sure to use parenthesis when needed Example: Find the average of three variables a, b and c Do not use: a + b + c / 3 Use: (a + b + c ) / 3

Arithmetic Arithmetic operators: C++ operation Arithmetic operator Algebraic expression Addition + f + 7 f + 7 Subtraction - p -c p -c Multiplication * b m b * m Division / x / y x / y Modulus % r mod s r % s C++ expression

Arithmetic Rules of operator precedence: Operator (s) Operation(s) Order of evaluation (precedence) () Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses on the same level (i.e., not nested), they are evaluated left to right. *, /, or % Multiplication Division Modulus + or - Addition Subtraction Evaluated second. If there are several, they reevaluated left to right. Evaluated last. If there are several, they are evaluated left to right

Decision Making: Equality and Relational Operators if structure Test conditions truth or falsity. If condition met execute, otherwise ignore Equality and relational operators Lower precedence than arithmetic operators Table of relational operators on next slide

Decision Making: Equality and Relational Operators Standard algebraic equality operator or relational operator Relational operators C++ equality or relational operator Example of C++ condition Meaning of C++ condition > > x > y x is greater than y < < x < y x is less than y >= x >= y x is greater than or equal to y <= x <= y x is less than or equal to y Equality operators = == x == y x is equal to y!= x!= y x is not equal to y

Decision Making: Equality and Relational Operators using statements Eliminate the need to use the std:: prefix Allow us to write cout instead of std::cout To use the following functions without the std:: prefix, write the following at the top of the program using std::cout; using std::cin; using std::endl;

Using if statements, relational, operators, and equality operators 1. Load <iostream> 2. main 2.1 Initialize num1 and num2 2.1.1 Input data 2.2 if statements 2.3 exit (return 0) Program Output

Using if statements, relational, operators, and equality operators 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Fig. 1.14: fig01_14.cpp // Using if statements, relational // operators, and equality operators. #include <iostream> using using using std::cout; // program uses cout std::cin; // program uses cin std::endl; // program uses endl // function main begins program execution int main() { int num1; // first number to be read from user int num2; // second number to be read from user cout << "Enter two integers, and I will tell you\n" << "the relationships they satisfy: "; cin >> num1 >> num2; // read two integers if ( num1 == num2 ) cout << num1 << " is equal to " << num2 << endl;

Using if statements, relational, operators, and equality operators 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 if ( num1!= num2 ) cout << num1 << " is not equal to " << num2 << endl; if ( num1 < num2 ) cout << num1 << " is less than " << num2 << endl; if ( num1 > num2 ) cout << num1 << " is greater than " << num2 << endl; if ( num1 <= num2 ) cout << num1 << " is less than or equal to " << num2 << endl; if ( num1 >= num2 ) cout << num1 << " is greater than or equal to " << num2 << endl; return 0; // indicate that program ended successfully } // end function main

Using if statements, relational, operators, and equality operators 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Fig. 1.14: fig01_14.cpp // Using if statements, relational // operators, and equality operators. #include <iostream> using std::cout; // program uses cout using std::cin; // program uses cin using std::endl; // program uses endl // function main begins program execution int main() { int num1; // first number to be read from user int num2; // second number to be read from user cout << "Enter two integers, and I will tell you\n" << "the relationships they satisfy: "; cin >> num1 >> num2; // read two integers if ( num1 == num2 ) cout << num1 << " is equal to " << num2 << endl; Notice the using statements.

Using if statements, relational, operators, and equality operators 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Fig. 1.14: fig01_14.cpp // Using if statements, relational // operators, and equality operators. #include <iostream> using std::cout; // program uses cout using std::cin; // program uses cin using std::endl; // program uses endl // function main begins program execution int main() { int num1; // first number to be read from user int num2; // second number to be read from user cout << "Enter two integers, and I will tell you\n" << "the relationships they satisfy: "; cin >> num1 >> num2; // read two integers Notice the using statements. if ( num1 == num2 ) Enter two integers, and I will tell you cout << num1 << " is equal to the " << relationships num2 << endl; they satisfy: 3 7

Using if statements, relational, operators, and equality operators 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Fig. 1.14: fig01_14.cpp // Using if statements, relational // operators, and equality operators. #include <iostream> using std::cout; // program uses cout using std::cin; // program uses cin using std::endl; // program uses endl // function main begins program execution int main() { int num1; // first number to be read from user int num2; // second number to be read from user cout << "Enter two integers, and I will tell you\n" << "the relationships they satisfy: "; cin >> num1 >> num2; // read two integers if ( num1 == num2 ) cout << num1 << " is equal to " << num2 << endl; The if statements test the truth of the condition. If it is true, body of if statement is executed. If not, body is skipped. To include multiple statements in a body, delineate them with braces {}.

Using if statements, relational, operators, and equality operators 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 if ( num1!= num2 ) cout << num1 << " is not equal to " << num2 << endl; 3 is not equal to 7 if ( num1 < num2 ) cout << num1 << " is less than " << num2 << endl; if ( num1 > num2 ) cout << num1 << " is greater than " << num2 << endl; if ( num1 <= num2 ) cout << num1 << " is less than or equal to " << num2 << endl; if ( num1 >= num2 ) cout << num1 << " is greater than or equal to " << num2 << endl; return 0; // indicate that program ended successfully } // end function main

Using if statements, relational, operators, and equality operators 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 if ( num1!= num2 ) cout << num1 << " is not equal to " << num2 << endl; 3 is not equal to 7 if ( num1 < num2 ) cout << num1 << " is less than " << num2 << endl; 3 is less than 7 if ( num1 > num2 ) cout << num1 << " is greater than " << num2 << endl; if ( num1 <= num2 ) cout << num1 << " is less than or equal to " << num2 << endl; if ( num1 >= num2 ) cout << num1 << " is greater than or equal to " << num2 << endl; return 0; // indicate that program ended successfully } // end function main

Using if statements, relational, operators, and equality operators 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 if ( num1!= num2 ) cout << num1 << " is not equal to " << num2 << endl; 3 is not equal to 7 if ( num1 < num2 ) cout << num1 << " is less than " << num2 << endl; 3 is less than 7 if ( num1 > num2 ) cout << num1 << " is greater than " << num2 << endl; if ( num1 <= num2 ) cout << num1 << " is less than or equal to " << num2 << endl; if ( num1 >= num2 ) cout << num1 << " is greater than or equal to " << num2 << endl; 3 is less than or equal to 7 return 0; // indicate that program ended successfully } // end function main

Using if statements, relational, operators, and equality operators Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7 Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12 Enter two integers, and I will tell you the relationships they satisfy: 7 7 7 is equal to 7 7 is less than or equal to 7 7 is greater than or equal to 7

Thinking About Objects: Introduction to Object Technology and the Unified Modeling Language Object orientation Natural way to think about the world and to write computer programs Attributes - properties of objects Size, shape, color, weight, etc. Behaviors - actions A ball rolls, bounces, inflates and deflates Objects can perform actions as well Inheritance New classes of objects absorb characteristics from existing classes Information hiding Objects usually do not know how other objects are implemented

Thinking About Objects: Introduction to Object Technology and the Unified Modeling Language Abstraction - view the big picture See a photograph rather than a group of colored dots Think in terms of houses, not bricks Class - unit of programming Classes serve as a Blueprint" of objects Objects are created from a class Classes contain functions Used to implement behaviors Classes contain data Used to implement attributes Classes are reusable

Thinking About Objects: Introduction to Object Technology and the Unified Modeling Language Unified Modeling Language (UML) Used to model object-oriented systems and aid with their design Complex, feature-rich graphical language