C++ Programming Fundamentals II: Basics of C++ Elements and Input/Output, Assignments of Computer Science

A lecture note from a c++ programming fundamentals ii course during summer 2008. It covers the basics of c++ elements, including functions, special symbols, identifiers, simple data types, and basic input/output. The concepts of data types, variables, assignment statements, arithmetic operators, and input/output streams.

Typology: Assignments

Pre 2010

Uploaded on 08/18/2009

koofers-user-oip
koofers-user-oip 🇺🇸

8 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI 152
Programming Fundamentals II
Summer 2008
Basics C++ Elements &
Basic Input / Output
June 5, 2008
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download C++ Programming Fundamentals II: Basics of C++ Elements and Input/Output and more Assignments Computer Science in PDF only on Docsity!

CSCI 152

Programming Fundamentals II

Summer 2008

Basics C++ Elements &

Basic Input / Output

June 5, 2008

Objectives

(Basic Elements Ch #2)

In this lecture/chapter you should review and:  (^) Become familiar with the basic components of a C++ program, including functions, special symbols, and identifiers  (^) Explore simple data types and examine the string data type  (^) Discover how to use arithmetic operators  (^) Examine how a program evaluates arithmetic expressions  (^) Learn what an assignment statement is and what

C++ Programs

A C++ program is a collection of one or

more subprograms, called functions

 A subprogram or a function is a collection

of statements that, when activated

(executed), accomplishes something

 Every C++ program has a function called

main

 The smallest individual unit of a program

Data Types

 Data Type: set of values together

with a set of operations is called a

data type

C++ data can be classified into

three categories:

 (^) Simple data type  (^) Structured data type  (^) Pointers

int Data Type

 Examples:

 Positive integers do not have to

have a + sign in front of them

 No commas are used within an

integer

Commas are used for separating

bool Data Type

 bool type

 (^) Has two values, true and false  (^) Manipulate logical (Boolean) expressions

 True and false are called logical

values

 bool, true, and false are reserved

Floating-Point Data Types

 Float: represents any real number

 (^) Range: -3.4E+38 to 3.4E+

 Memory allocated for the float type is 4

bytes

 Double: represents any real number

 (^) Range: -1.7E+308 to 1.7E+

 Memory allocated for double type is 8

bytes

string Data Type

 (^) Programmer-defined type supplied in standard library  (^) Sequence of zero or more characters  (^) Enclosed in double quotation marks  (^) Null: a string with no characters  (^) Each character has relative position in string  (^) Position of first character is 0, the position of the second is 1, and so on  (^) Length: number of characters in string

Assignment Statement

 (^) The assignment statement takes the form: variable = expression;  (^) Expression is evaluated and its value is assigned to the variable on the left side  (^) In C++ = is called the assignment operator  (^) A C++ statement such as: i = i + 2; evaluates whatever is in i, adds two to it,

Arithmetic Operators

C++ Operators

 (^) + addition  (^) - subtraction  (^) * multiplication  (^) / division  (^) % remainder (mod operator)

 +, -, *, and / can use with integral

and floating-point data types

 Unary operator - has only one

operand

Objectives

(Basic Input/Output Ch

In this lecture/chapter you should review and:  (^) Discover how to input data into memory using input statements  (^) Examine ways to output results using output statements  (^) Learn what a stream is and examine input and output streams  (^) Explore how to read data from the standard input device  (^) Learn how to write data to the standard output device

Input/Output Streams

 (^) I/O: sequence of bytes (stream of bytes) from source to destination  (^) Bytes are usually characters, unless program requires other types of information  (^) Stream: sequence of characters from source to destination  (^) Input Stream: sequence of characters from an input device to the computer  (^) Output Stream: sequence of characters from the computer to an output device

Using cin and cout in a

Program and namespace

 cin and cout are declared in the

header file iostream, but within a

namespace named std

To use cin and cout in a program,

use the following two statements:

#include

using namespace std;

Input (Read) Statement

 (^) cin is used with >> to gather input cin>>variable>>variable.. .;  (^) The extraction operator is >>  (^) For example, if miles is a double variable cin>>miles;  (^) Causes computer to get a value of type double  (^) Places it in the memory cell miles