1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.

Slides:



Advertisements
Similar presentations
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
Advertisements

CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Types and Variables. Computer Programming 2 C++ in one page!
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. The Fundamentals of C++ Basic programming elements and concepts.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Data types and variables
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept.
Basic Elements of C++ Chapter 2.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
COMPUTER SCIENCE I C++ INTRODUCTION
By Dr. Awad Khalil Computer Science & Engineering Department
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Input & Output: Console
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Overview of C++ Chapter C++ Language Elements t Comments make a program easier to understand t // Used to signify a comment on a single line.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
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 
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Bill Tucker Austin Community College COSC 1315
Variables, Identifiers, Assignments, Input/Output
Chapter 2: Introduction to C++
Chapter 2: Introduction to C++
Chapter 2: Introduction to C++
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Variables, Identifiers, Assignments, Input/Output
COMS 261 Computer Science I
Chapter 2: Introduction to C++.
Fundamental Programming
COMS 261 Computer Science I
Primitive Types and Expressions
The Fundamentals of C++
Presentation transcript:

1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6

2 Announcements I need to miss office hours today –¾ ton of tile in Richmond is calling me

3 Review Program Organization Objects First C++ Program –Hello, World Code Warrior IDE

4 Outline Area.cpp Standard data types –Integer –Real (float) –Character

Write a program to 1)Prompt the user for the width and height of a rectangle 2)Read and store the entered width and height values 3)Compute the area of the rectangle 4)Display the area, width, and height of the specified rectangle Area Program - Area.cpp

Prompt the user –What does this mean? –Output a message on the display similar to: Enter the width and height of the rectangle: –Do you know how to output a message to the display? Insert a string literal into the output object: cout cout << “Enter the width and height of a rectangle “; Step 1 Insertion operator

Read and store the entered width and height values –Read from where? The keyboard, which is the standard input device: cin Extract two values from the keyboard Save them in variables (objects whose value can change) cin >> width >> height; Step 2 Extraction operator

Compute the area of a rectangle: rectangleArea = width * height Step 3

Display the area, width, and height of the specified rectangle –Output a message on the display similar to: Area: # width: # height: # –Insert some text and numbers into the standard output object: cout cout << “Area: “ << rectangleArea << “width: “ << width << “height: “ << height << endl; Step 4

10 Area.cpp #include // since we need the insertion and extraction operators using namespace std;// since we need cout and cin int main() {// the main function definition starts here cout << "Rectangle dimensions: ";// insert the prompt into the output float width; // define a variable called width float height;// define a variable called height cin >> width >>height;// extract values for width and height from standard input float rectangleArea = width * height; // define and initialize the area cout << “Area: “ << rectangleArea // insert values into the standard output object << “width: “ << width << “height: “ << height << endl; return 0;// end of the program } // end of the main function

11 Area.cpp #include // since we need the insertion and extraction operators using namespace std;// since we need cout and cin int main() {// the main function definition starts here cout << "Rectangle dimensions: ";// insert the prompt into the output float width; // define a variable called width float height;// define a variable called height cin >> width >>height;// extract values for width and height from standard input float rectangleArea = width * height; // define and initialize the area cout << “Area: “ << rectangleArea // insert values into the standard output object << “width: “ << width << “height: “ << height << endl; return 0;// end of the program } // end of the main function

12 Area.cpp #include // since we need the insertion and extraction operators using namespace std;// since we need cout and cin int main() {// the main function definition starts here cout << "Rectangle dimensions: ";// insert the prompt into the output float width; // define a variable called width float height;// define a variable called height cin >> width >>height;// extract values for width and height from standard input float rectangleArea = width * height; // define and initialize the area cout << “Area: “ << rectangleArea // insert values into the standard output object << “width: “ << width << “height: “ << height << endl; return 0;// end of the program } // end of the main function

13 Area.cpp #include // since we need the insertion and extraction operators using namespace std;// since we need cout and cin int main() {// the main function definition starts here cout << "Rectangle dimensions: ";// insert the prompt into the output float width; // define a variable called width float height;// define a variable called height cin >> width >>height;// extract values for width and height from standard input float rectangleArea = width * height; // define and initialize the area cout << “Area: “ << rectangleArea // insert values into the standard output object << “width: “ << width << “height: “ << height << endl; return 0;// end of the program } // end of the main function

14 Area.cpp #include // since we need the insertion and extraction operators using namespace std;// since we need cout and cin int main() {// the main function definition starts here cout << "Rectangle dimensions: ";// insert the prompt into the output float width; // define a variable called width float height;// define a variable called height cin >> width >>height;// extract values for width and height from standard input float rectangleArea = width * height; // define and initialize the area cout << “Area: “ << rectangleArea // insert values into the standard output object << “width: “ << width << “height: “ << height << endl; return 0;// end of the program } // end of the main function

15 Area.cpp #include // since we need the insertion and extraction operators using namespace std;// since we need cout and cin int main() {// the main function definition starts here cout << "Rectangle dimensions: ";// insert the prompt into the output float width; // define a variable called width float height;// define a variable called height cin >> width >>height;// extract values for width and height from standard input float rectangleArea = width * height; // define and initialize the area cout << “Area: “ << rectangleArea // insert values into the standard output object << “width: “ << width << “height: “ << height << endl; return 0;// end of the program } // end of the main function

16 Area.cpp #include // since we need the insertion and extraction operators using namespace std;// since we need cout and cin int main() {// the main function definition starts here cout << "Rectangle dimensions: ";// insert the prompt into the output float width; // define a variable called width float height;// define a variable called height cin >> width >>height;// extract values for width and height from standard input float rectangleArea = width * height; // define and initialize the area cout << “Area: “ << rectangleArea // insert values into the standard output object << “width: “ << width << “height: “ << height << endl; return 0;// end of the program } // end of the main function

17 Area.cpp #include // since we need the insertion and extraction operators using namespace std;// since we need cout and cin int main() {// the main function definition starts here cout << "Rectangle dimensions: ";// insert the prompt into the output float width; // define a variable called width float height;// define a variable called height cin >> width >>height;// extract values for width and height from standard input float rectangleArea = width * height; // define and initialize the area cout << “Area: “ << rectangleArea // insert values into the standard output object << “width: “ << width << “height: “ << height << endl; return 0;// end of the program } // end of the main function

18 Area.cpp #include // since we need the insertion and extraction operators using namespace std;// since we need cout and cin int main() {// the main function definition starts here cout << "Rectangle dimensions: ";// insert the prompt into the output float width; // define a variable called width float height;// define a variable called height cin >> width >>height;// extract values for width and height from standard input float rectangleArea = width * height; // define and initialize the area cout << “Area: “ << rectangleArea // insert values into the standard output object << “width: “ << width << “height: “ << height << endl; return 0;// end of the program } // end of the main function

19 Area.cpp #include // since we need the insertion and extraction operators using namespace std;// since we need cout and cin int main() {// the main function definition starts here cout << "Rectangle dimensions: ";// insert the prompt into the output float width; // define a variable called width float height;// define a variable called height cin >> width >>height;// extract values for width and height from standard input float rectangleArea = width * height; // define and initialize the area cout << “Area: “ << rectangleArea // insert values into the standard output object << “width: “ << width << “height: “ << height << endl; return 0;// end of the program } // end of the main function

20 Area.cpp #include // since we need the insertion and extraction operators using namespace std;// since we need cout and cin int main() {// the main function definition starts here cout << "Rectangle dimensions: ";// insert the prompt into the output float width; // define a variable called width float height;// define a variable called height cin >> width >>height;// extract values for width and height from standard input float rectangleArea = width * height; // define and initialize the area cout << “Area: “ << rectangleArea // insert values into the standard output object << “width: “ << width << “height: “ << height << endl; return 0;// end of the program } // end of the main function

21 Definition with initialization Definitions Extraction Area.cpp #include // since we need the insertion and extraction operators using namespace std;// since we need cout and cin int main() {// the main function definition starts here cout << "Rectangle dimensions: ";// insert the prompt into the output float width; float height; cin >> width >>height; float rectangleArea = width * height; cout << “Area: “ << rectangleArea << “width: “ << width << “height: “ << height << endl; return 0;// end of the program } // end of the main function

22 Metroworks IDE with Area.cpp

Area.cpp Output

Fundamental C++ Objects C++ has a large number of fundamental or built-in object types The fundamental object types fall into one of three categories –Integer objects –Floating-point objects –Character objects Z 5 P 3.14

Integer Object Types The basic integer object type is int –The size of an int depends on the machine and the compiler On PCs it is normally 32 bits How many different integer numbers can be represented with 32 bits? Other integers object types –short : typically uses less bits (16 bits) –long : sometimes uses more bits (32 bits)

Integer Object Types Different types allow programmers to use resources more efficiently Standard arithmetic and relational operations are available for these types

Integer Constants Integer constants are positive or negative whole numbers Integer constant forms –Decimal –Octal (base 8) Digits 0, 1, 2, 3, 4, 5, 6, 7 –Hexadecimal (base 16) Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, A, B, C, D, E, F

Decimal Constants Examples –97 –40000L –50000 –23a (illegal) The type of the constant depends on its size, unless the type specifier is used L or l indicates long integer

Character Object Types Character type char is related to the integer types Characters are encoded as 8-bit integers ASCII is the dominant encoding scheme –Examples ' ' encoded as 32 '+' encoded as 43 'A' encoded as 65 'Z' encoded as 90 'a' encoded as 97 'z' encoded as 122 Complete ASCII character set in App. A

Character Operations Arithmetic and relational operations are defined for characters types –'a' < 'b' is true –'4' > '3' is true –'6' <= '2' is false

Character Constants Explicit (literal) characters within single quotes –'a','D','*' Special characters - delineated by a backslash \ –Two character sequences (escape codes) –Some important special escape codes \t denotes a tab \n denotes a new line \\ denotes a backslash

Character Constants \' denotes a single quote \" denotes a double quote '\t' is the explicit tab character '\n' is the explicit new line character and so on…

Literal String Constants A literal string constant is a sequence of zero or more characters enclosed in double quotes –"We are even loonier than you think" –"Rust never sleeps\n" –"Nilla is a Labrador Retriever“ –Not a fundamental type

Floating-Point Object Types Floating-point object types represent real numbers –Integer part –Fractional part The number breaks down into the following parts –108 - integer part – fractional part

Floating-Point Object Types C++ provides three floating-point object types –float –double –long double –In most implementations of C++, long double is equivalent to a double

Floating-Point Constants Standard decimal notation F Standard scientific notation 1.45E e-3L When not specified, floating-point constants are of type double F or f indicates single precision floating point value L or l indicates long double floating point value

Names Used to denote program values or components A valid name is a sequence of –Letters (upper and lowercase) –Digits A name cannot start with a digit –Underscores A name should not normally start with an underscore

Names Names are case sensitive –MyObject is a different name than MYOBJECT There are two kinds of names –Keywords –Identifiers

Keywords Keywords are words reserved as part of the language –int, return, float, double They cannot be used by the programmer to name things They consist of lowercase letters only They have special meaning to the compiler

Identifiers Identifiers should be –Short enough to be reasonable to type (single word is norm) Standard abbreviations are fine (but only standard abbreviations) –Long enough to be understandable When using multiple word identifiers capitalize the first letter of each word –Examples Min, Temperature, CameraAngle, CurrentNbrPoints

Definitions All objects that are used in a program must be defined An object definition specifies –Type –Name General definition form –Our convention is one definition per statement!

Examples char Response; int MinElement; float Score; float Temperature; int i; int n; char c; float x; Objects are uninitialized with this definition form (Value of a object is whatever is in its assigned memory location)

Arithmetic Operators Common –Addition+ –Subtraction- –Multiplication* –Division/ –Mod% Write m*x + b not mx + b

Arithmetic Operators Note –No exponentiation operator –Single division operator Integer and float –Operators are overloaded to work with more than one type of object

Integer Division Integer division produces an integer result –Truncates the result Examples –3 / 2 evaluates to 1 –4 / 6 evaluates to 0 –10 / 3 evaluates to 3

Mod Produces the remainder of the division Examples –5 % 2 evaluates to 1 –12 % 4 evaluates to 0 –4 % 5 evaluates to 4

Operators and Precedence Consider mx + b –which of the following is it equivalent to (m * x) + b m * (x + b) –Operator precedence tells how to evaluate expressions

Operators and Precedence Standard precedence order –() Evaluate first, if nested innermost done first –* / % Evaluate second. If there are several, then evaluate from left-to-right –+ - Evaluate third. If there are several, then evaluate from left-to-right

Operator Precedence Examples / 5 * * 5 % 4 (4 / 5) ((4 / 5) * 2) ((4 / 5) * 2) (3 * 5) ((4 / 5) * 2) ((3 * 5) % 4) (20 -((4 / 5) * 2)) ((3 * 5) % 4) (20 -((4 / 5) * 2)) + ((3 * 5) % 4)

Defining and Initializing When an object is defined using the basic form, the memory allotted to it contains random information Better idea to specify its desired value at the same time –Exception is when the next statement is an extraction for the object Remember our convention of one definition per statement!

Examples int FahrenheitFreezing = 32; char FinalGrade = 'A'; cout << "Slope of line: "; float m; cin >> m; cout << "Intercept: "; float b; cin >> b; cout << "X value of interest: "; float x; cin >> x; float y = (m * x) + b;