Chapter 2 Introduction to C++ Programming

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.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
 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
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 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.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 CISC 1600 – Computer Science I Fall 2010 Introduction to C++ Programming Chapters 1 and 2 (Deitel.
© 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.
 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.
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.
© 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++
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
Chapter 02 (Part II) Introduction to C++ Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
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.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
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 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 PART #1 C++ PROGRAM STRUCTURE
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
1.2 What is a Computer? Computer Computer programs Hardware Software
Chapter 2 - Introduction to C Programming
Chapter 2, Part I Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

Chapter 2 Introduction to C++ Programming C++, How to Program Deitel & Deitel Spring 2010 CS1600 Yanjun Li

Learn C++ by Examples Five examples demonstrate How to display messages How to obtain information from the user How to perform arithmetic calculations Spring 2010 CS1600 Yanjun Li

Important Parts of a C++ program Comments: //, /* …. */ Preprocessor directives : #include Function main Body of the function Return statement Other statements Spring 2010 CS1600 Yanjun Li

Comments Explain programs to other programmers Ignored by compiler Improve program readability Ignored by compiler Single-line comment Begin with // Example //allows program to output data to the screen. Multi-line comment Start with /* End with */ Spring 2010 CS1600 Yanjun Li

Preprocessor Directives Processed by preprocessor before compiling Begin with # Example #include <iostream> Tells preprocessor to include the input/output stream header file <iostream> White spaces Blank lines, space characters and tabs Delimiter, used to make programs easier to read Extra spaces are ignored by the compiler Spring 2010 CS1600 Yanjun Li

Function main A part of every C++ program Exactly one function in a program must be main main is a Keyword. Keyword : A word in code that is reserved by C++ for a specific use. Header of function main : int main( ) Body is delimited by braces ({ }) Spring 2010 CS1600 Yanjun Li

Statements Instruct the program to perform an action All statements end with a semicolon (;) Examples : return 0; std::cout << “Welcome to C++!\n ”; Spring 2010 CS1600 Yanjun Li

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; Spring 2010 CS1600 Yanjun Li

Output Statement (1) std::cout << “Welcome to C++!\n”; std::cout Standard output stream object. Defined in input/output stream header file <iostream> We are using a name (cout) that belongs to “namespace” std. Normally outputs to computer screen. Stream insertion operator << Value to right (right operand) inserted into left operand. The string of characters contained between “ ” after the operator << shows on computer screen. Spring 2010 CS1600 Yanjun Li

Output Statement (2) Escape character : backslash : "\" Escape sequence : A character preceded by backslash (\) Indicates “special” character output Examples : "\n" Newline. Cursor moves to beginning of next line on the screen “\t” Horizontal tab. Move the screen cursor to the next tab stop. Spring 2010 CS1600 Yanjun Li

Good Programming Practices Add comments Every program should begin with a comment that describes the purpose of the program, author, date and time. Use good indentation Indent the entire body of each function one level within the braces that delimit the body of the function. This makes a program’s functional structure stand out and helps make the program easier to read. Spring 2010 CS1600 Yanjun Li

Run C++ Program Save the program with the right extension programOne.cpp Send the program in the right directory Compiling the program: g++ programOne.cpp –o programOne.out Run the executable file ./programOne.out Spring 2010 CS1600 Yanjun Li

Modifying Our First C++ Program Print text on one line using multiple statements Each stream insertion resumes printing where the previous one stopped Statements: Std::cout << “Welcome ”; Std::cout << “to C++!\n”; Spring 2010 CS1600 Yanjun Li

Modifying Our First C++ Program Print text on several lines using a single statement. Each newline escape sequence positions the cursor to the beginning of the next line Two newline characters back to back outputs a blank line Example statement : Std::cout << “Welcome\nto\n\nC++!\n”; Spring 2010 CS1600 Yanjun Li

Variable Location in memory where value can be stored Common data types (fundamental, primitive or built-in) int – integer numbers : 1, 2, 4,…. char – characters : ‘a’, ‘c’, … float, double – floating point numbers: 2.5, 4.96 The value of a variable could be changed while the program is running. Spring 2010 CS1600 Yanjun Li

Declaration of Variables (1) Declare variables with name and data type before they are used. A variable name is any valid identifier that is not a keyword. Series of characters - letters, digits, underscores ( _ ) Cannot begin with digit Case sensitive Choosing meaningful identifiers helps make a program self-documenting. Spring 2010 CS1600 Yanjun Li

Declaration of Variables (2) Can Declare each variable on a separate line. int integer1; int integer2; int sum; Can declare several variables of same type in one declaration. Comma-separated list int integer1, integer2, sum; Spring 2010 CS1600 Yanjun Li

Assign Value to Variables Assignment operator = Assigns value on right to variable on left Binary operator (two operands) Assign a value after declaration int integer1; //declaration integer1 = 10; //assignment Declare and assign a value at the same time. int integer2 = 20; Spring 2010 CS1600 Yanjun Li

Input stream object std::cin from <iostream> Usually connected to keyboard Stream extraction operator >> Waits for user to input value, press Enter (Return) key Stores value in variable to right of operator Converts value to variable data type Example int number1; std::cin >> number1; Reads an integer typed at the keyboard Stores the integer in variable number1 Spring 2010 CS1600 Yanjun Li

Constant variables Declared using the const qualifier Also called named constants or read-only variables Must be initialized with a constant expression when they are declared and cannot be modified thereafter Example: const int size = 5; Spring 2010 CS1600 Yanjun Li

Arithmetic (1) Arithmetic operators + : Addition - : Subtraction * : Multiplication / : Division Integer division truncates remainder 7 / 5 evaluates to 1 % : Modulus operator returns remainder 7 % 5 evaluates to 2 Attempting to use the modulus operator (%) with non-integer operands is a compilation error. Spring 2010 CS1600 Yanjun Li

Arithmetic operators Spring 2010 CS1600 Yanjun Li

Arithmetic (2) Straight-line form Grouping sub-expressions Required for arithmetic expressions in C++ All constants, variables and operators appear in a straight line Grouping sub-expressions Parentheses are used in C++ expressions to group sub-expressions Same manner as in algebraic expressions Example a * ( b + c ) Multiple a times the quantity b + c Spring 2010 CS1600 Yanjun Li

Rules of operator precedence Operators in parentheses evaluated first Nested/embedded parentheses Operators in innermost pair first Multiplication, division, modulus applied next Operators applied from left to right Addition, subtraction applied last Spring 2010 CS1600 Yanjun Li

Precedence of Arithmetic Operators Spring 2010 CS1600 Yanjun Li

Performing Arithmetic Calculation in C++ Program Adding two integers int number1, number2, sum; std::cin >> number1; std::cin >> number2; sum = number1 + number2; Add the values of variable1 and variable2 Store result in sum Spring 2010 CS1600 Yanjun Li

Question Question : Which statement is correct , A or B ? int integerOne = 1; double doubleOne = 2.5; int sumOne = integerOne + doubleOne; //A double sumTwo = integerOne + doubleOne; //B Which statement is correct , A or B ? Spring 2010 CS1600 Yanjun Li

Output Statement With Expression Variables or expressions could be inserted into output statement, its value is printed out. Long output statement could be broken into multiple lines. Example: std::cout << “sum is ” << sum << “. bye-bye! \n”; std::cout << “sum is ” << number1 + number2 ; Spring 2010 CS1600 Yanjun Li

End of Line Stream manipulator std::endl Outputs a newline Flushes the output buffer Spring 2010 CS1600 Yanjun Li

Memory Concepts Variable names Correspond to actual locations in computer's memory Every variable has name, type, size and value When new value placed into variable, overwrites old value Writing to memory is destructive Reading variables from memory nondestructive Example sum = number1 + number2; Value of sum is overwritten Values of number1 and number2 remain intact Spring 2010 CS1600 Yanjun Li

Fig. 2.6 | Memory location showing the name and value of variable number1. Spring 2010 CS1600 Yanjun Li

Fig. 2.7 | Memory locations after storing values for number1 and number2. Spring 2010 CS1600 Yanjun Li

Fig. 2.8 | Memory locations after calculating and storing the sum of number1 and number2. Spring 2010 CS1600 Yanjun Li

Type Sizes and Ranges The size and range of any data type is compiler and architecture dependent. Many architectures implement data types of a standard size. ints and floats are often 32-bit, chars 8-bit, and doubles are usually 64-bit. Spring 2010 CS1600 Yanjun Li

Reference Reproduced from the Cyber Classroom for C++, How to Program, 5/e by Deitel & Deitel. Reproduced by permission of Pearson Education, Inc. Spring 2010 CS1600 Yanjun Li