






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
C++ Basics. 2.1. Variables and Assignments 40. Variables 40. Names: Identifiers 41. Variable Declarations 44. Assignment Statements 46.
Typology: Summaries
1 / 30
This page cannot be seen from the preview
Don't miss anything!























Lecture 2 COP 3014 Spring 2022
January 12, 2022
I (^) Sequence of statements, typically grouped into functions. I (^) function: a subprogram. a section of a program performing a specific task. I (^) Every function body is defined inside a block. I (^) For a C++ executable, exactly one function called main() I (^) Can consist of multiple files and typically use libraries. I (^) Statement: smallest complete executable unit of a program. I (^) Declaration statement I (^) Execution statement I (^) Compound statement โ any set of statements enclosed in set braces { } (often called a block) I (^) Simple C++ statments end with a semi-colon. (A block does not typically need a semi-colon after it, except in special circumstances).
I (^) Usually pre-compiled code available to the programmer to perform common tasks I (^) Compilers come with many libraries. Some are standard for all compilers, and some may be system specific. I (^) Two parts I (^) Interface: header file, which contains names and declarations of items available for use I (^) Implementation: pre-compiled definitions, or implementation code. In a separate file, location known to compiler I (^) Use the #include directive to make a library part of a program (satisfies declare-before-use rule)
I (^) Starts with source code, like the first sample program I (^) Pre-processing I (^) The #include directive is an example of a pre-processor directive (anything starting with #). I (^) #include
I (^) Comments are for documenting programs. They are ignored by the compiler. I (^) Block style (like C) /* This is a comment. It can span multiple lines */ I (^) Line comments โ use the double-slash // int x; // This is a comment x = 3; // This is a comment
Atomic data types are the built-in types defined by the C++ language. I (^) bool: has two possible values, true or false I (^) integer types I (^) char - 1 byte on most systems. I (^) Typically used for representing characters I (^) Stored with an integer code underneath (ASCII on most computers today) I (^) short - (usually at least 2 bytes) I (^) int - (4 bytes on most systems) I (^) long - (usually 4 or more bytes) I (^) The integer types have regular and unsigned versions I (^) floating point types - for storage of decimal numbers (i.e. a fractional part after the decimal) I (^) float I (^) double I (^) long double
I (^) Donโt re-use common identifiers from standard libraries (like cout, cin) I (^) Start names with a letter, not an underscore. System identifiers and symbols in preprocessor directives often start with the underscore. I (^) Pick meaningful identifiers โ self-documenting
numStudents, firstName // good a, ns, fn // bad I (^) a couple common conventions for multiple word identifiers I (^) numberOfMathStudents I (^) number of math students
I (^) Declare Before Use: Variables must be declared before they can be used in any other statements I (^) Declaration format: typeName variableName1, variableName2, ...;
int numStudents; // variable of type integer double weight; // variable of type double char letter; // variable of type character
//Examples of multiple variables of the same type //in single declaration statements
int test1, test2, finalExam; double average, gpa;
I (^) Variables of built-in types can be declared and initialized on the same line, as well
int numStudents = 10; double weight = 160.35; char letter = โAโ;
int test1 = 96, test2 = 83, finalExam = 91; double x = 1.2, y = 2.4, z = 12.9;
An alternate form of initializing and declaring at once:
// these are equivalent to the ones above int numStudents(10); double weight(160.35); char letter(โAโ);
int test1(96), test2(83), finalExam(91); double x(1.2), y(2.4), z(12.9);
I (^) A variable can be declared to be constant. This means it cannot change once itโs declared and initialized I (^) Use the keyword const I (^) MUST declare and initialize on the same line const int SIZE = 10; const double PI = 3.1415;
// this one is illegal, because itโs not // initialized on the same line const int LIMIT; // BAD!!! LIMIT = 20; I (^) A common convention is to name constants with all-caps (not required)
I (^) A symbolic constant is created with a preprocessor directive, #define. (This directive is also used to create macros). I (^) Examples: #define PI 3. #define DOLLAR โ$โ #define MAXSTUDENTS 100 I (^) The preprocessor replaces all occurrences of the symbol in code with the value following it. (like find/replace in MS Word). I (^) This happens before the actual compilation stage begins
I (^) floating point literal โ an actual decimal number written in code (4.5, -12.9, 5.0) I (^) These are interpreted as type double by standard C++ compilers I (^) Can also be written in exponential (scientific) notation: (3.12e5, 1.23e-10) I (^) character literal โ a character in single quotes: (โFโ, โaโ, โ\nโ) I (^) string literal โ a string in double quotes: (โHelloโ, โByeโ, โWow!\nโ) I (^) boolean literals - true or false
I (^) String and character literals can contain special escape sequences I (^) They represent single characters that cannot be represented with a single character from the keyboard in your code I (^) The backslash \is the indicator of an escape sequence. The backslash and the next character are together considered ONE item (one char) I (^) Some common escape sequences are listed in the table below
Escape Sequence Meaning \n newline \t tab \โ double quote \โ single quote \ backslash