CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.

Slides:



Advertisements
Similar presentations
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Advertisements

© 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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Data types and variables
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Chapter 2: Basic Elements of C++
Starting Out with C++, 3 rd Edition 1 Chapter 2. Introduction to C++
Basic Elements of C++ Chapter 2.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
Input & Output: Console
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
Introduction to C The Parts of a C++ Program –Anatomy of a simple C++ program.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Lecture 3: The parts of a C++ program (Cont’d) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 2 Introduction to C++ Department of Computer Science Missouri State University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
Introduction to C++. 2 What Is a Program Made Of?
1 Chapter 2 Introduction to C++. 2 Topics 2.1 Parts of a C++ Program 2.2 The cout Object 2.3 The #include Directive 2.4 Variables and Constants 2.5 Identifiers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Lecture 2 Variables, Types, Operators Sampath Jayarathna Cal Poly Pomona Based on slides created by Bjarne Stroustrup & Tony Gaddis CS 128 Introduction.
Bill Tucker Austin Community College COSC 1315
Chapter 2: Basic Elements of C++
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
LESSON 2 Basic of C++.
Chapter 2: Introduction to C++
Basic Elements of C++.
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Chapter 2: Introduction to C++
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Lecture 2 Variables, Types, Operators
Subject:Object oriented programming
Presentation transcript:

CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1

Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout << "Hello, there!"; return 0; } CS1 Lesson 2 -- John Cole2

Notes: Preprocessor directives begin with # (pound sign). For #include, the file name is in brackets if it is one of the standard headers, in quotes if it is one of yours. CS1 Lesson 2 -- John Cole3

The cout Object Displays output on the screen (the stdout device) Use the << (stream insertion) operator. You can string these together: – cout << “The values is” << x; CS1 Lesson 2 -- John Cole4

The endl Manipulator endl (end line) makes the stream go to a new line. cout << “First line” << endl << “second line”; Note that the last character is the lower-case letter “L”, not the digit “1”. (You can also use \n in a string: cout << “First line\nSecond line”; We’ll discuss other manipulators later. CS1 Lesson 2 -- John Cole5

The #include Directive Any line that starts with a # sign is not an actual C++ statement. It directs the compiler to do something. #include directs the compiler to include a source file as though you had typed it in line. There is no semicolon at the end of this directive. CS1 Lesson 2 -- John Cole6

Variables Variable: a storage location in memory – Has a name and a type of data it can hold – Can be changed by your program – Must be defined before it can be used: int item; item = 1; CS1 Lesson 2 -- John Cole7

Literals A value (including strings) written into the program code. Examples: – “Hello World”// string – // double – ‘z’// char Avoid using literals. They can make your program inflexible. CS1 Lesson 2 -- John Cole8

Identifiers Any programmer-defined name, including the names of variables. Function names, class names, etc. are identifiers. C++ keywords cannot be used as identifiers CS1 Lesson 2 -- John Cole9

C++ Keywords CS1 Lesson 2 -- John Cole10

Identifier Rules The first character must be a letter or an underscore (_) After the first character you may use letters, numbers, and underscores Upper- and lower-case letters are distinct. CS1 Lesson 2 -- John Cole11

Identifier Conventions Identifiers for methods and variables generally begin with a lower-case letter. If contains more than one word, the second word is capitalized: itemsOrdered; totalSalesTax Class names begin with a capital letter. Named constants are usually all upper case: const double PI = ; Avoid single-letter identifiers in all but the simplest cases. CS1 Lesson 2 -- John Cole12

Integer Data Types CS1 Lesson 2 -- John Cole13

Integers, continued When you use an integer literal, it is by default stored as an int. To make it a long, follow the number with ‘L’: – 1234 is an int – 1234L is a long. Integer constants that begin with 0 (zero) are octal: 061. (089 is not valid) Integer constants that begin with 0x are hexadecimal: 0xFF, 0x1A CS1 Lesson 2 -- John Cole14

The char Data Type This represents a single character. It can also be treated as an integer. You can do arithmetic on them. In ASCII encoding, these take one byte of memory. The numeric value of the character is stored: – char columnID = ‘A’;// stores 65 – char row = ‘2’;// stores 50 CS1 Lesson 2 -- John Cole15

Character Strings A series of characters stored in memory is called a string. String literals are enclosed in quotes, and are stored with a binary zero (null) at the end. “Hello” is stored as: CS1 Lesson 2 -- John Cole16

The C++ string class Special data type supports working with strings #include Can define string variables in programs: string firstName, lastName; Can receive values with assignment operator: firstName = "George"; lastName = "Washington"; Can be displayed via cout cout << firstName << " " << lastName; CS1 Lesson 2 -- John Cole17

Floating-Point Data Types The floating-point data types are: float double long double They can hold real numbers such as: Stored in a form similar to scientific notation All floating-point numbers are signed CS1 Lesson 2 -- John Cole18

Floating Point CS1 Lesson 2 -- John Cole19

Floating Point Literals Can be represented in – Fixed point (decimal) notation: – E notation: E16.25e-5 Are double by default Can be forced to be float ( f ) or long double ( L ) CS1 Lesson 2 -- John Cole20

The bool Data Type Represents values that are true or false bool variables are stored as small integers false is represented by 0, true by 1: bool bRun = true; bool finished = false; CS1 Lesson 2 -- John Cole21

The Size of a Data Type The sizeof operator gives the size of any data type or variable: double amount; cout << "A double is stored in " << sizeof(double) << "bytes\n"; cout << "Variable amount is stored in " << sizeof(amount) << "bytes\n"; CS1 Lesson 2 -- John Cole22

Assignment An assignment statement uses the = operator to store a value in a variable. item = 12; long fedDeficit= L; This statement assigns the value 12 to the item variable. CS1 Lesson 2 -- John Cole23

Variable Initialization To initialize a variable means to assign it a value when it is defined: int length = 12; Can initialize some or all variables: int length = 12, width = 5, area; CS1 Lesson 2 -- John Cole24

Scope of Variables The scope of a variable: the part of the program in which the variable can be accessed A variable cannot be used before it is defined int main() { cout << value;// Error int value = 31; } CS1 Lesson 2 -- John Cole25

Scope, continued for (int iy=0; iy<10; iy++) { cout << iy; } cout << iy; // iy is out of scope here. CS1 Lesson 2 -- John Cole26

Arithmetic Operators Used for performing numeric calculations C++ has unary, binary, and ternary operators: – unary (1 operand) -5 – binary (2 operands) – ternary (3 operands) exp1 ? exp2 : exp3 CS1 Lesson 2 -- John Cole27

Binary Arithmetic Operators CS1 Lesson 2 -- John Cole28

Division / (division) operator performs integer division if both operands are integers cout << 13 / 5; // displays 2 cout << 91 / 7; // displays 13 If either operand is floating point, the result is floating point cout << 13 / 5.0; // displays 2.6 cout << 91.0 / 7; // displays 13.0 CS1 Lesson 2 -- John Cole29

Modulus % (modulus) operator computes the remainder resulting from integer division cout << 13 % 5; // displays 3 % requires integers for both operands cout << 13 % 5.0; // error CS1 Lesson 2 -- John Cole30

Comments Use comments to explain to yourself and other programmers (including your instructor and TAs) what your program does. The compiler ignores comments, so they don’t increase code size. See the Web site for more notes on comments CS1 Lesson 2 -- John Cole31

Comment Types A single-line comment is anything following a // (Unless the // is in quotes) You can delimit comments with /* and */ and span multiple lines. You can also use this style within a line: calculate(double rate, /* tax rate*/, double amount); CS1 Lesson 2 -- John Cole32

Named Constants Use the “const” modifier to indicate that a variable really isn’t: const int MAX_RECORDS = 100; const double PI = ; Use these in place of literals, especially if you use the value more than once. Avoid using #define CS1 Lesson 2 -- John Cole33

Programming Style Make your programs look visually pleasing and readable. Use good naming conventions. Common elements to improve readability: Braces { } aligned vertically Indentation of statements within a set of braces Blank lines between declaration and other statements Long statements wrapped over multiple lines with aligned operators CS1 Lesson 2 -- John Cole34

Standard and Prestandard C++ Older-style C++ programs: – Use.h at end of header files: – #include – Use #define preprocessor directive instead of const definitions – Do not use using namespace convention – May not compile with a standard C++ compiler CS1 Lesson 2 -- John Cole35