 2008 Pearson Education, Inc. All rights reserved. 1 CISC 1600 – Computer Science I Fall 2010 Introduction to C++ Programming Chapters 1 and 2 (Deitel.

Slides:



Advertisements
Similar presentations
Chapter 2 Part B CISS 241. Take a few moments and work with another student to develop an algorithm for a program that will add two whole numbers (integers)
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
 2005 Pearson Education, Inc. All rights reserved Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Introduction to C Programming
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 1 – Introduction to Computers and C++ Programming Outline 1.1 Introduction 1.2 What is a Computer?
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 1 – Introduction to Computers and C++ Programming Outline 1.6 Machine Languages, Assembly Languages,
Introduction Kingdom of Saudi Arabia Shaqra University
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline History of C and C++ C++ Standard Library Object Technology Basics.
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
Three types of computer languages
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2003 Prentice Hall, Inc. All rights reserved Computer Organization Six logical units of computer 1.Input unit “Receiving” section Obtains information.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
Introduction to C Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 01: Introduction to Computer Programming
Introduction to C++ Programming
COMPUTER SCIENCE I C++ INTRODUCTION
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
 2008 Pearson Education, Inc. All rights reserved Introduction to Computers, the Internet and World Wide Web.
 2006 Pearson Education, Inc. All rights reserved Introduction to Computers, the Internet and World Wide Web.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 1 February 8, 2005.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
INTRODUCTION Kingdom of Saudi Arabia Princess Nora bint Abdul Rahman University College of Computer Since and Information System CS240.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Announcements Starting next week class 6-8 on Thursday Homework 1 on the web  Due January 29 – next class meeting  Homework policy No late assignments.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
Chapter 02 (Part II) Introduction to C++ Programming.
1 Introduction to C++ Programming Outline Introduction Machine Languages, Assembly Languages, and High-Level Languages History of C++ C++ Standard Library.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
 2003 Prentice Hall, Inc. All rights reserved Introduction Software –Instructions to command computer to perform actions and make decisions Hardware.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
1.2 What is a Computer? Computer Computer programs Hardware Software
Introduction to C++ Programming
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Introduction to C++ Programming
Chapter 1 – Introduction to Computers and C++ Programming
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Presentation transcript:

 2008 Pearson Education, Inc. All rights reserved. 1 CISC 1600 – Computer Science I Fall 2010 Introduction to C++ Programming Chapters 1 and 2 (Deitel & Deitel)

 2008 Pearson Education, Inc. All rights reserved Machine Languages, Assembly Languages and High-Level Languages Three types of computer languages –Machine language Only language computer directly understands – “Natural language” of computer – Defined by hardware design Generally consist of strings of numbers – Ultimately 0s and 1s Instruct computers to perform elementary operations Cumbersome for humans Example –

 2008 Pearson Education, Inc. All rights reserved Machine Languages, Assembly Languages and High-Level Languages (Cont.) Three types of computer languages (Cont.) –Assembly language English-like abbreviations representing elementary computer operations Clearer to humans Incomprehensible to computers – Convert to machine language by translator programs (assemblers) Example – loadbasepay add overpay store grosspay

 2008 Pearson Education, Inc. All rights reserved Machine Languages, Assembly Languages and High-Level Languages (Cont.) Three types of computer languages (Cont.) –High-level languages Similar to everyday English – Uses common mathematical notations Single statements accomplish substantial tasks Converted to machine language by translator programs (compilers) Interpreter programs – Directly execute high-level language programs – Execute more slowly than the compiled program Example – grossPay = basePay + overTimePay

 2008 Pearson Education, Inc. All rights reserved History of C and C++ History of C – Evolved from BCPL and B Developed by Dennis Ritchie (Bell Laboratories) – Development language of UNIX – Hardware independent Can write portable programs – ANSI and ISO standard for C published in 1990 ANSI/ISO 9899: 1990

 2008 Pearson Education, Inc. All rights reserved History of C and C++ (Cont.) History of C++ – Extension of C Developed by Bjarne Stroustrup (Bell Laboratories) in e arly 1980s – Provides capabilities for object-oriented programming Objects: reusable software components – Model items in the real world Object-oriented programs – Easier to understand, correct and modify

 2008 Pearson Education, Inc. All rights reserved Key Software Trend: Object Technology Objects – Reusable software components that model real world items – Meaningful software units Time objects, paycheck objects, record objects, etc. Any noun can be represented as an object – More understandable, better organized and easier to maintain than procedural programming – Libraries of reusable software

 2008 Pearson Education, Inc. All rights reserved Typical C++ Development Environment C++ programs normally undergo six phases – Edit Programmer writes program (and stores source code on disk) – Preprocess Perform certain manipulations before compilation – Compile Compiler translates C++ programs into machine languages – Link Link object code with missing functions and data – Load Transfer executable image to memory – Execute Execute the program one instruction at a time

 2008 Pearson Education, Inc. All rights reserved. 9 Typical C++ environment.

 2008 Pearson Education, Inc. All rights reserved Introduction C++ programming – Facilitates disciplined approach to computer program design – Programs process information and display results Five examples demonstrate – How to display messages on the screen – How to obtain information from the user – How to perform arithmetic calculations – How to make decisions by comparing numbers

 2008 Pearson Education, Inc. All rights reserved First Program in C++: Printing a Line of Text (Cont.) Comments – Explain programs to you and other programmers – Improve program readability – Ignored by compiler – Single-line comment Begins with // Example – // This is a text-printing program. – Multi-line comment Starts with /* Ends with */

 2008 Pearson Education, Inc. All rights reserved. 12 Outline fig02_01.cpp (1 of 1) fig02_01.cpp output (1 of 1) Single-line commentsPreprocessor directive to include input/output stream header file Function main appears exactly once in every C++ program Function main returns an integer value Left brace { begins function body Corresponding right brace } ends function body Statements end with a semicolon ; Name cout belongs to namespace std Stream insertion operator Keyword return is one of several means to exit a function; value 0 indicates that the program terminated successfully

 2008 Pearson Education, Inc. All rights reserved First Program in C++: Printing a Line of Text (Cont.) Preprocessor directives – Processed by preprocessor before compiling – Begin with # – Example #include – Tells preprocessor to include the input/output stream header file White space – Blank lines, space characters and tabs – Used to make programs easier to read – Ignored by the compiler

 2008 Pearson Education, Inc. All rights reserved First Program in C++: Printing a Line of Text (Cont.) Function main – A part of every C++ program Exactly one function in a program must be main – Can return a value – Example int main() – This main function returns an integer (whole number) – Body is delimited by braces ( {} ) Statements – Instruct the program to perform an action – All statements end with a semicolon ( ; )

 2008 Pearson Education, Inc. All rights reserved First Program in C++: Printing a Line of Text (Cont.) Namespace – std:: Specifies using a name that belongs to “namespace” std Can be removed through the use of using statements Standard output stream object – std::cout “Connected” to screen Defined in input/output stream header file

 2008 Pearson Education, Inc. All rights reserved First Program in C++: Printing a Line of Text (Cont.) Stream insertion operator << – Value to right (right operand) inserted into left operand – Example std::cout << "Hello"; – Inserts the string "Hello" into the standard output Displays to the screen Escape characters – A character preceded by "\" Indicates “special” character output – Example "\n" – Cursor moves to beginning of next line on the screen

 2008 Pearson Education, Inc. All rights reserved First Program in C++: Printing a Line of Text (Cont.) return statement – One of several means to exit a function – When used at the end of main The value 0 indicates the program terminated successfully Example – return 0;

 2008 Pearson Education, Inc. All rights reserved. 18 Fig. 2.2 | Escape sequences.

 2008 Pearson Education, Inc. All rights reserved. 19 Outline fig02_03.cpp (1 of 1) fig02_03.cpp output (1 of 1) Multiple stream insertion statements produce one line of output because line 8 ends without a newline

 2008 Pearson Education, Inc. All rights reserved. 20 Outline fig02_04.cpp (1 of 1) fig02_04.cpp output (1 of 1) Use newline characters to print on multiple lines

 2008 Pearson Education, Inc. All rights reserved Another C++ Program: Adding Integers Variable – Is a location in memory where a value can be stored – Common data types (fundamental, primitive or built-in) int – for integer numbers char – for characters double – for floating point numbers – Declare variables with data type and name before use int integer1; int integer2; int sum;

 2008 Pearson Education, Inc. All rights reserved. 22 Outline fig02_05.cpp (1 of 1) fig02_05.cpp output (1 of 1) Declare integer variables Use stream extraction operator with standard input stream to obtain user input Stream manipulator std::endl outputs a newline, then “flushes” output buffer Concatenating, chaining or cascading stream insertion operations

 2008 Pearson Education, Inc. All rights reserved Another C++ Program: Adding Integers (Cont.) Variables (Cont.) – You can declare several variables of same type in one declaration Comma-separated list int integer1, integer2, sum; – Variable name Must be a valid identifier – Series of characters (letters, digits, underscores) – Cannot begin with digit – Case sensitive (uppercase letters are different from lowercase letters)

 2008 Pearson Education, Inc. All rights reserved Another C++ Program: Adding Integers (Cont.) Input stream object – std::cin from Usually connected to keyboard Stream extraction operator >> – Waits for user to input value, press Enter (Return) key – Stores a value in the variable to the right of the operator Converts the value to the variable’s data type Example – std::cin >> number1; Reads an integer typed at the keyboard Stores the integer in variable number1

 2008 Pearson Education, Inc. All rights reserved Another C++ Program: Adding Integers (Cont.) Assignment operator = – Assigns the value on the right to the variable on the left – Binary operator (two operands) – Example: sum = variable1 + variable2; – Adds the values of variable1 and variable2 – Stores the result in the variable sum Stream manipulator std::endl – Outputs a newline – Flushes the output buffer

 2008 Pearson Education, Inc. All rights reserved Another C++ Program: Adding Integers (Cont.) Concatenating stream insertion operations – Use multiple stream insertion operators in a single statement Stream insertion operation knows how to output each type of data – Also called chaining or cascading – Example std::cout << " Sum is " << number1 + number2 << std::endl; – Outputs "Sum is “ – Then outputs the sum of variables number1 and number2 – Then outputs a newline and flushes the output buffer

 2008 Pearson Education, Inc. All rights reserved Memory Concepts Variable names – Correspond to actual locations in the computer's memory Every variable has a name, a type, a size and a value – When a new value placed into a variable, the new value overwrites the old value Writing to memory is “destructive” – Reading variables from memory is nondestructive – Example sum = number1 + number2; – Although the value of sum is overwritten – The values of number1 and number2 remain intact

 2008 Pearson Education, Inc. All rights reserved. 28 Fig. 2.6 | Memory location showing the name and value of variable number1.

 2008 Pearson Education, Inc. All rights reserved. 29 Fig. 2.7 | Memory locations after storing values for number1 and number2.

 2008 Pearson Education, Inc. All rights reserved. 30 Fig. 2.8 | Memory locations after calculating and storing the sum of number1 and number2.

 2008 Pearson Education, Inc. All rights reserved Arithmetic Arithmetic operators – * Multiplication – / Division Integer division truncates (discards) the remainder – 7 / 5 evaluates to 1 – % The modulus operator returns the remainder – 7 % 5 evaluates to 2

 2008 Pearson Education, Inc. All rights reserved Arithmetic (Cont.) Straight-line form – Required for arithmetic expressions in C++ – All constants, variables and operators appear in a straight line Grouping subexpressions – Parentheses are used in C++ expressions to group subexpressions In the same manner as in algebraic expressions – Example a * ( b + c ) – Multiple a times the quantity b + c

 2008 Pearson Education, Inc. All rights reserved. 33 Fig. 2.9 | Arithmetic operators.

 2008 Pearson Education, Inc. All rights reserved Arithmetic (Cont.) Rules of operator precedence – Operators in parentheses are evaluated first For nested (embedded) parentheses – Operators in innermost pair are evaluated first – Multiplication, division and modulus are applied next Operators are applied from left to right – Addition and subtraction are applied last Operators are applied from left to right

 2008 Pearson Education, Inc. All rights reserved. 35 Fig | Precedence of arithmetic operators.

 2008 Pearson Education, Inc. All rights reserved Decision Making: Equality and Relational Operators Condition – Expression can be either true or false – Can be formed using equality or relational operators if statement – If the condition is true, the body of the if statement executes – If the condition is false, the body of the if statement does not execute

 2008 Pearson Education, Inc. All rights reserved. 37 Fig | Equality and relational operators.

 2008 Pearson Education, Inc. All rights reserved. 38 Outline fig02_13.cpp (1 of 2) using declarations eliminate the need for std:: prefix You can write cout and cin without std:: prefix Declaring variables if statement compares the values of number1 and number2 to test for equality If the condition is true (i.e., the values are equal), execute this statement if statement compares values of number1 and number2 to test for inequality If the condition is true (i.e., the values are not equal), execute this statement Compares two numbers using relational operators

 2008 Pearson Education, Inc. All rights reserved. 39 Outline fig02_13.cpp (2 of 2) fig02_13.cpp output (1 of 3) (2 of 3) (3 of 3) Compares two numbers using the relational operators =

 2008 Pearson Education, Inc. All rights reserved. 40 Fig | Precedence and associativity of the operators discussed so far.