Вы находитесь на странице: 1из 30

CLOSER LOOK INTO C++ Lecture 3

PROGRAMMING Apr 29, 2019


COURSE BOOKS AND COMPILER
1. Starting Out with C++ From Control Structures
through Objects, 7th Edition by Tony Gaddis
2. C++ How to Program, 5th Edition by Dietel and
Dietel
3. C Programming Language, 2nd edition by Dennis
Ritchie and Kernighan Brian

Download Dev C++


https://filehippo.com/download_dev-c/

Copyright © 2012 Pearson Education, Inc.


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;

Copyright © 2012 Pearson Education, Inc.


VARIABLE DEFINITION IN PROGRAM

Variable Definition

Copyright © 2012 Pearson Education, Inc.


LITERALS

Literal: a value that is written into a program’s code.

"hello, there" (string literal)


12 (integer literal)

Copyright © 2012 Pearson Education, Inc.


INTEGER LITERAL IN PROGRAM

20 is an integer literal

Copyright © 2012 Pearson Education, Inc.


STRING LITERALS IN PROGRAM

These are string literals

Copyright © 2012 Pearson Education, Inc.


CONSTANTS

Constants: a storage location in memory similar to


variable but its value can not be changed when
assigned.
 Has a name and a type of data it can hold
 Must be defined before it can be used
 Its value can not be updated

const double PI = 3.14;


const double STANDARD_GRAVITY = 9.8065

Copyright © 2012 Pearson Education, Inc.


C++ KEY WORDS
You cannot use any of the C++ key words as an identifier.
These words have reserved meaning.

Copyright © 2012 Pearson Education, Inc.


VARIABLE NAMES

A variable name should represent the purpose of the


variable. For example:

items, itemsPurchased, items_count


marks, obtainedMarks, total_marks

Copyright © 2012 Pearson Education, Inc.


IDENTIFIERS

An identifier is a programmer-defined name for some


part of a program: variables, constants, functions, etc.

Copyright © 2012 Pearson Education, Inc.


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
Items and items are two different identifiers
_items is a valid name

Copyright © 2012 Pearson Education, Inc.


VALID AND INVALID IDENTIFIERS
IDENTIFIER VALID? REASON IF INVALID

totalSales

total_Sales

total.Sales

4thQtrSales

totalSale$

Copyright © 2012 Pearson Education, Inc.


VALID AND INVALID IDENTIFIERS
IDENTIFIER VALID? REASON IF INVALID

totalSales Yes

total_Sales Yes

total.Sales No Cannot contain .

4thQtrSales No Cannot begin with digit

totalSale$ No Cannot contain $

Copyright © 2012 Pearson Education, Inc.


INTEGER DATA TYPES
• Integer variables can hold whole numbers such as 12,
7, and -99.

Copyright © 2012 Pearson Education, Inc.


DEFINING VARIABLES
Variables of the same type can be defined
- On separate lines:
int length;
int width;
double salary;
double tax;
- On the same line:
int length, width;
double salary, tax;

Variables of different types must be in different


definitions

Copyright © 2012 Pearson Education, Inc.


INTEGER TYPES IN PROGRAM 2-10

This program has three variables: checking,


miles, and days

Copyright © 2012 Pearson Education, Inc.


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.

Copyright © 2012 Pearson Education, Inc.


INTEGER LITERALS IN PROGRAM 2-10

Integer Literals

Copyright © 2012 Pearson Education, Inc.


THE CHAR DATA TYPE

Used to hold characters or single digit numeric values


Usually 1 byte of memory
Numeric value of character from the character set is
stored in memory:

CODE: MEMORY:
char letter; letter
letter = 'C';
67

Copyright © 2012 Pearson Education, Inc.


CHARACTER LITERALS

Character literals must be enclosed in single quote


marks. Example:
'A'

Copyright © 2012 Pearson Education, Inc.


CHARACTER LITERALS IN PROGRAM

Copyright © 2012 Pearson Education, Inc.


CHARACTER STRINGS

A series of characters in consecutive memory


locations:
"Hello"
Stored with the null terminator, \0, at the end:

Comprised of the characters between the " "


H e l l o \0

Copyright © 2012 Pearson Education, Inc.


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;

Copyright © 2012 Pearson Education, Inc.


THE STRING CLASS IN PROGRAM

Copyright © 2012 Pearson Education, Inc.


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

Copyright © 2012 Pearson Education, Inc.


FLOATING-POINT DATA TYPES

Copyright © 2012 Pearson Education, Inc.


FLOATING-POINT DATA TYPES
double has 2x more precision then float.

float is a 4 byte (32 bit) IEEE 754 single precision Floating


Point Number 1 bit for the sign, (8 bits for the exponent,
and 23* for the value), i.e. float has 7 decimal digits of
precision.

double is an 8 byte (64 bit) IEEE 754 double precision


Floating Point Number (1 bit for the sign, 11 bits for the
exponent, and 52* bits for the value), i.e. double has 15
decimal digits of precision.

Copyright © 2012 Pearson Education, Inc.


FLOATING-POINT LITERALS
Can be represented in
 Fixed point (decimal) notation:
31.4159 0.0000625
 E notation:
3.14159E1 6.25e-5

Copyright © 2012 Pearson Education, Inc.


FLOATING-POINT DATA TYPES

Copyright © 2012 Pearson Education, Inc.

Вам также может понравиться