[WIP]C++ 정리 01
Hello, World!
// This program outputs the message “Hello, World!” to the monitor
#include "std_lib_facilities.h"
int main(void) // C++ programs start by executing the function main
{
cout << "Hello, World!\n"; // output “Hello, World!”
return 0;
}
Objects, Types, and Values
- An object is a region of memory with a type that specifies what kind of information can be placed in it.
- A named object is called a variable.
- For example, character strings are put into string variables and integers are put into int variables.
- The representation of a string is a bit more complicated than that of an int because a string keeps track of the number of characters it holds.
Type safety
- using a variable before it has been initialized is not considered type-safe
- Complete type safety is the ideal and therefore the general rule for the language.
- The ideal of type safety is incredibly important when writing code.
Unsafe conversions
- On a PC the range of char values is [–128:127], but only [0:127] can be used portably because not every computer is a PC, and different computers have different ranges for their char values, such as [0:255].
- C++11 introduced an initialization notation that outlaws narrowing conversions. For example, we could (and should) rewrite the troublesome examples above using a {}-list notation, rather than the = notation:
double x{2.7}; // OK
int y{x}; // error: double -> int might narrow
int a{1000}; // OK
char b{a}; // error: int -> char might narrow
char b1 {1000}; // error: narrowing (assuming 8-bit chars)
char b2 {48}; // OK
Computation
Objective and tools
- Our job as programmers is to express computations:
- Correctly
- Simply
- Efficiently
- Please note the order of those ideals: it doesn’t matter how fast a program is if it gives the wrong results.
Expressions
- The most basic building block of programs is an expression.
- An expression computes a value from a number of operands.
- The simplest expression is simply a literal value, such as 10, ‘a’, 3.14, or “Norah”.
- Overuse of parentheses, as in
(a*b)+(c/d)
, decreases readability. - Don’t write absurdly complicated expressions such as
a*b+c/d*(e–f/g)/h+7
and always try to choose meaningful names. - const vs. constexpr
- A constexpr symbolic constant must be given a value that is known at compile time. For example:
constexpr int max = 100; void use(int n) { // OK: c1 is 107 constexpr int c1 = max + 7; // error: we don’t know the value of c2 constexpr int c2 = n + 7; // … }
- To handle cases where the value of a “variable” that is initialized with a value that is not known at compile time but never changes after initialization, C++ offers a second form of constant (a const):
constexpr int max = 100; void use(int n) { // OK: c1 is 107 constexpr int c1 = max + 7; // OK, but don’t try to change the value of c2 const int c2 = n + 7; // … c2 = 7; // error: c2 is a const }
- Such “const variables” are very common for two reasons:
- C++98 did not have constexpr, so people used const.
- “Variables” that are not constant expressions (their value is not known at compile time) but do not change values after initialization are in themselves widely useful.
Conversions
The notations type(value)
and type{value}
mean “convert value to type as if you were initializing a variable of type type
with the value value
.” In other words, if necessary, the compiler converts (“promotes”) int
operands to double
s or char
operands to int
s. The type{value}
notation prevents narrowing (§3.9.2), but the type(value)
notation does not.
- easy to forget about integer division in an expression
double dc;
cin >> dc;
// beware!
double df = 9/5*dc+32;
the value of 9/5 is 1 rather than the 1.8 we might have hoped for. To get the code mathematically correct, either 9 or 5 (or both) will have to be changed into a double
.
double dc;
cin >> dc;
// better
double df = 9.0/5*dc+32;
Statements
- References:
- Bjarne Stroustrup. (2014). Programming : Principles and Practice Using C++ (Second Edition). Addison-Wesley.
Leave a comment