C++ Basics: Lecture 2 - Structure of a C++ Program, Libraries, Data Types, and Variables, Summaries of Compilers

C++ Basics. 2.1. Variables and Assignments 40. Variables 40. Names: Identifiers 41. Variable Declarations 44. Assignment Statements 46.

Typology: Summaries

2022/2023

Uploaded on 02/28/2023

dewan
dewan ๐Ÿ‡บ๐Ÿ‡ธ

4.6

(17)

253 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C++ Basics
Lecture 2
COP 3014 Spring 2022
January 12, 2022
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download C++ Basics: Lecture 2 - Structure of a C++ Program, Libraries, Data Types, and Variables and more Summaries Compilers in PDF only on Docsity!

C++ Basics

Lecture 2 COP 3014 Spring 2022

January 12, 2022

Structure of a C++ Program

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).

Libraries

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)

Building and Running a C++ Program

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 tells the preprocessor to copy the standard I/O stream library header file into the program I (^) Compiling I (^) Syntax checking, translation of source code into object code (i.e. machine language). Not yet an executable program. I (^) Linking I (^) Puts together any object code files that make up a program, as well as attaching pre-compiled library implementation code (like the standard I/O library implementation, in this example) I (^) End result is a final target โ€“ like an executable program I (^) Run it!

Comments

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

Data Types

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

Style Conventions for Identifiers

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

Declaring Variables

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;

Initializing Variables

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;

Initializing Variables

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);

Constants

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)

Symbolic Constants (an alternative)

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

More Literals

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

Escape Sequences

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