2-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

Slides:



Advertisements
Similar presentations
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Advertisements

1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Chapter 2: Introduction to C++.
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
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.
Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
CPS120: Introduction to Computer Science
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Course websites CS201 page link at my website: Lecture slides Assistant’s Information Recitations Office Hours Make-up.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Basic Elements of C++ Chapter 1.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
2-1 Chapter 2 A Little Java Computing Fundamentals with Java Rick Mercer Franklin, Beedle & Associates, 2002 ISBN Presentation Copyright.
3-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Variables, Identifiers, Assignments, Input/Output
C++ First Steps.
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Computing and Statistical Data Analysis Lecture 2
BASIC ELEMENTS OF A COMPUTER PROGRAM
Programming Fundamentals
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Chapter 2 C++ Fundamentals
Variables, Identifiers, Assignments, Input/Output
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

2-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN Presentation Copyright 1999, Franklin, Beedle & Associates Students who purchase and instructors who adopt Computing Fundamentals with C++, Object-Oriented Programming and Design by Rick Mercer are welcome to use this presentation as long as this copyright notice remains intact.

2-2 Chapter 2 Implementation  Chapter Objectives  Understand how to reuse existing code in your programs with #include  Obtain input data from the user and display information to the user with cin and cout  Evaluate and create arithmetic expressions 3*x  Understand operations common to many objects such as int, double, and string:  Construction, Output, Assignment, and Input –Exercises and Programming Projects to reinforce implementing simple programs

The C++ Programming Language: A Start  A C++ program is a sequence of characters created with a text editor and stored as a file.  this is the source code  The file name have specific endings:  UNIX:.cc or.C  DOS or MS-Windows:.cpp  MacOS:.cp or.cpp

2-4 General Forms  General forms provide information to create syntactically correct programs  Anything in yellow boldface must be written exactly as shown ( cout << for example)  Anything in italic represents something that must be supplied by the user  The italicized portions are defined elsewhere

2-5 General Form: A program // Comment #include-directive(s) using namespace std; int main() { object-initializations statement(s) return 0; }

2-6 Example C++ program // This C++ program gets a number from the // user and displays that value squared #include // for cout cin endl using namespace std; int main() { double x = 0.0; cout << "Enter a number: "; cin >> x; cout << "x squared: " << (x * x) << endl; return 0; }

2-7 The compiler  The compiler  reads source code in a character by character fashion  reports errors whenever possible  reports warnings to help avoid errors  conceptually replaces #includes with the source code of the #included file

2-8 #include directives  General form: #include-directive #include #include -or- #include " include-file "  causes a search of the system folder(s)  these files should be found automatically.  The form with " " first searches the working folder before searching the system folder(s)  the " " indicates an author supplied file name that you may need in your working folder.

The smallest pieces of a C++ Program  A token is the smallest recognizable unit in a programming language  There are four types of tokens:  special symbols  keywords  identifiers  constants

2-10 A "tokenized program"  Each color represents either a different token special symbol reserved word identifier constant comment (gray), or #include directive (cyan) // Comment: This is a complete C++ program // Comment: This is a complete C++ program #include #include using namespace std; using namespace std; main() int main() { cout << "Hello World!"; cout << "Hello World!"; 0; return 0; }

Special Symbols  One or two character sequences (no spaces) // ( ) { ( ) { << ; } !=  Some special symbols mean different things in different contexts

Keywords  Word like tokens with a pre-defined meaning that can't be changed (reserved) double int double int  Some of the keywords in the text : bool class for operator typedef bool class for operator typedef case do if return void case do if return void char else long switch while char else long switch while

Identifiers  There are some standard (must be available wth the C++ compiler) identifiers: endl sqrt string width std endl sqrt string width std  The programmer can make up new identifiers (programmer-defined) test1 x1 aNumber MAXIMUM A_1 test1 x1 aNumber MAXIMUM A_1  Note: main is a programmer-defined identifier, cout is a standard identifier

2-14 Active Learning  Identifiers have from 1 to 32 characters: 'a'..'z', 'A'..'Z', '0'..'9', '_'  Identifiers should start with a letter: a1 is legal, 1a is not (can also start with underscore _  C++ is case sensitive. A and a are different.  Which of these are valid identifiers? a) abc e) ABC i) a_1 a) abc e) ABC i) a_1 b) m/h f) 25or6to4 j) student Number b) m/h f) 25or6to4 j) student Number c) main g) 1_time k) string c) main g) 1_time k) string d) double h) first name l) ______ d) double h) first name l) ______

Constants  floating-point constants e10 0.1e-5  string constants "character between double quotes"  integer constants  character constants (see Chapter 7) 'A' 'b' '\n' '1' 'A' 'b' '\n' '1'

Comments  Example comments // on one line or // on one line or /* /* between slash star and star slash between slash star and star slash */ */  Provide internal documentation  Helps us understand program that we must read-- including our own.  Can be used as "pseudocode" within a program and later changed into C++ or left as is to provide documentation

Common Operations on Objects  Common Operations for many classes of objects include these four  Declaration Construct an object Initialization or optionally initialize its state  Assignment Modify the state of an object  Input Modify the state of an object  Output Inspect the state of an object

Object Constructions  Use default initial state: double is garbage class-name identifier class-name identifier ;-or- class-name identifier identifier identifier class-name identifier, identifier, …, identifier ;  Allow client to supply initial state: class-name identifier initial-state class-name identifier = initial-state ; -or - class-name identifier initial-state class-name identifier ( initial-state ) ; -or - -or - class-name identifier initial-state identifier initial-state class-name identifier = initial-state, identifier ( initial-state ) … identifier initial-state, …, identifier = initial-state ;

2-19 Examples Object Constructions  Construct seven objects qualityPoints is undefined double credits = 15.5, qualityPoints; double credits = 15.5, qualityPoints; string firstName, lastName("Harrison"); string firstName, lastName("Harrison"); double x = 0.0, y = 0.0; double x = 0.0, y = 0.0; double z; // garbage state (unknown) double z; // garbage state (unknown)  Note: strings have the default value of the null string "", which has 0 characters.  A double object has no default value--it's garbage unless an initial value is supplied.

Output with cout  Programs must communicate with users  This can be done with keyboard input statements and screen output statements  A C++ statement is composed of several components properly grouped together to perform some operation  Here is the first statement used to display constants and object state to the screen:

2-21 The cout statement  The general form of a cout statement: cout << expression-1 << expression-2 << expression-n ;  Example cout << "Grade: " << courseGrade << endl; cout << "Grade: " << courseGrade << endl;

2-22 What happens with cout?  When a cout statement is encountered, the expressions are displayed on the screen in a manner appropriate to the expression  New Lines with endl  When encountered in a cout statement, endl generates a new line on the monitor  To properly use cout and endl, your program must have this code at the top of the file: #include // for cout and endl #include // for cout and endl using namespace std; using namespace std;

2-23 Active Learning: Write the output generated by this program #include // for cout using namespace std; // so we don’t need std:: int main() { double aDouble = 0.0; double aDouble = 0.0; cout << " " << endl; cout << " " << endl; cout << (2.5 * 3) << (2*3) << endl; cout << (2.5 * 3) << (2*3) << endl; cout << aDouble; cout << aDouble; return 0; return 0;}Output?:

Assignment  Certain objects have undefined state double dunno, do_you; double dunno, do_you; cout << dunno << endl; // Output? ______ cout << dunno << endl; // Output? ______  The programmer can set the state of objects with assignment operations of this form: object-name = expression ;  Examples: dunno = 1.23; dunno = 1.23; do_you = dunno ; do_you = dunno ;

2-25 Memory before and after Object Old Modified NameStateState dunno?1.23 do_you?1.0  The expression must be a value that the object can store (assignment compatible) dunno = "Ohhh no, you can't do that"; // <-Error dunno = "Ohhh no, you can't do that"; // <-Error string s; string s; s = 1.23; // <-Error s = 1.23; // <-Error

2-26 Active Learning:  Write the values for bill and name: double bill; double bill; string name; string name; bill = 10.00; bill = 10.00; bill = bill + (0.06 * bill); bill = bill + (0.06 * bill); name = "Bee Bop"; name = "Bee Bop"; name = "Hip Hop"; name = "Hip Hop"; // bill is ___________? // bill is ___________? // name is now ________? // name is now ________?

Input with cin  General forms : cin >> object-1 ; -or- -or- cin >> object-1 >> object-2 >> object-n ;  Example: cin >> test1;  When a cin statement is encountered  the program pauses for user input.  the characters typed by the user are processed  the object's state is changed to the value of the input

2-28 Input is separated with blanks, tabs, and newlines #include // for cout, cin, endl #include // for cout, cin, endl #include // for class string #include // for class string using namespace std; using namespace std; int main() int main() { string name; string name; cout << "Enter your name: "; cout << "Enter your name: "; cin >> name; cin >> name; cout << "Hello " << name; cout << "Hello " << name; return 0; return 0; } Dialogue when the user enters Kim McPhee McPhee is still waiting to be processed by a non-existent future cin statement Enter your name: Kim McPhee Enter your name: Kim McPhee Hello Kim Hello Kim

Arithmetic Expressions  Arithmetic expressions consist of operators such as + - / * % and operands such as 1.0, payRate, and hoursWorked  Example expression used in an assignment: grossPay = payRate * hoursWorked; grossPay = payRate * hoursWorked;  Another example expression: (40*payRate) + 1.5*payRate*(hoursWorked-40) (40*payRate) + 1.5*payRate*(hoursWorked-40) The previous expression has how many operators ?___ ? operands ?___ ?

2-30 Arithmetic Expressions a numeric object x a numeric object x or a numeric constant 100 or 99.5 or expression + expression x or expression - expression x or expression * expression 2 * x or expression / expression x / 2.0 or ( expression ) ( ) An arithmetic expression can be written in these forms

2-31 Precedence of Arithmetic Operators  Expressions with more than one operator require some sort of precedence rules: * / // evaluated left to right order * / // evaluated left to right order - + // evaluated left to right order - + // evaluated left to right order What is * 8.0 / 6.0 ____?  Use (parentheses ) for readability or to intentionally alter an expression: double C; double C; double F = 212.0; double F = 212.0; C = 5.0 / 9.0 * (F - 32); // C = _____? C = 5.0 / 9.0 * (F - 32); // C = _____?

2-32 #include // for cin, cout, and endl using namespace std; int main() { // Declare Objects double x, y, z, average; double x, y, z, average; // Prompt for and get input from the user // Prompt for and get input from the user cout << "Enter three numbers: "; cout << "Enter three numbers: "; cin >> x >> y >> z; cin >> x >> y >> z; // Process: // Process: average = (x + y + z) / 3.0; average = (x + y + z) / 3.0; // Output: // Output: cout << "Average: " << average << endl; cout << "Average: " << average << endl; return 0; return 0;} Active Learning: Write the complete dialogue with input

const objects  It is sometimes convenient to have objects that cannot have altered state const class-name identifier = expression ; const class-name identifier = expression ; // use this form -or- const class-name identifier ( expression ) ; const class-name identifier ( expression ) ; Examples: Examples: const double PI = ; const double PI = ; const string ERROR_MESSAGE = "Nooooooo"; const string ERROR_MESSAGE = "Nooooooo"; Errors: Errors: PI = 9.8; PI = 9.8; cin >> ERROR_MESSAGE; cin >> ERROR_MESSAGE;

Another Algorithm Pattern: Prompt then Input  The Input/Process/Output programming pattern can be used to help design many programs in the first several chapters  The Prompt then Input pattern also occurs frequently  The user is often asked to enter data  The programmer must make sure the user is told what to enter

2-35 Prompt then Input Pattern

2-36 Examples  Instances of the Prompt then Input pattern: cout << "Enter your first name: "; cout << "Enter your first name: "; cin >> firstName; cin >> firstName; cout << "Enter accumulated credits: "; cout << "Enter accumulated credits: "; cin >> credits; cin >> credits;  Write code that gets the current temperature from the user ? ________ ? ? ________ ? Optional Demo average3.cpp to see prompt then input used three times