Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor

Slides:



Advertisements
Similar presentations
Introducing C++ Elements. CSCE 1062 Outline Main algorithms’ constructs General form of a C++ program {section 2.5} C++ language elements {section 2.1}
Advertisements

Chapter 2: Basic Elements of C++
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.
1 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
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 2: Introduction to C++.
Chapter 2: Basic Elements of C++
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
By Dr. Awad Khalil Computer Science & Engineering Department
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Input & Output: Console
Chapter 2 Overview of C++ Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
Lecture 4: Computer Languages. A Tutorial Introduction to C++ COS120 Software Development Using C++ AUBG, COS dept.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Overview of C++ Problem Solving, Abstraction, and Design using.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
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.
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.
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.
1 C++ Syntax and Semantics, and the Program Development Process.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
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:
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Bill Tucker Austin Community College COSC 1315
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
What Actions Do We Have Part 1
Chapter 1.2 Introduction to C++ Programming
Documentation Need to have documentation in all programs
Computing Fundamentals
Basic Elements of C++.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
C++ Data Types Data Type
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
What Actions Do We Have Part 1
Chapter 2: Overview of C++
Presentation transcript:

Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor

3.1 C++ Language Elements Comments Compiler directives Function main Declaration statements Executable statements 2

Comments // symbols indicate a line comment - apply to just the rest of the line Block comments start with /* and end with */ - apply to as many lines as you like Used to describe the code in English or provide non-code information E.g. to include the name of the program or the author’s name 3

#include Compiler directive Includes previously written code from a library into your program E.g. #include has operators for performing input and output within the program Libraries allow for code reuse 4

using namespace std; Indicates to compiler that this program uses objects defined by a standard namespace called std. Ends with a semicolon Follows #include directives in the code Must appear in all programs 5

Function main int main ( ) { // function body } 6

Function main Exactly one main function per program A function is a collection of related statements that perform a specific operation int indicates the return type of the function ( ) indicates no special information passed to the function by the operating system 7

Types of Statements Declaration statements - describe the data the function needs: const float KM_PER_MILE = 1.609; float miles, kms; Executable statements - specify the actions the program will take: cout << “Enter the distance in miles: “; cin >> miles; 8

3.2 Reserved Words (Keywords) Have special meaning in C++ Cannot be used for other purposes 9

Listing 1 Converting miles to kilometers 10

Reserved words in Listing 1 11

Identifiers Names for data and objects to be manipulated by the program Must begin with a letter or underscore (not recommended) Consist only of letters, digits, and underscores Cannot be reserved word Upper and lower case significant 12

Identifiers IdentifierUse cin C++ name for standard input stream cout C++ name for standard output stream km Data element for storing distance in kilometers KM_PER_MILE Conversion constant miles Data element for storing distance in miles std C++ name for the standard namespace 13

3.3 Data Types Defines a set of values and operations that can be performed on those values integers positive and negative whole numbers, e.g. 5, -52, short, int, long represented internally in binary predefined constants INT_MIN and INT_MAX 14

Data Types (con.) Floating point (real) number has two parts, integral and fractional e.g. 5, , , 5.0 float, double, long double stored internally in binary as mantissa and exponent 10.0 and 10 are stored differently in memory 15

Data Types (con.) Boolean named for George Boole represent conditional values values: true and false 16

Data Types (con’t) Characters represent individual character values E.g. ’A’ ’a’ ’2’ ’*’ ’”’ ’ ’ stored in 1 byte of memory special characters: escape sequences E.g. ’\n’ ’\b’ ’\r’ ’\t’ ‘\’’ 17

string Class Strings not built-in, but come from library Classes extend C++ string literal enclosed in double quotes E.g.: “Enter speed: “ “ABC” “B” “true” “1234” #include for using string identifiers, but not needed for literals 18

Variable Declarations Set aside memory with a specific name for the data and define its values and operations The value can change during execution type identifier-list; E.g.:float x, y; int me, you; float week = 40.0; string flower = “rose”; 19

Constant Declarations Memory cells whose values cannot change once set const type constant-identifier = value; E.g.: const float KM_PER_MILE = 1.609; Often identified by using all upper case name 20

Listing 2 Printing a welcoming message 21

3.4 Executable Statements Assignment statements Input statements Program output The return statement 22

Memory (a) before and (b) after execution of a program 23

Assignment Statements variable = expression; E.g.: kms = KM_PER_MILE * miles; 24

Input Statements Obtain data for program to use - different each time program executes Standard stream library iostream cin - name of stream associated with standard input device (keyboard by default) Extraction operator (>>) E.g.: cin >> miles; cin >> age >> firstInitial; 25

In Listing 2: cin >> letter1 >> letter2 >> lastname; has the effect: 26

Effect of cin >> letter1 >> letter2 >> lastname; 27

Program Output Used to display results of program Also standard stream library iostream cout - name of stream associated with standard output device (monitor by default) Insertion operator (<<) for each element cout << data-element; 28

Program Output cout statement can be broken across lines Strings cannot be broken across lines Prompt messages used to inform program user to enter data Screen cursor is a moving marker indicating the position of where the next character will be displayed endl (or ‘\n’) causes a new line in output 29

Output Example cout << “The distance in kilometers is “ << kms << endl; If variable kms has value 16.09, the output is: The distance in kilometers is

The return Statement Last line of main function is typically return 0; This transfers control from the program to the operating system, indicating that no error has occurred 31

Listing 3 General Form of a C++ Program 32