Sei sulla pagina 1di 17

# Starting Out with C++: Early Objects

5th Edition
Chapter 2
Introduction to C++
(Part III)

## 2006 Pearson Education.

Topics
2.12 Determining the Size of a Data Type
2.13 More on Variable Assignments and
Initialization
2.14 Scope
2.15 Arithmetic Operators
2.17 Focus on Software Engineering:
Programming Style
Chapter 2 Starting Out with

## 2006 Pearson Education.

Sections 2.12 to 2.17

## 2.12 Determining the Size of a

Data Type
The sizeof operator gives the size of any
data type OR variable (in no. of bytes)
double amount;
cout << "A float is stored in "
<< sizeof(float) << "bytes\n";
cout << "Variable amount is stored in "
<< sizeof(amount) << "bytes\n";
Chapter 2 Starting Out with

## 2.13 More on Variable

Assignments and Initialization
Assigning a value to a variable
Assigns a value to a previously created variable
A single variable name must appear on left side
of the = symbol
int size;
size = 5;
// legal
5 = size;
// not legal
Chapter 2 Starting Out with

## Variable Assignment vs.

Initialization
Initializing a variable
Gives an initial value to a variable at the time
it is created
Can initialize some or all variables
int length = 12;
int width = 7, height = 5, area;
Chapter 2 Starting Out with

## 2006 Pearson Education.

2.14 Scope
The scope of a variable
That part of the program where the variable
can be used
A variable cannot be used before it is defined
int
cin
cin
int

a;
>> a;
>> b;
b;

// legal
// illegal

## 2.15 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

Chapter 2 Starting Out with

## Binary Arithmetic Operators

SYMBOL

OPERATION

EXAMPLE

ans

ans = 7 + 3;

10

subtraction

ans = 7 - 3;

multiplication ans = 7 * 3;

21

division

ans = 7 / 3;

modulus

ans = 7 % 3;

## 2006 Pearson Education.

/ Operator
C++ division operator (/)performs integer
division if both operands are integers
cout << 13 / 5;
cout << 2 / 4;

// displays 2
// displays 0

## If either operand is floating-point, the result

is floating-point
cout << 13 / 5.0;
cout << 2.0 / 4;
Chapter 2 Starting Out with

// displays 2.6
// displays 0.5
2006 Pearson Education.

% Operator
C++ modulus operator (%) computes the
remainder resulting from integer division
cout << 9 % 2;

// displays 1

## % requires integers for both operands

cout << 9 % 2.0; // error

## 2006 Pearson Education.

Are used to document parts of a program
Are 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

Chapter 2 Starting Out with

## C++ Style 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;
Chapter 2 Starting Out with

## 2006 Pearson Education.

Begin with /* and end with */
Can span multiple lines
/*---------------------------Multi-line C-style comment
----------------------------*/

## Can be used like C++ style comments

int area;

/* Calculated area */

## 2.17 Programming Style

Refers to the way source code is
visually arranged
Features of Good Programming Styles
Consistency (e.g. two space indented for
the if statement)
Adhering to common practices (e.g. indent
all the lines inside a pair of braces)
Chapter 2 Starting Out with

## Example: Poor Style

#include <iostream>
Using namespace std;int main(){int x=1.011;double y=12.343;cout
<<X = <<x<<Y = <<y<<.\n;return 0;}

## What is this program all about

????
Chapter 2 Starting Out with

## Example: Good Style

#include <iostream>
Using namespace std;
int main()
{
int x=1.011;
double y=12.343;
cout << X = << x << Y = << y << .\n;
return 0;
}