230 likes | 251 Views
Master C++ in 45 minutes. An incomplete introduction To C++. Xun Chen 2019/7/21 The 2 nd Lattice QCD Train Camp. A simple C++ program. Return type. Function name. Argument list. A C++ program should contain one or more functions, and a function with the name “ main ” is required.
E N D
Master C++ in 45 minutes An incomplete introduction To C++ Xun Chen 2019/7/21 The 2nd Lattice QCD Train Camp
A simple C++ program Return type Function name Argument list • A C++ program should contain one or more functions, and a function with the name “main” is required. • The return type for main function must be int. • The argument list could be empty. • The body is enclosed by braces -- { }. • The return statement terminate the function and return value to the caller. int main() { return 0; } simple.cc Function body The 2nd Lattice QCD Train Camp
Compile and run the program • Compile it with gcc • Execute the compiled program • Nothing happens! That’s what the program supposed to do. • A C++ program is a sequence of text files (typically header and source files) that contain declarations. They undergo translation to become an executable program, which is executed when the OS calls its main function. g++ -o simple simple.cc ./simple The 2nd Lattice QCD Train Camp
Coding Styles • C++ is nearly free form. You can write the simple program before like • But it is better to do it following some rules • To improve the readability of the code • Important if you want to do thing right. • Rules are applied to • Comment style • Name conventions: file, class, variable • Formatting: line length, space vs. Tabs • ‘clang-format’ is your good friend. The 2nd Lattice QCD Train Camp
Hello World! • Let’s add some output here header #include <iostream> int main () { std::cout << “Hello World!” << std::endl; } The header provides names of std::cout and std::endl The 2nd Lattice QCD Train Camp
Types and variables • Every name and every expression has a type associated with it. • A type defines a set of possible values and a set of operations (for an object) • An object is some memory hold a value of some type. • A value is a set of bits interpreted according to the type. • A variable is a named object. bool C++ built-in types char int double The 2nd Lattice QCD Train Camp
Variable declaration and definition • A variable is a named object. • Name identifier: • Composed of letters, numeric digits and underscore • Start with letters or underscore. • C++ keywords cannot be used • A variable should be declared before being used. Declarations introduce (or re-introduce) names into the C++ program. • A Definition is a type of declarations that create the associated entity of the given name. The 2nd Lattice QCD Train Camp
Scope of a name • Each name that appears in a C++ program is only valid in some possibly discontinuous portion of the source code called its scope. • Block scope: start at declaration, end at block end. • Names cannot be visited outside of the scope. The 2nd Lattice QCD Train Camp
Literals • Literals are used to represent constant values embedded in the source code. • integer literals : 0, 23, 49, ... • character literals : ‘A’, ‘z’, ‘,’ ... • floating-point literals: 99.3, 1e-3 • string literals: “Hello World”, “Too young, too simple”, ... • boolean literals: true, false The 2nd Lattice QCD Train Camp
Reference type • Reference is an alias of an already-existing object or function. Defined with type name + “&” • A reference is not an object! • We can use reference as using the object itself. a b 100 1000 • Reference can’t be initialized with a literal. Note: We only discuss lvalue reference. The 2nd Lattice QCD Train Camp
Pointers • Just as its name, a pointer points to another type, which could be another object, function, or a member of an object. • A pointer is declared with a type name and a “*”. • A pointer stores the address. “&” (address-of operator) can be used to get the address of a variable. (It’s not a reference!) a is a pointer to an int. d is a pointer to a double. The 2nd Lattice QCD Train Camp
Understand pointer • A pointer has its own address. y 0x7ffc55eb09a8 0x7ffc55eb09b4 0x7ffc55eb09a0 ... x 320 0x7ffc55eb09a8 ... z 3.144444 0x7ffc55eb09b4 The 2nd Lattice QCD Train Camp
Expressions • Composed of one or more operands, and possible operators • Yields a result when evaluated • Could be lvalue or rvalue • lvalue: has identity, cannot be moved from • prvalue: has no identity, can be moved from int a; int b = a; a = 2 + 5 * 8; lvalue rvalue The 2nd Lattice QCD Train Camp
Operators • Evaluating a compound expression involves grouping the operands to the operators. • The value of an expression depends on how the subexpressions are grouped. • Operands of operators with higher precedence group more tightly than operands of operators at lower precedence. • C++ provides rich operators: arithmetic, logical, assignment, increment/decrement, member access, conditional, sizeof, comma... int a; int b = a; a = 2 + 5 * 8; The * has higher precedence than + The 2nd Lattice QCD Train Camp
Statements • Fragments executed in sequence. • The most common statements are expression statements – expressions with semicolon ;. • Compoundstatements – blocks with brace pairs {}. The 2nd Lattice QCD Train Camp
Flow Control • Selection Statements • choose between one or several flows of control • Iteration Statements • Repeatedly execute some code. • Four forms • Jump Statements • Interrupt the flow of execution • if (condition) statement • if (condition) statement else statement • switch (condition) statement • while ( condition ) statement • do statement while ( expression ) • for ( expression; expression ; expression) statement • for ( range_declaration: range_expression ) statement • break; • continue; • goto; • return; The 2nd Lattice QCD Train Camp
Functions • A function definition typically consists of a return type, a name, a list of zero or more parameters, and a body. • The parameters are specified in a comma-separated list enclosed in parentheses. • The actions that the function performs are specified in a statement block, referred to as the function body. • A function is executed through the call operator () The 2nd Lattice QCD Train Camp
Function example • A function to calculate the factorial of an integer • Call the function, provide an int type, obtain an int value. Return type Function name Argument list Function body The 2nd Lattice QCD Train Camp
Argument passing • When we call a function, the parameters are initialized with the arguments passed in. • The type of the parameters determines how they are initialized. • If a parameter is a reference, the argument is “passed by reference”, or, the parameter is bound to the argument (an alias of the argument). • Otherwise, the argument’s value is copied. • When the value is copied, the parameter and argument are independent objects. Pass by reference Pass by value The 2nd Lattice QCD Train Camp
Overload of functions • Functions that have the same name but different parameter lists and that appear in the same scope are overloaded. declaration function call definition The 2nd Lattice QCD Train Camp
The std::vector class • A vector is a collection of objects, all of which have the same type. • Every object in the collection has an associated index. • A vector is a type of ‘container’, because it contains other objects. • A vector is a class template. The compiler need a process called instantiation to create the corresponding class from template. • We need to provide information of the object type for instantiation. It is recommend to use std::vector when you need to use array in modern C++. The 2nd Lattice QCD Train Camp
Classes • Define you own types with class. • Values • Operations • Two different class defines two different types. Even they have same definition of members. • When a class is defined, we can use it to define variable as built-in types. • Every class defines its own new scope. • Class constructor controls how an object of the class is initialized. The 2nd Lattice QCD Train Camp
Summary • C++ is too complex so I only can give an incomplete introduction here. • Knowledge of following concepts is helpful • Name • Type • Variable • Expression • Statements • Important topics not covered in this slides: • Type conversion • Construction of objects • Function overloading • New features since the C++11 standard. The 2nd Lattice QCD Train Camp