C+= Flashcards
What does a C++ compiler do?
It sequentially goes through each source code (.cpp) file and does 1. check if code follows rules of C++ and 2. translates C++ source code into a machine language file called an object file (.o / .obj)
What does the linker do?
- Takes all object files generated by compiler and combine them into a single executable program (.exe)
- Capable of linking library files
- All cross-file dependencies are resolved properly
What is a statement? Why are they the most common type of instruction?
A statement is a type of instruction that causes the program to perform some action.
They are the smallest independent unit of computation in the c++ lang.
What are Functions? What function must every program have?
Is a reusable sequence of statements designed to do a particular job.
The main function.
What is syntax error?
Is a compiler error that occurs at compile-time when your program violates grammar rules
What is the C++ Standard library?
A library file is collection of precompiled code that has been packaged up for reuse in other programs
What are programs?
Collections of instructions that manipulate data to produce a desired result
What is data and value
Data is any information that can be moved, processed, or stored by a computer. Value is a single piece of data stored in memeory.
What is a variable and identifier?
Variables is a named object while identifier is the name of the object.
What is an object?
Region of storage (usually memory) that has a value and other associated properties
How to create a variable
Use a special kind of declaration statement called a definition.
What is the use of RAM (random access memory)?
Its basically like a bunch of mailboxes that can be used to hold pieces of data while program is running
What is a data type? When must the type of a variable be known?
Tells program how to interpret a value in memory or what type of value the variable will store.
Must be known at compile-time
What happens at runtime when program runs?
The variable will be instantiated. Instantiation mean sobject will be created and assigned a memory address.
What is assignment?
Using the = assignment operator to give a variable a value after it has been defined
What is initialization?
When you provide an initial value for a variable at the same time it is defined
What is default initialization?
When no initialization value is provided
What is copy initialization
When value is provided after an equals sign
What is direct initialization
When value provided inside a parenthesis
int width(5);
What is brace initialization
int width {6};
int height = {6};
int depth {};
Why is brace initialization the best practice
Because it has a benefit to dishallowing narrowing conversions, meaning if you try to use brace to initialize a variable with a value it can not safely hold, the compiler will throw a warning or an error while the other init will truncate or round the number
What is value and zero initialization?
When a variable is initialized with empty braces, value init takes place and will init the variable to zero
What is best practice when initializing your variables?
Initialize upon creation
What is the input/output library?
Part of C++ standard library that deals with basic input/output (keyboard and console)
How to include the IO library?
include
What is the std::cout variables?
what does “cout” stand for
It allows you to send data to the console to be printed as text.
character output
How do you send to the console with the std::count variable?
you use the insertion operator (
What is the std::endl?
THis prints a newline character to console
Why do people use \n over std:endl?
The std::endl moves the cursor to the next line, and it flushes the output (makes sure that it shows up on screen immediately). THe std::cout already flushes already so it is not important. \n only moves cursor
How to use ‘\n’
You have to use single quotes when used by itself. Otherwise use it naturally
What is std::cin
It reads input from keyboard using the extraction operator (»). Input must be stored in a variable
What is uninitialized variables?
C/C++ does not initialize most variables to a given value (zero) auto. When a variable is assigned a memory location by the compiler, the default value of that variable is whatever value happens to already be in that memory
What is undefined behavior?
When using the value from an uninitialized variable. When you execute code whose behavior is not well defined
What is whitespace?
Refers to characters that are used for formatting purposes (spaces, tabs, newlines)
What are literals?
A literal is a fixed value that has been inserted directly into the source code
How is literal different from value(type)?
Value of an interval is fixed and can’t be damage (constant)
What is an operation
Mathematical calculation involving zero/more input values (operands) that produces an new value
What are expressions
Combination of literals, variables, operators, and function calls that can be executed to produce a singular value.
What is evaluation and result in expressions?
Process of executing an expression is evaluation and the single value produced the result.
What is a user defined function
return-type identifier () { // your code }
Is nest functions supported in C++
No
What is return by value in functions?
Functions will use a return statement to indicated specific values to be returned to the caller. It sends the value.
What are void return values
They tell the compiler that the function does not return a value
What is the return value from
main function
It is called a status or exit code which terminates the program
What are function parameters
They are variables used in functions. Must be initialized with a value provided by the caller of the function
What are local variables?
Function parameters, as well as variables defined inside a function body
What is an identifier’s scope?
Determines where the identifier can be accessed within the source code.
What is a forward declaration
It allows us to tell the compiler about the existence of an identifier before actually defining the identifier
What is a function prototype
Consists of a return type, name, and parameter, but no function body
int add(int x, int y);
What is a definition?
A definition actually implements or instantiates the identifier.
What is a declaration?
A statement that tells the compiler about the existence of an identifier and its type
A declaration is all a compiler needs to satisfy the compiler
What is a namespace?
Region that allows you to declare names inside of it for the purpose of disambiguation. It provides a scope region to the names declared inside of it
What is the global namespace
Any name not defined inside a class, function, or a namespace
What is the scope resolution operator?
:: symbol
Left of symbol identifies the namespace that the name to the right is contained within
What a using directive
Tells compiler to check a specified namespace when trying to resolve an identifier that has no namespace prefix
using namespace std;
What is the preprcoessor
As a separate program that manipulates the text in each code file. It scans through the code file top to boottom, looking for preprocessor directives
What are preprocessor directives
Instructions that start with a #symbol and end with a newline not a semicolon
What do preprocessor directives do
Tell the preprocessor to perform specific particular text manipulation tasks. It does not understand C++ syntax. The output goes through several more translation phases and then is compiled
What does includes do?
The #include directive makes the preprocessor replace the #include directive with the contents of the included file and then compiled
What is a macro?
Is a rule that defines how input text is converted into replacement output text.
define MY_NAME “ALEX”
What does this output
int main() { std::cout << "My name is: " << MY_NAME; return 0 }
My name is: Alex
What is conditional compilation
Allow you to specify under what conditions something will or won’t compile
#ifdef #ifndef #endif
What does #ifdef preprocessor directive do?
It allows the preprocessor to check whether an identifier has been previously #defined and if it was then it will compile whatever is contained inside #ifdef #endif
What does #indef do
it is opposite of #ifdef and allow syou check if it was not defined
if 0
What does
do
Exclude this block of code from being compiled
What are header files?
.h extension on files
Allows you to put declarations in one location and then import them wherever we need them
Angled brackets vs double quotes
Use double quotes to include header files included in current directory and angled brackets to include headers that come with your compiler
What are additional header files that are implicitly included meaning when your code file includes the first header and then that header includes other header files?
transitive includes
LIST OF HEADER FILE BEST PRACTICES
always include header guards
do not define variables/functions in header files
give header file the same name as the source file its associated with
each header file should have a specific job
be mindful of which headers you need explicitly include for the functionality tha tyou are using
every header you write should compile
How to create header guards
endif
#ifndef SOMENAME #define SOMENAME
Whats a syntax error?
When you write a statement that is not valid according to the grammar of the c++ language
Whats a semantic error
Statment is syntactically valid, but does not do what the programmer intended
List the 5 steps to debugging a problem
- Find the root cause of the problem (line that breaks)
- Ensure you understand why issue is occuring
- Determine you to fix the issue
- Repair the issue
- retest to ensure problem is fixed and no new problems have emerged
Whats the first a most important step to finding the rpoblem
Be able to reproduce teh problem 100% so that we can run the program over and over to look for clues and determine the issue
What is a binary digit (bit)
Smallest unit of memory
1 or 0
What is memory addresses
Memory organized into sequential units
What is a byte? how many bits?
Group of bits that are operated as a unit. 8 sequential bits.
What is a data type
Its used to tell the compiler how to interpret the contents of memory
An object with n bits can hold….
2^n
Minimum size of data types
boolean - 1 byte char - 1 byte short - 2 byte int - 2 byte long - 4 byte float - 4 bytes double - 8 bytes
How to determine the size of a data type
sizeof(data_type)
What is an integer
An integral type that can represent positive and negative whole numbers
What is signed integers
Number’s sign is stored as part of the number (uses a single bit called the sign bit)
Whats the range of 8 bit signed?
-128 to 127
Whats range of 16 bit signed?
-32768 to 32767
What is integer overflow
When we try to store a value that is outside the range of the type
A n-bit unsigned variable has a range of
0 to (2^n) - 1
What behavior do unsigned numbers have with overflow?
It wraps around
Why isn’t the size of the integer variables fixed?
So that compiler implementers could pick a size for int that performs best on the target computer architecture
Two downsides of fixed-width itntegers
Are not guaranteed to be defined on all architectures. also may be slower than a wider type on some archiectures
What are fast and least integers
Fast types provide the fastest signed/unsigned integer type with a width atleast # bits
Least types provide the smallest signed/unsigned integer type with a width at least #bits
What is std::size_t
An unsigned integral type, that typically is used to represent the size or length of objects
What are the digits in the part before the e in scientific notation?
Significant digits
What is the number of sigdigts do
defines a numbers precision
Whats a floating point type variable
A variable that can hold a real number. It means decimal point can “float” and support a variable number of digits before and after the decimal point
Will std:cout print fractional part of a number
no
Whats the default precision of std::cout
6
How to override the default precision
#include //for output manipulator function std::setprecision(#)
What is rounding error
When precision is lost because a number can’t be stored precisely
Two special categories of float
Inf - infinity
NaN - Not a number or IND
What do true and false evaluate to
1 and 0
how to get std::cout to print true or false instead of 1 and 0
std::boolalpha
What does cin accept inputs for boolean variables
0 and 1
What is a if statement?
Allows us to execute one or more lines of code only if some condition is true
what is a condition
an expression that evaluates to a boolean value
What is the char data type?
Designed to hold a character
How are char variables stored as
As an integer - ASCII character
What is ASCII code
Stands for american standard character for information interchange.
Way to represent english characters as numbers 0 to 127
\n
start new line
\a
alert
\b
backspace
\t
horizontal tab
\v
vertical tab
'
single quote
"
double quote
Rule between single and double quotes
Put single chars in single quotes. Text put between double quotes
What is implicit type conversion
When compiler does type conversion for you automatically
Rule for implicit type conversions
int to double is safe
double to int is not safe
Perform a explicit type conversion
static_cast (expression)
takes value from expression as input and returns a value converted into the type new_type
how to read a full line of input into a string
std::getline(std::cin»_space; std::ws, age)
What are literal constants
Unnamed values inserted directly into the code
What happens in floating point division
If either(or both) value is float then fraction is kept
What happens in integer division
It will divide by drop the fractional component
What happens when you do modulus with a negative number
The remainder will carry the sign