1/35
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
//
Beginning of a comment
#
Beginning of preprocessor directive
<>
Enclose filename in #include
()
Used when naming a function
{}
Encloses a group of statements
“”
Encloses string of characters
;
End of programming statement
The cout Object
Displays output on the computer screen
You use the stream insertion operator («) to send output to cout
Ex. cout «“Programming is fun!”;
Can be used to send more than one item to cout
Ex. cout « “Hello” « “there!”; OR
cout « “Hello”;
cout « “there!”; which still produces one line of output
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!”;
DO NOT put quotation marks around endl
The \n Escape Sequence
You can also use the \n escape sequence to state a new line of output. This will produce two lines of output:
cout « “Programming is\n”;
cout « “fun!’’;
*\n is inside the string!
The #include Directive
Inserts the contents of another file into the program
A preprocessor directive, not part of C++ language
#include lines not seen by compiler
DO NOT PLACE A SEMICOLON AT THE END OF #include line
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;
Literals
a value that is written into a program’s code
String literal (“hello, there”)
12 (integer literal)
Identifiers
A programmer-defined name for some part of a program (variables, functions, etc.)
YOU CANNOT USE ANY OF THE C++ KEY WORDS AS AN IDENTIFIER
Rules:
The first character of an identifier must be an alphabetic character or an underscore
After the first character you may use alphabetic characters, numbers, or underscore
Upper- and lowercase characters are distinct
Defining Variables
Variables of the same time can be defined on separate lines (;;;) or the same line (,,;,;)
Variables of different types must be in different definitions
Integer Literals
An integer value that is typed into a program’s code (itemsOrdered = 15)
Stored in memory as int by default
To store in a long memory location, put ‘L’ at the end of the number: 1234L
To store in a long long memory location, put ‘LL’ at the end of the number: 324LL
Constants that begin with ‘0’ are base 8: 075
Constants that begin with ‘0x’ are base 16: 0×75A
True or false: A variable name has no relation to the purpose of the variable
False, a variable name should represent the purpose of the variable
The char Data Type
Used to hold characters of 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 (‘A’)
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)
The C++ string Class
Special data type supports working 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;)
Floating-Point Data Types
Float, double, long double
Can hold real numbers such as: 12.45, -3.8
Stored in a form similar to scientific notation
All floating-point numbers are signed
Floating Point Literals
Can be represented in Fixed point (decimal) notation (31.4159) or E notation (3.14159E1)
Are double by default
Can be forced to be float (3.14159f) or long double (.0000625L)
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; returns allDone 1)
Determining the Size of a Data Type
the sizeof operator gives the size of any data type or variable
Variable assignments and initialization
An assignment statement uses the = operator to store a value in a variable (item= 12; assigns the value 12 to the item variable)
Assignment= the variable receiving the value must appear on the left side of the = operator.
True or false: this will work:
12 = item;
False, the variable receiving the value must appear on the left side of the = operator
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;)
Declaring Variables with the auto Key Word
C++ 11 introduces an alternative way to define variables, using the auto key word and an initialization value (auto amount = 100; is an int)
The auto key word tells the compiler to determine the variable’s data type from the initialization value.
Scope
The scope of a variable is the part of the program in which the variable can be accessed
A variable cannot be used before it is defined
Arithmetic Operators
Used for performing numeric calculations
C++ has unary, binary, and ternary opterators:
Unary (1 operand): -5
Binary (2 operands): 13 -17
Ternary (3 operands): exp1 ? Exp2 : exp3
/ (division) operator performs integer division if both operands are integers
If either operand is a floating point, the result is a floating point
% (modulus) operator computes the remained resulting from integer division
requires integers for both operands (NO FLOATING POINTS)
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
Single-Line Comments
Begin with // through to the end of the line (Int length = 12; //length in inches OR // calculate rectangle area)
Multi-Line Comments
Begin with /*, end with */
Can span multiple lines
Can begin and end on the same line
Named Constants
Aka constant variable
A variable whose content cannot be changed during program execution
Used for representing constant balues with descriptive names
Often named in uppercase letters
“ const double TAX_RATE = 0.0675;
Programming Style
The visual organization of the source code
Includes the uses of spaces, tabs, and blank lines
Does not affect the syntax of the program
Affects the readability of the source code