CIS22A - Notes for Week 2, 9/30 - 10/3

Announcements and Reminders


Topics

Terminology

C++ Program Basics


What does  int main() mean?
Is  return 0 required?

Program 2-1 from the textbook

cout
What is cout?

What is the << operator?

    aka the insertion operator, the left-shift operator

What is endl?  endline, aka a manipulator

What is a newline?  aka \n

Escape sequences (see Table 2-2)
    \n   \t   \a   \r   \\   \"  



Literals, variables and types
Literals
Types
int
double
C-string, null terminated char array
char

Variables

Identifiers

Used for variables, constants, functions, user-defined types

What are the rules for creating an identifier?
  • first character must be a letter or underscore
  • other characters must be alphanumeric or underscore
  • identifiers are case sensitive

Types

Primitive types or Built-In types

int
double
char
float
long
short
long long
long double
unsigned, unsigned int
unsigned long
unsigned short
unsigned char

C-string, null terminated char array

The string class

#include <string>

A string variable or object

How do you know the difference between a variable and an object?


Input

cin

is used to enter data into a variable.  More precisely, cin gets data from the input buffer and copies it into a memory location (a variable).  cin uses the right-shift operator ( >> ).  The right-shift operator is also called the extraction operator.  Why?

int x;

cin >> x;

string y;

cin >> y;

cin >> x >> y;

What header file is needed to use cin?

What is cin (type) ?

Variables, int type, assignment operator

Program 2-7 from the textbook

Integer Types

Type Size in bytes Range of Values
char 1 -128 to 127
unsigned char 1 0 to 255
short 2 -32,768 to 32,767
unsigned short 2 0 to 65,535
int 4 -1,247,483,648 to 2,147,483,647
unsigned int 4 0 to 4,294,967,295
long 4 –2,147,483,648 to 2,147,483,647
unsigned long 4 0 to 4,294,967,295
long long 8 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long long 8 0 to 18,446,744,073,709,551,615

How do you choose the right type?

  • Be conservative
  • Allow for growth
  • Match existing types and function types

Program 2-10 - page 46

char type

char C1 = 'A';
char C2 = "A";  // ERROR

What's the output from this program? 

#include <iostream>
using namespace std;

int main()
{
    int number = 77;
    char ch = 'A';

    cout << ch << ' ' << number << endl;

    ch = number;
    cout << ch << ' ' << number << endl;

    number = 'x';
    cout << ch << ' ' << number << endl;

    return 0;
}

What does convert mean?  What is a conversion?

The string class

  • string is a type
  • Requires #include <string>
  • a literal "string" constant
cout << "Have a nice day";

use of a string object (aka a string variable)

string text;
text = "Have a nice day";
cout << text;

Floating point types

Type Size in bytes Range of Values Minimum Value Maximum Value
float 4 +/- E37 or +/- E-37 FLT_MIN FLT_MAX
double 8 +/- E308 or +/- E-308  DBL_MIN DBL_MAX
long double    8,12, or 16 +/- E4932 or +/- E-4932   LDBL_MIN LDBL_MAX

Minimum and maximum floating point constant values are defined in <cfloat>.

Floating point literals.

What is the type of a floating point literal?

Program 2-16 - Page 56

The bool type

The bool type is a built-in, or primitive type.

Variables

  • assignment and initialization
  • sizeof
  • scope

Arithmetic operators

  • + - * / %
  • binary operator
  • unary operator

operator precedence - Page 1289

integer division

static_cast

Program 2-21 - Page 65

Comments

Program 2-26 - Page 72
Program 2-27 - Page 72

Constants

literal
named
defined

Programming Style

Program 2-29 - Page 75

Program 2-30 - Page 76

How would you modify this program to read the values of shares and avgPrice from the keyboard?


Videos

Understanding a Simple C++ Program

More on Printing Text

Variables


Read Me
comments

preprocessor directive
#include

braces

cout

class
object
ostream

operator
left-shift operator
insertion operator

endl
manipulator

newline

literal
variable
type

primitive type
built-in type


int
double
char
float
long
short
long long
long double
unsigned, unsigned int
unsigned long
unsigned short
unsigned char
signed char
unsigned long long

C-string

string

class
object
header file
<string>

floating point type
float
double
long double

bool type

identifier
underscore
case sensitive

cin
istream

Operators
sizeof 
assignment 
comma
arithmetic + - * / %
mod
binary
unary
operator precedence
static_cast

comments
c++ style comment
c style comment

literal constant
named constant
defined constant

const

#define