LECTURE 02 INTRODUCTION TO C++

Similar documents
Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Introduction to C++

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

CHAPTER 3 BASIC INSTRUCTION OF C++

The C++ Language. Arizona State University 1

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

BITG 1233: Introduction to C++

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Lecture 2 Tao Wang 1

Objectives. In this chapter, you will:

C++ Programming: From Problem Analysis to Program Design, Third Edition

Integer Data Types. Data Type. Data Types. int, short int, long int

6.096 Introduction to C++ January (IAP) 2009

REVIEW. The C++ Programming Language. CS 151 Review #2

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Chapter 2: Overview of C++

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Creating a C++ Program

UNIT- 3 Introduction to C++

Computer Programming : C++

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Lab # 02. Basic Elements of C++ _ Part1

Chapter 2 Basic Elements of C++

CSc Introduction to Computing

LECTURE 04 MAKING DECISIONS

Introduction to the C++ Programming Language

2 nd Week Lecture Notes

Fundamental of Programming (C)

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Fundamentals of Programming CS-110. Lecture 2

Fundamentals of Programming

Your First C++ Program. September 1, 2010

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Introduction to Programming EC-105. Lecture 2

Programming. C++ Basics

Chapter Two MULTIPLE CHOICE

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Full file at C How to Program, 6/e Multiple Choice Test Bank

Chapter 2. C++ Basics

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Chapter 2. Outline. Simple C++ Programs

Chapter 2. Lexical Elements & Operators

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Visual C# Instructor s Manual Table of Contents

Full file at

VARIABLES & ASSIGNMENTS

Engineering Problem Solving with C++, Etter/Ingber

Getting started with C++ (Part 2)

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

DEPARTMENT OF MATHS, MJ COLLEGE

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Basics of Java Programming

A First Program - Greeting.cpp

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Your first C++ program

BASIC ELEMENTS OF A COMPUTER PROGRAM

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Unit 3. Constants and Expressions

UEE1302 (1102) F10: Introduction to Computers and Programming

Introduction to Programming

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Topic 2: C++ Programming fundamentals

CS2141 Software Development using C/C++ C++ Basics

Chapter 1 Introduction to Computers and C++ Programming

CS242 COMPUTER PROGRAMMING

Introduction to C++ (Extensions to C)

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Lecture 3 Tao Wang 1

Programming Fundamentals and Methodology. COMP104: C++ Basics

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

CSCE 110 PROGRAMMING FUNDAMENTALS

Chapter 2: Using Data

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

CHAPTER 2 Java Fundamentals

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Introduction of C++ OOP forces us to think in terms of object and the interaction between objects.

ARG! Language Reference Manual

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Chapter 1 INTRODUCTION

Transcription:

PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 02 INTRODUCTION TO C++ IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD THE PART OF A C++ PROGRAM // sample C++ program comment #include <iostream> preprocessor directive using namespace std; which namespace to use int main() beginning of function named main { beginning of block for main cout << "Hello, there!"; output statement string literal return 0; send 0 to operating system } end of block for main 2 Lecture 02 - Introduction to C++ 1

THE PART OF A C++ PROGRAM SPECIAL CHARACTERS 3 THE COUT OBJECT Displays output on the computer screen You use the stream insertion operator << to send output to cout: cout << "Programming is fun!"; Can be used to send more than one item to cout: cout << "Hello " << "there!"; Or: cout << "Hello "; cout << "there!"; This produces one line of output: cout << "Programming is "; cout << "fun!"; 4 Lecture 02 - Introduction to C++ 2

THE COUT OBJECT THE ENDL MANIPULATOR You can use the endl manipulator to start a new line of output. This will produce two lines of output: cout << "Programming is" << endl; cout << "fun!"; Programming is fun! You do NOT put quotation marks around endl The last character in endl is a lowercase L, not the number 1. endl This is a lowercase L 5 THE COUT OBJECT THE \N ESCAPE SEQUENCE You can also use the \n escape sequence to start a new line of output. This will produce two lines of output: cout << "Programming is\n"; cout << "fun!"; Notice that the \n is INSIDE the string. Programming is fun! 6 Lecture 02 - Introduction to C++ 3

THE #INCLUDE DIRECTIVE Inserts the contents of another file into the program This is a preprocessor directive, not part of C++ language #include lines not seen by compiler Do not place a semicolon at end of #include line 7 VARIABLES AND LITERALS VARIABLE a storage location in memory Has a name and a type of data it can hold Must be defined before it can be used: int item; Variable Definition 8 Lecture 02 - Introduction to C++ 4

VARIABLES AND LITERALS LITERALS a value that is written into a program s code. "hello, there" (string literal) 12 (integer literal) 20 is an integer literal 9 VARIABLES AND LITERALS LITERALS a value that is written into a program s code. "hello, there" (string literal) 12 (integer literal) These are string literals 10 Lecture 02 - Introduction to C++ 5

IDENTIFIERS DEFINITION An identifier is a programmer-defined name for some part of a program: variables, functions, etc. 11 IDENTIFIERS C++ KEY WORDS You cannot use any of the C++ key words as an identifier. These words have reserved meaning. 12 Lecture 02 - Introduction to C++ 6

IDENTIFIERS VARIABLE NAMES A variable name should represent the purpose of the variable. For example: itemsordered The purpose of this variable is to hold the number of items ordered. 13 IDENTIFIERS IDENTIFIER RULES The first character of an identifier must be an alphabetic character or and underscore ( _ ), After the first character you may use alphabetic characters, numbers, or underscore characters. Upper- and lowercase characters are distinct 14 Lecture 02 - Introduction to C++ 7

IDENTIFIERS VALID AND INVALID IDENTIFIERS 15 INTEGER DATA TYPES Integer variables can hold whole numbers such as 12, 7, and -99 16 Lecture 02 - Introduction to C++ 8

INTEGER DATA TYPES DEFINING VARIABLES Variables of the same type can be defined On separate lines: int length; int width; unsigned int area; On the same line: int length, width; unsigned int area; Variables of different types must be in different definitions 17 INTEGER DATA TYPES INTEGER TYPES IN PROGRAM This program has three variables: checking, miles, and days 18 Lecture 02 - Introduction to C++ 9

INTEGER DATA TYPES INTEGER LITERALS An integer literal is an integer value that is typed into a program s code. For example: itemsordered = 15; In this code, 15 is an integer literal. 19 INTEGER DATA TYPES INTEGER LITERALS IN PROGRAM Integer Literals 20 Lecture 02 - Introduction to C++ 10

INTEGER DATA TYPES INTEGER LITERALS Integer literals are stored in memory as ints by default To store an integer constant in a long memory location, put L at the end of the number: 1234L Constants that begin with 0 (zero) are base 8: 075 Constants that begin with 0x are base 16: 0x75A 21 THE CHAR DATA TYPE CHARACTER LITERALS Used to hold characters or very small integer values Usually 1 byte of memory Numeric value of character from the character set is stored in memory: CODE: char letter; letter = 'C'; MEMORY: letter 67 Character literals must be enclosed in single quote marks. Example: 'A' 22 Lecture 02 - Introduction to C++ 11

THE CHAR DATA TYPE CHARACTER LITERALS IN PROGRAM 23 THE CHAR DATA TYPE CHARACTER STRING A series of characters in consecutive memory locations: "Hello" Stored with the null terminator, \0, at the end: Comprised of the characters between the " " 24 Lecture 02 - Introduction to C++ 12

THE C++ STRING CLASS Special data type supports working with strings #include <string> 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; 25 THE C++ STRING CLASS IN PROGRAM 26 Lecture 02 - Introduction to C++ 13

FLOATING-POINT DATA TYPES The floating-point data types are: float double long double They can hold real numbers such as: 12.45-3.8 Stored in a form similar to scientific notation All floating-point numbers are signed 27 FLOATING-POINT DATA TYPES FLOATING-POINT LITERALS Can be represented in Fixed point (decimal) notation: 31.4159 0.0000625 E notation: 3.14159E1 6.25e-5 Are double by default Can be forced to be float (3.14159f) or long double (0.0000625L) 28 Lecture 02 - Introduction to C++ 14

FLOATING-POINT DATA TYPES IN PROGRAM 29 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 alldone = true; bool finished = false; alldone finished 1 0 30 Lecture 02 - Introduction to C++ 15

THE BOOL DATA TYPE IN PROGRAM 31 DETERMINING 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"; 32 Lecture 02 - Introduction to C++ 16

VARIABLE ASSIGNMENTS AND INITIALIZATION ASSIGNMENT An assignment statement uses the = operator to store a value in a variable. item = 12; This statement assigns the value 12 to the item variable. The variable receiving the value must appear on the left side of the = operator. This will NOT work: // ERROR! 12 = item; 33 VARIABLE ASSIGNMENTS AND INITIALIZATION 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; 34 Lecture 02 - Introduction to C++ 17

SCOPE 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 35 ARITHMETIC OPERATORS Used for performing numeric calculations C++ has unary, binary, and ternary operators: unary (1 operand) -5 binary (2 operands) 13-7 ternary (3 operands) exp1? exp2 : exp3 36 Lecture 02 - Introduction to C++ 18

ARITHMETIC OPERATORS BINARY ARITHMETIC OPERATORS 37 ARITHMETIC OPERATORS IN PROGRAM 38 Lecture 02 - Introduction to C++ 19

ARITHMETIC OPERATORS A CLOSER LOOK AT THE / OPERATOR / (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 39 ARITHMETIC OPERATORS A CLOSER LOOK AT THE % OPERATOR % (modulus) operator computes the remainder resulting from integer division cout << 13 % 5; // displays 3 % requires integers for both operands cout << 13 % 5.0; // error 40 Lecture 02 - Introduction to C++ 20

COMMENTS Used to document parts of the program Intended for persons reading the source code of the program: Indicate the purpose of the program Describe the use of variables Explain complex sections of code Are ignored by the compiler 41 COMMENTS SINGLE LINE COMMENTS Begin with // through to the end of line: int length = 12; // length in inches int width = 15; // width in inches int area; // calculated area // calculate rectangle area area = length * width; 42 Lecture 02 - Introduction to C++ 21

COMMENTS MULTIPLE LINE COMMENTS Begin with /*, end with */ Can span multiple lines: /* this is a multi-line comment */ Can begin and end on the same line: int area; /* calculated area */ 43 NAMED CONSTANTS Named constant (constant variable): variable whose content cannot be changed during program execution Used for representing constant values with descriptive names: const double TAX_RATE = 0.0675; const int NUM_STATES = 50; Often named in uppercase letters 44 Lecture 02 - Introduction to C++ 22

NAMED CONSTANTS IN PROGRAM 45 PROGRAMMING STYLE The visual organization of the source code Includes the use of spaces, tabs, and blank lines Does not affect the syntax of the program Affects the readability of the source code 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 46 Lecture 02 - Introduction to C++ 23

STANDARD AND PRESTANDARD C++ Older-style C++ programs: Use.h at end of header files: #include <iostream.h> Use #define preprocessor directive instead of const definitions Do not use using namespace convention May not compile with a standard C++ compiler 47 STANDARD AND PRESTANDARD C++ IN PROGRAM 48 Lecture 02 - Introduction to C++ 24