Programming is learned by writing programs.

Size: px
Start display at page:

Download "Programming is learned by writing programs."

Transcription

1 Financial Computing with C++, Lecture 1 - p3/24 Financial Computing with C++, Lecture 1 - p4/24 Outline Financial computing with C++ Background LG Gyurkó Getting started - Basic syntax University of Oxford Compiling the source code Michaelmas Term 2015 Interacting and using types Outline of the course Financial Computing with C++, Lecture 1 - p1/24 Outline of the course Financial Computing with C++, Lecture 1 - p2/24 Programming is learned by writing programs. Brian Kernighan C++ at what level? typical quants spend significant portion of their time with coding core quants implement quantitative libraries, optimise code, they know the hard core stuff non-core quants are users of the quantitative libraries, they must understand what these libraries do, but how they do it is not necessarily interesting What is this course about? Objective: learn what is required for non-core quants, in particular Exam Reading and understanding C++ code, structure, built-in types, control flow Designing and implementing classes, object-base programming Template functions, template classes Standard template library, containers Object oriented principles, inheritance Composing objects, a bit of design patterns 3 hour computer based exam in week 0 of Hilary Term.

2 Financial Computing with C++, Lecture 1 - p7/24 Financial Computing with C++, Lecture 1 - p8/24 Outline of this lecture - understanding basic syntax Brief history of C++ I know nothing! (I am from Barcelona.) Manuel (Fawlty Towers) Bell Labs: UNIX, B, C Bell Labs, Bjarne Stroustrup: extended C to C with classes New features classes and object oriented features exceptions templates namespaces STL C++ aimed to be standardized and portable Extensions: Boost Most recent standards: C++11 about its new features... see later Platforms and environments: Unix, Linux Windows (e.g. MS Visual Studio) Mac (e.g. Xcode) Eclipse, CodeBlocks etc. available on all of these platforms. Recommended books Financial Computing with C++, Lecture 1 - p5/24 More books Financial Computing with C++, Lecture 1 - p6/24 Accelerated C++ by Andrew Koenig, Barbara E. Moo, Addison Wesley 2000 Programming: Principles and Practice Using C++ by Bjarne Stroustrup, Addison Wesley 2008 The C++ Standard Library: A Tutorial and Reference by Nicolai M. Josuttis, Addison Wesley 1999 The C++ Programming Language by Bjarne Stroustrup, Addison Wesley 2000 ( ) Effective C++: 55 Specific Ways to Improve Your Programs and Designs by Scott Meyers, Addison Wesley 2005 More Effective C++: 35 New Ways to Improve Your Programs and Designs by Scott Meyers, Addison Wesley 1996 Effective STL: 50 Specific Ways to Improve the Use of the Standard Template Library by Scott Meyers, Addison Wesley, 2001 Beyond the C++ Standard Library: An Introduction to Boost by Bjorn Karlsson, Addison Wesley 2005 C++ Design Patterns and Derivatives Pricing by Mark Joshi, CUP 2008 Design patterns : elements of reusable object-oriented software by Eric Gamma et al. (aka.: Gang of four ), Addison Wesley 1995 For interviews you ll need to learn about: data structures and basic algorithms. Algorithms Introduction to Algorithms by T Cormen, C Leiserson, R Rivest, C Stein, MIT Press; 3rd edition, 2009 The Algorithm Design Manual by Steven S Skiena, Springer; 2nd ed 2009 Job interview preparations: Cracking the Coding Interview: 150 Programming Questions and Solutions by Gayle Laakmann McDowell, 6th edition, CareerCup, 2015 Programming Interviews Exposed: Secrets to Landing Your Next Job by John Mongan, Noah Kindler, Eric Giguere, 3rd edition, John Wiley & Sons, 2012 Exercises:

3 Financial Computing with C++, Lecture 1 - p11/24 Financial Computing with C++, Lecture 1 - p12/24 Useful links C++11 References On the history of C++ Other bs/hopl2.pdf Boost: Fiddler (for various languages including c++): Links to C++11 features n3242.pdf bs/c++0xfaq.html Supporting compilers GCC (not much of the Concurrency features are implemented) Microsoft Visual Studio Other compilers: http: //wiki.apache.org/stdcxx/c%2b%2b0xcompilersupport Until full support: C++11 Financial Computing with C++, Lecture 1 - p9/24 A very simple program Financial Computing with C++, Lecture 1 - p10/24 Some of the new features Container improvements, new algorithms Smart pointers Random number generators and distributions Threads, Locks Lambdas Range-for statement Rvalue references, move semantics Initializer lists auto, decltype - deduction of a type from an initializer static_assert - static (compile-time) assertions long long at least 64bit long integers operator "" - user-defined literals override, final - override controls etc., etc., etc. A piece of simple C++ code (Lecture01/Lecture01.cpp): 1 #include <iostream> 2 3 int main() 4 { 5 //This program plots some text 6 std::cout << "Technically, this is the \"Hello" << 7 " world!\" program." << std::endl; 8 9 return 0; 10 } The output is: Technically, this is the "Hello world!" program.

4 Financial Computing with C++, Lecture 1 - p15/24 Financial Computing with C++, Lecture 1 - p16/24 A very simple program A very simple program 1 #include <iostream> 1 int main() 2 { 3 } The #include directive is to be used when asking for facilities not included in the core language. iostream is part of the standard-library, the basic package for displaying values. we will use several other header files from the standard library we will create many of our own header files (*.h, *.hpp, etc.) Declaration of a function has a strict form: return_type func_name(arg1_type arg1_name,...) The function body goes between curly braces (block). Normally the declaration and the implementation (definition) are separated. The main function can have arguments and can be called from console command line with arguments. There is a version of main taking arguments one can pass arguments to and execute applications written in c++ from command line. A very simple program Financial Computing with C++, Lecture 1 - p13/24 A very simple program Financial Computing with C++, Lecture 1 - p14/24 1 std::cout << "Technically, this is the \"Hello" << 2 " world!\" program." << std::endl; 1 //This program plots some text The part of the line after // is a comment and not part of the code. Alternatively, multiple lines can be commented using the opening /* and closing tag */, e.g: 1 std::cout << std::endl; /* Comment starts here 2 and this is also part of the comment 3 until the closing tag. */ Namespaces are used to separate pieces of code into different domains (or scope). A namespace can spread over several (header) files, and one (header) file may contain several namespaces. Namespaces can be nested. The standard-library is placed in the namespace std. One can access the facilities in std, by using the address std:: (:: is the scope-operator). cout is the command to display values (string, int, double, etc.). << is the operator which inserts values on its right into the stream on its left (<< is left associative). std::endl is the command inserting the end-of-line into the output stream. Statements can be broken into several lines (a line break is just a space), each statement must end with ;. The string inserted into the stream starts and ends with ", however the character after \ is regarded as part of the text.

5 Financial Computing with C++, Lecture 1 - p19/24 Financial Computing with C++, Lecture 1 - p20/24 A very simple program How to get to an executable?... please remember, the compiler is your friend; possibly, the compiler is the best friend you have when you program. Bjarne Stroustrup 1 return 0; If a function does not return value, its return type is void (except constructors, and destructors, see later) If a function has a non-void return type, it should explicitly return a value of that type using the return keyword Any command after the return...; part is ignored by the compiler (unless it s one branch of an if statement and the other branch is implemented after that) The main returns 0 if the execution is successful, otherwise a non-zero return indicates a failure. After the code is typed in, it must be compiled. The compiler translates the human-readable source code into object code. The files containing source code have extension cpp, h, hpp, the object code is saved in files with extension obj (in Windows) or o (in Unix, Linux) The compiler checks syntax and many things, and compilation might result in error messages. The compiler might change the (object) code to something equivalent and does lots of optimization. Some of the most popular C++ compilers: Visual C++, GCC, Intel, etc. The pieces of the object code are linked together by the linker and an executable file is created. The source code is portable between different platforms, the object code and the executable are not. Errors could be: compile-time, link-time or run-time-errors. A program with input Financial Computing with C++, Lecture 1 - p17/24 A program with input Financial Computing with C++, Lecture 1 - p18/24 Extract from Lecture010203/Example1.hpp : 1 std::cout << "Please tell me your first " << 2 "name and your age." << std::endl; 3 4 std::string sfirstname; 5 int iage; 6 7 std::cin >> sfirstname; 8 std::cin >> iage; 9 std::cout << "Hi, " << sfirstname << "! " ; if( iage<30 ) 12 std::cout<< "Five years from now, " << 13 "you ll be only " << iage+5 << ".\n"; 14 else 15 std::cout << "No offense, but you re old. " 16 "Five years from now, you ll be " 17 << iage+5 << "." << std::endl; The output of the previous code: Please tell me your first name and your age. Greg 33 Hi, Greg! No offense, but you re old. Five years from now, you ll be 38.

6 Financial Computing with C++, Lecture 1 - p23/24 Financial Computing with C++, Lecture 1 - p24/24 A program with input A program with input 1 std::string sfirstname; 2 int iage; In C++, variables must be declared first. Variables can be initialised when defined, but this could also be done later. These variables are local and will be destroyed at the end of the scope (i.e. at the brace } closing the brace { before the definition, excluding the braces denoting nested scopes) The definition must contain the type of the variable. There are built-in types in the core language: int, short int, long int, unsigned int, unsigned short int, etc. float, double, long double char, unsigned char, bool enum (see later) The standard library contains lots of types (e.g. see #include<string>). One can implement his/her own types, i.e. classes (see later). Variables can be defined to be const, e.g.: const double tiny=1e-21; The value of const variables must be initialised at declaration and won t change during its life time. If at some point in the code, one tries to modify the value of a const variable, the compiler will give an error message. 1 std::cin >> sfirstname; 2 std::cin >> iage; cin reads from console into variables. The operator >> is used for reading in values. If the typed in value has a type different from what is expected, then it is converted. For example, double is converted to int with losing some information: Please tell me your first name and your age. Felix 6 Hi, Felix! Five years from now, you ll be only 11. A program with input Financial Computing with C++, Lecture 1 - p21/24 Summary Financial Computing with C++, Lecture 1 - p22/24 1 if( CONDITION_TRUE ) { } 4 else if { } 7 else { } An if block uses the keywords if, else if, else The condition is to be written between ( and ) The statements of each branch form a scope, i.e. variables defined there will be local, i.e. restricted to their scope. One can skip the braces in case of single statement scopes. #include directive namespaces variable declaration types std::cout<< if then else function declaration iostream standard library, std variable initialisation built-in types std::cin>> return function definition

7 Financial Computing with C++, Lecture 2 - p3/22 Financial Computing with C++, Lecture 2 - p4/22 Outline Financial computing with C++ LG Gyurkó University of Oxford Control flow and operations Operators Enum and switch Michaelmas Term 2015 Functions Built-in maths functions Outline of the course Financial Computing with C++, Lecture 2 - p1/22 Outline of this lecture - control flow (loops, etc) Financial Computing with C++, Lecture 2 - p2/22 Programming is learned by writing programs. Brian Kernighan Good evening. Tonight on It s the Mind, we examine the phenomenon of déjà vu, that strange feeling we sometimes get that we ve lived through something before, that what is happening now has already happened tonight on It s the Mind we examine the phenomenon of déjà vu, that strange feeling we sometimes get that we ve... [looks puzzled] Monty Python s Flying Circus

8 Financial Computing with C++, Lecture 2 - p7/22 Financial Computing with C++, Lecture 2 - p8/22 Control flow and operations 1 #include <iostream> 2 #include <iomanip> 3 #include <vector> 4 #include <algorithm> 5 void Example2() 6 { 7 double dmark; 8 std::vector<double> vmarks; 9 std::cout << "Please, type in your marks." << std::endl; while(std::cin >> dmark) 12 vmarks.push_back(dmark); 13 double daverage(0); 14 std::vector<double>::size_type MarksSize=vMarks.size(), i; for(i=0; i<markssize; ++i) 17 daverage+=vmarks[i]; daverage/=markssize; std::sort(vmarks.begin(), vmarks.end()); 22 double median=vmarks[markssize/2]; std::cout<< "The median is " << median << 25 " and the average is: " << std::setprecision(4) 26 << daverage << std::endl; 27 } Control flow and operations 1 #include <iostream> 2 #include <iomanip> 3 #include <vector> 4 #include <algorithm> iomanip contains setprecision vector contains the implementation of the standard-library s vector container algorithm is a collection of routines for manipulating standard-library style containers Control flow and operations Financial Computing with C++, Lecture 2 - p5/22 Control flow and operations Financial Computing with C++, Lecture 2 - p6/22 1 while(std::cin >> dmark) 2 vmarks.push_back(dmark); In general: 1 while( CONDITION_TO_CONTINUE ){ 2 //... 3 } The while loop can be used to execute a set of statements repeatedly as long as the condition-to-continue is true The set of statements forms a scope, i.e. anything declared within the scope is destroyed when it goes out of scope In case of single statement while scopes, the braces can be omitted. Use the break; statement to terminate the execution of the nearest enclosing loop. Use the continue; statement to jump to the beginning of the next iteration (including the test) in the nearest enclosing while statement. 1 for(i=0; i<markssize; ++i) 2 daverage+=vmarks[i]; In general: 1 for(init_loop_variables; CONDITION_TO_CONTINUE; UPDATE_LOOP_VARIABLES) 2 { 3 //... 4 } The for loop requires a loop variant, a condition to continue and a statement updating the loop variant The set of statements forms a scope, i.e. anything declared within the scope is destroyed when it goes out of scope In case of single statement for scopes, the braces can be omitted. Use the break statement to terminate the execution of the nearest enclosing loop. Use the continue; statement to jump to the beginning of the next iteration in the nearest enclosing for statement. The next iteration includes the update-loop-variant expression as well.

9 Financial Computing with C++, Lecture 2 - p11/22 Financial Computing with C++, Lecture 2 - p12/22 Control flow and operations Control flow and operations 1 vmarks.push_back(dmark); 2 std::vector<double>::size_type MarksSize=vMarks.size(), i; 3 daverage+=vmarks[i]; std::vector is a templated container, it can contain elements of any type (if copyable and assignable) The most important vector operations are vector<t> v; - defines an empty vector to hold elements of type T vector<t> v(n); - defines a vector of size n v.push_back(value) - inserts a new element at the end of the vector v.size() - returns the number of entries v.resize(n); - sets the size to n, note: resizing of vectors is possible, it s also possible to reset the capacity. v[i] - accesses the ith entry, note: indexing starts at 0 v.begin() - returns an iterator to (the address of) the first entry v.end() - returns an iterator to the memory place one past the last element (push_back will write here) Vectors allow random access of elements, vectors are quick at inserting/erasing elements at the end, On the other hand, vectors are inefficient at removing/inserting elements other than at the end. We will see more about std::vector s in lecture 5. 1 std::sort(vmarks.begin(), vmarks.end()); sort is a standard-library algorithm. It takes a range and does efficient sorting. Works on many container types, however some of the containers have their own sorting algorithms (possibly even more efficient). See more details later (lecture 6). Operators Financial Computing with C++, Lecture 2 - p9/22 Control flow - switch Financial Computing with C++, Lecture 2 - p10/22 a=b assignment a=b+c, b-c, b*c, b/c, b%c (modulo) ++i, i++ pre-increment and post-increment, with analogy: --i, i-- a+=b is equivalent to a=a+b, with analogy: a-=b, a*=b, a/=b!a logical negation (a && b), (a b) logical and and or (return bool) a==b, a!=b, a>b, a>=b, a<b, a<=b relation operators (return bool) (a? b : z) yields b if a is true, z otherwise. vec[i] accesses the ith entry of the container vec obj.prop accesses the member named prop of the object obj obj.mfun(...) calls the member function named mfun of the object obj The action of the operator on the simple types is straightforward. What do they do on user defined types? - Whatever the user makes them do! 1 void Example3() 2 { 3 enum Month{jan=1, feb, mar, apr, may, jun, 4 jul, aug, sep, oct, nov, dec}; 5 std::cout << "Please type in the current " << 6 "month (in form of an integer)!" << std::endl; 7 int iinput; 8 std::cin >> iinput; 9 Month minput=month(iinput); switch(minput){ 12 case jan: case mar: case may: case jul: 13 case aug: case oct: case dec: 14 std::cout<< "There are 31 days in this month."<< std::endl; 15 break; 16 case apr: case jun: case sep: case nov: 17 std::cout<< "There are 30 days in this month."<< std::endl; 18 break; 19 case feb: 20 std::cout << "There are 28 days in this month, " << 21 "unless it s a leap year." << std::endl; 22 break; 23 default: 24 std::cout << "That s not a month." << std::endl; 25 break; 26 } 27 }

10 Financial Computing with C++, Lecture 2 - p15/22 Financial Computing with C++, Lecture 2 - p16/22 Control flow - switch 1 enum Month{jan=1, feb, mar, apr, may, jun, 2 jul, aug, sep, oct, nov, dec}; Examples: 1 Month m=feb; 2 //... 3 if(m==feb) 4 //... Use enum when types with names constants are required. Enumeration by default assigns consecutive integers starting at 0 to labels. One can specify different starting value, or even non-consecutive integer values. In general, avoid hard coded constants magic numbers. Control flow - switch 1 switch(minput){ 2 case jan: case mar: case may: case jul: 3 case aug: case oct: case dec: 4 std::cout<< "There are 31 days in this month."<< std::endl; 5 break; 6 case apr: case jun: case sep: case nov: 7 std::cout<< "There are 30 days in this month."<< std::endl; 8 break; 9 case feb: 10 std::cout << "There are 28 days in this month, " << 11 "unless it s a leap year." << std::endl; 12 break; 13 default: 14 std::cout << "That s not a month." << std::endl; 15 break; 16 } The switch statement evaluates an expression and jumps to the matching case label Use the break; statement at the end of the block, otherwise execution will follow from one label to the next. The value on which switch switches must be integer, char or enum. Other types (string, double etc.) are not accepted. The value in the case labels must be constant expression. Functions Financial Computing with C++, Lecture 2 - p13/22 Functions - passing arguments Financial Computing with C++, Lecture 2 - p14/22 A reminder of what has been said about functions. Functions must be declared before use. The declaration is of the form: 1 type_name name(arg1_type, arg2_type,...); Any statement of the same syntax structure is regarded as a function declaration. If a function does not return value, its return type must be void. The implementation of a function can be placed after the main, or even in different cpp file. Each function is determined by its name and the list of argument types. I.e. functions can share their name if their argument lists are different. During execution it will be recognized which one to call. E.g.: 1 void MyMax(int, int ); 2 void MyMax(std::vector<double>); 1 void swap1(int a, int b) 2 { 3 int temp=a; 4 a=b; 5 b=temp; 6 } 7 void Example4() 8 { 9 header(4); 10 int a(1), b(2); 11 std::cout<< "Before calling swap1 on a and b," << std::endl; 12 std::cout<< "a: " << a << " b: " << b << std::endl; 13 swap1(a,b); 14 std::cout<< "After calling swap1 on a and b," << std::endl; 15 std::cout<< "a: " << a << " b: " << b << std::endl; 16 // } Before calling swap1 on a and b, a: 1 b: 2 After calling swap1 on a and b, a: 1 b: 2 OK, what has just happened here???

11 Financial Computing with C++, Lecture 2 - p19/22 Financial Computing with C++, Lecture 2 - p20/22 Functions - passing arguments problem explained Functions - passing arguments problem explained Arthur: Bedevere: Arthur: Bedevere: Arthur: What happens now? Well, now, uh, Lancelot, Galahad, and I, uh, wait until nightfall, and then leap out of the rabbit, taking the French, uh, by surprise. Not only by surprise, but totally unarmed! Who leaps out? Lancelot, Galahad, and I, uh, leap out of the rabbit, uh, and uh... Ohh. Holy grail 1 void swap1(int a, int b) 2 { 3 int temp=a; 4 a=b; 5 b=temp; 6 } The function Swap1 in fact takes two arguments. However it does not work on the variables a and b, but it works on copies of them. I.e. the variables themselves are not passed to the function, they are NOT in the rabbit. This strategy is referred to as pass-by-value. The variables in the function body are local variables, created in the scope and destroyed when leaving the scope. Pass-by-value is useful to pass objects of small size to a function if only their values are needed, especially if the original objects are not to be modified. Functions - passing arguments problem fixed Financial Computing with C++, Lecture 2 - p17/22 Functions - passing arguments by (const) reference Financial Computing with C++, Lecture 2 - p18/22 1 void swap2(int & a, int & b) 2 { 3 int temp=a; 4 a=b; 5 b=temp; 6 } The symbol & indicates that the variables are passed by reference. In this case, no copies of the original variables are created, but the function directly accesses them. The operations in the function body will act on the original variables, changing their values. Pass-by-reference can be used to avoid copying objects of significant size. An alternative solution is to use pointers. See later. 1 void AddVector(const vec_type & a, const vec_type & b, vec_type & c) 2 { 3 vec_type::size_type n=std::min(a.size(),b.size()); 4 c.resize(n); 5 for(vec_type::size_type i=0; i<n; ++i) 6 c[i]=a[i]+b[i]; 7 } Further possibility: use pass-by-const-reference if one wants to avoid copying and wants to protect the argument from modification. In this example, a and b are passed by const reference, c is passed by reference No copies of a or b are created, saving memory and time The const keyword protects a and b from modification (the compiler would give an error if any modification was attempted). The function works on the original c, the entries of c will be modified (that s fine, c is passed by non-const reference). Note: the function explicitly returns void, however, implicitly, it returns c. This strategy avoids copying the returned value into some objects, again saving time and memory.

12 Some useful functions Summary #include<iomanip> std::setprecision() Use #include <cmath> to access the following functions trigonometric functions: cos, sin, tan, acos, asin, atan hyperbolic functions: cosh, sinh, tanh exponential/logarithmic functions: exp, log, log10, modf, frexp, ldexp power functions: pow, sqrt rounding, absolute value, remainder: ceil, floor, fabs, fmod while for #include<vector> std::vector<t>, resize(), etc. #include<algorithm> sort() =, +, -, *, /, % ++, --, +=, -=, etc. enum switch, case function signature pass by value function scope pass by (const) reference #include<cmath> Financial Computing with C++, Lecture 2 - p21/22 Financial Computing with C++, Lecture 2 - p22/22

13 Financial Computing with C++, Lecture 3 - p3/21 Financial Computing with C++, Lecture 3 - p4/21 Outline Lifetime of variables Financial computing with C++ Using types LG Gyurkó References University of Oxford Michaelmas Term 2015 Pointers Namespaces Naming conventions Outline of the course Financial Computing with C++, Lecture 3 - p1/21 Outline of this lecture - types, variables, lifetime Financial Computing with C++, Lecture 3 - p2/21 Programming is learned by writing programs. Brian Kernighan Roger Murdock: Flight er, you are cleared for take-off. Captain Oveur: Roger! Roger Murdock: Huh? Tower voice: L.A. departure frequency, 123 point 9 er. Captain Oveur: Roger! Roger Murdock: Huh? Victor Basta: Request vector, over. Captain Oveur: What? Tower voice: Flight er cleared for vector 324. Roger Murdock: We have clearance, Clarence. Captain Oveur: Roger, Roger. What s our vector, Victor? Tower voice: Tower s radio clearance, over! Captain Oveur: That s Clarence Oveur. Over. Tower voice: Over. Captain Oveur: Roger. Roger Murdock: Huh? Tower voice: Roger, over! Roger Murdock: What? Captain Oveur: Huh? Victor Basta: Who? Airplane!

14 Financial Computing with C++, Lecture 3 - p7/21 Financial Computing with C++, Lecture 3 - p8/21 Lifetime of variables Creating objects - memory layout 1 int main() 2 { 3 int i=0; 4 Global variable - static storage Local variable - stack storage - object is created in a block ({...}) and automatically destroyed at the end of the block (when goes out of scope) Using the new operator - free store or heap - must be destroyed using the delete operator (more about new and delete under pointers) 5 if(i==0) 6 { 7 int j=0; // j is created in this block 8 9 i+=j; // this is OK, this block is part of i s scope 10 // and hence, i is accessible from here 11 } // j is destroyed at this point j; // ERROR, j no longer exists 14 } Types of the core language Recall: when declaring a variable, its type must be specified. The following types are provided by the core language bool - represents logical value, takes two possible values: true and false signed int, or just int - integer, can be positive or negative unsigned int - integer, only positive, if negative value is assigned, the compiler generates an error message float - single-precision floating point number double - double-precision floating point number char - represents a narrow character void - absence of any value, no variable can be declared void, however functions can be declared to return void enum - enumerated types (see lecture 2) long... - extended precision versions available for int and double Using types in general - specifiers Financial Computing with C++, Lecture 3 - p5/21 References Financial Computing with C++, Lecture 3 - p6/21 Variables can be declared const - the value of a const object cannot be modified, moreover, a non-const member function of a const object cannot be called (see later) Note: a const object must be initialised at declaration mutable - denotes a data member that can be modified even if the object is const static - static lifetime objects, its value is remembered when the execution returns to the scope it has been declared in Note: static data members must be initialised outside of the class declaration (see later) static member functions will be introduced later. 1 for(unsigned int i=0; i<3; ++i) 2 for(unsigned int j=0; j<3; ++j) 3 { 4 static unsigned int icounter(0); 5 std::cout << ++icounter << ", "; 6 } 1, 2, 3, 4, 5, 6, 7, 8, 9, 1 int a; // a plain simple variable 2 int & b=a; // b is a reference to a 3 4 a=1; 5 std::cout << "After a=1, a: " << a << " b: " << b << std::endl; 6 7 b=2; 8 std::cout << "After b=2, a: " << a << " b: " << b << std::endl; After a=1, a: 1 b: 1 After b=2, a: 2 b: 2 b is defined to be a reference to a, b is nothing but another name one can use to refer to a. The reference must be initialized at declaration. Note: once b is initialized to be the reference to a, it will always refer to the variable a, and cannot be re-assigned to another variable. (This is NOT equivalent to non-modifiable value!) Reference to base can actually take a reference to derived (see later). Return type of a function can be reference only if it refers to object existing before the function is called (e.g. class member functions, see later).

15 Financial Computing with C++, Lecture 3 - p11/21 Financial Computing with C++, Lecture 3 - p12/21 References - examples References - examples 1 // Vector is some vector type with some 2 // algebraic operations defined 3 4 Vector & payoff(const Vector & rfactors) 5 { 6 Vector vpayoff; 7 // some code using, but not modifying rfactors 8 // and writing into vpayoff 9 // return vpayoff; // ERROR!! 11 } vpayoff is created in the scope of the function vpayoff is destroyed at the end of the function s scope the function returns reference to a non-existing object - ERROR! 1 Vector & payoff(const Vector & rfactors, Vector & rpayoff) 2 { 3 // some code using, but not modifying rfactors 4 // and writing into rpayoff 5 //... 6 return rpayoff; 7 } rpayoff is created before the function call the results are written into rpayoff the function returns reference to rpayoff, it s OK now, rpayoff is not destroyed at the end of the function execution An example: 1 Vector vfactors(n), vpayoff(m), vsum(m); 2 vsum*=0.0; 3 4 for(int i=0; i<isamplesize; ++i) 5 { 6 // generate vfactors... 7 vsum+=payoff(vfactors,vpayoff); 8 } Pointers ptr data... Financial Computing with C++, Lecture 3 - p9/21 What is a pointer? Pointer to an object is the value representing the memory address of the object. In many cases, it s cheaper to pass the pointer than the whole object. The object (both data and member functions ) is accessible through its pointer. How to create a pointer? Declaration of a pointer to an object of type int: 1 int * iptr; Take the address of an existing object using the address operator &: 1 int * iptr= &i; //where i has been declared earlier Allocate memory for a new object using operator new (see later). One can define pointers to function, this is one way to pass functions as arguments. How to access the object through its pointer? By the dereference operator *: Pointers - as function arguments Extract from Lecture010203/Example4.hpp : 1 void swap3(int *a, int *b) 2 { 3 int temp=*a; 4 *a=*b; 5 *b=temp; 6 } 7 void Example4() 8 { 9 int a(1), b(2); 10 std::cout<< "Before calling swap3 on a and b," << std::endl; 11 std::cout<< "a: " << a << " b: " << b << std::endl; 12 swap3(&a,&b); // note: addresses are passed 13 std::cout<< "After calling swap3 on a and b," << std::endl; 14 std::cout<< "a: " << a << " b: " << b << std::endl; 15 } Output: Before calling swap3 on a and b, a: 2 b: 1 After calling swap3 on a and b, a: 1 b: 2 Financial Computing with C++, Lecture 3 - p10/21 1 int i=*iptr;

16 Financial Computing with C++, Lecture 3 - p15/21 Financial Computing with C++, Lecture 3 - p16/21 Pointers - examples Pointers - continued Extract from Lecture010203/Example7.hpp : 1 int a(1); 2 int * ptr1=&a; //the address of a is taken 3 std::cout<< "Value of a after first assignment: " 4 << a << std::endl; 5 std::cout<< "Value of a accessed through its address: " 6 << *ptr1 << std::endl; 7 *ptr1+=2; //value of a is changed 8 std::cout<< "Changing the value of a through its " 9 << "address: " << a << std::endl; 10 int * ptr2=new int(3); //new variable on the heap 11 std::cout<< "The value stored at the address ptr2: " 12 << *ptr2 << std::endl; 13 delete ptr2; //must be deleted at some point 14 std::cout<< "The value stored at the address ptr2 " 15 << "after releasing memory: " << *ptr2 << std::endl; Output: Value of a after first assignment: 1 Value of a accessed through its address: 1 Changing the value of a through its address: 3 The value stored at the address ptr2: 3 The value stored at the address ptr2 after releasing memory: 3 The memory for the local variables is allocated and freed automatically on the stack. The memory on the heap can be allocated using the operator new and must be freed using the operator delete before the end of the program. The use of new and delete requires care, even if delete is part of the code, the execution might take turns different from what normally expected, resulting in memory leak. One can allocate memory for arrays and free using delete[] : 1 T * p=new T[n]; 2 //... 3 delete [] p; Safer alternatives - smart pointers, for example std::auto_ptr, boost::scoped_ptr, boost::shared_ptr, etc. Pointers - smart pointers Financial Computing with C++, Lecture 3 - p13/21 Pointers to functions Financial Computing with C++, Lecture 3 - p14/21 Smart pointers are like standard pointers, the operators *, -> behave like in the standard case There is no need to delete smart pointers, they do that themselves when going out of scope Extract from Lecture010203/Example8.hpp : 1 namespace ptrnamespace = boost; 2 typedef ptrnamespace::shared_ptr<int> int_ptr; 3 //defining an alternative name for the type name 4 5 int_ptr returnpointer(int Arg){ 6 int_ptr pval(new int(arg)); 7 return pval; 8 } 9 10 void Example8() { 11 int_ptr ptemp=returnpointer(1); 12 std::cout<< "Value: " << *ptemp << std::endl; 13 } // ptemp is out of scope and automatically deleted here 1 typedef double (*MyFunType)(double); It s possible to define type for functions with a given signature (argument types and return type) In the above example, the type name MyFunType is the type of functions taking one double and returning double This is useful for passing functions as arguments. See the example on the next slide.

17 Financial Computing with C++, Lecture 3 - p19/21 Financial Computing with C++, Lecture 3 - p20/21 Pointers to functions 1 typedef double (*MyFunType)(double); 2 3 // function declarations 4 double fn1(double); 5 double fn2(double); 6 7 void Example9() { 8 header(9); 9 MyFunType fnptr1=&fn1; //fn1 has been declared, can be used 10 MyFunType fnptr2=fn2; //fn2 has been declared, can be used 11 std::cout << "fnptr1 evaluated at 3.0: " 12 << fnptr1(3.0) << std::endl; 13 std::cout << "fnptr2 evaluated at 3.0: " 14 << fnptr2(3.0) << std::endl; 15 } // function implementation 18 double fn1(double darg){ 19 return darg+2.0;} 20 double fn2(double darg){ 21 return darg*darg;} Pointers to functions 1 typedef double (*PayoffFunction)(double); 2 3 double montecarlo(int isamplesize, PayoffFunction payoffarg) 4 { 5 double dval(0.0); 6 for(int i=0; i<isamplesize; ++i) 7 { 8 double dstock; 9 //... generate stock price dstock 10 dval+=payoffarg(dstock); 11 } 12 return dval; 13 } double particularpayoff(double Arg) 16 { //... } int main() 19 { 20 int isamplesize(1000); 21 double dval=montecarlo(isamplesize, particularpayoff); 22 // } fnptr1 evaluated at 3.0: 5 fnptr2 evaluated at 3.0: 9 MonteCarlo() takes an argument that is a pointer to a function the implementation of MonteCarlo() contains the common part the payoff-specific part is factorised out (particular payoff) Namespaces Financial Computing with C++, Lecture 3 - p17/21 With namespaces, one can group functions, classes, typedefs, etc. under a name Structure code, divide the global scope into sub-scopes A way to avoid name conflicts 1 namespace mynamespace 2 { 3 int ia; //declaring a variable 4 int myfun(int);// declaring a function 5 } 6 7 int mynamespace::myfun(int iarg) 8 { //... } // implementing the function 9 10 { 11 int ib=mynamespace::ia; //accessing objects from a namespace 12 } { 15 using mynamespace::ia; // introducing particular names 16 // from a namespace into this scope 17 int ib=ia; 18 } { 21 using namespace mynamespace; // introduce an entire namespace 22 // into this scope 23 } Naming conventions There are different name conventions, you might need to adapt to the one used. Examples. type names: mixed case, starting with upper-case: MyType variable names: mixed case, starting with lower case: samplesize Financial Computing with C++, Lecture 3 - p18/21 Hungarian notation : isamplesize - int, dstockprice - double, rpayoffvec - reference, pmyclass - pointer, m_idatamember - integer data member function names: mixed case, starting with lower case: montecarlo() named constants: all upper-case: MAX_INT names of namespaces: all lower-case variables with long scope must have long name, variable with short scope can have short names names of boolean variables and methods should start with the prefix is: isknockedin

18 Summary stack storage core types pointers heap storage specifiers new, delete pointers to functions address &, dereference a pointer * reference namespaces using naming conventions Financial Computing with C++, Lecture 3 - p21/21

19 Financial Computing with C++, Lecture 4 - p3/19 Financial Computing with C++, Lecture 4 - p4/19 Outline Financial computing with C++ General properties of classes LG Gyurkó Members of classes University of Oxford Constructors and destructors Michaelmas Term 2015 Special members - overloaded operators Outline of the course Financial Computing with C++, Lecture 4 - p1/19 Outline of this lecture - creating new types Financial Computing with C++, Lecture 4 - p2/19 Programming is learned by writing programs. Brian Kernighan Man: Look, Ivan! My own hybrid of a potato and tomato! Ivan: And this should be a potato below-ground and a tomato above-ground? Man: Precisely! Ivan: And where are the tomatoes? I can t see anything! Man: It s too early, it s only March.... Man: Last year, it did not succeed. The leaves were that of a potato and the roots that of a tomato. Ivan: Then your hybrid sucks. Life and Extraordinary Adventures of Private Ivan Chonkin

20 Financial Computing with C++, Lecture 4 - p7/19 Financial Computing with C++, Lecture 4 - p8/19 Types We have seen types provided by the core language. In C++ it s possible to implement custom types. What is a type? Collection of states described by some data members Behaviour implemented by member functions (methods) Classes - ComplexNumber, the interface Example: ComplexNumber is a class with a few members. (The example is taken from Exceptional C++ by Herb Sutter with minor modifications.) 1 #include <iostream> 2 3 class ComplexNumber 4 { 5 public: 6 ComplexNumber(); 7 explicit ComplexNumber(double, double=0.0); 8 9 ComplexNumber & operator+=(const ComplexNumber &); 10 ComplexNumber & operator-=(const ComplexNumber &); 11 ComplexNumber & operator*=(const ComplexNumber &); 12 ComplexNumber & operator/=(const ComplexNumber &); ComplexNumber & operator+=(const double &); 15 ComplexNumber & operator-=(const double &); 16 ComplexNumber & operator*=(const double &); 17 ComplexNumber & operator/=(const double &); std::ostream & print(std::ostream &) const; private: 22 double dre_; 23 double dim_; 24 }; General properties of classes Financial Computing with C++, Lecture 4 - p5/19 we distinguish between class (the type) and instance of a class (a particular value) classes can have public, private and protected members or properties (members of each category can exists simultaneously in one class) public members are accessible without restrictions (by any function or type) private members of an instance C of class type_of_c are only accessible by instances of the same class type_of_c and objects declared as friend of type_of_c (examples... maybe later). protected members of an instance C of class type_of_c are accessible by friend s and instances of classes derived from type_of_c. (... later) members of a class can be data and function. unless otherwise specified, class members are private by default struct is similar to class, however struct members are public by default the keywords class and struct are required when declaring a class or struct respectively. members can be accessed (given the permission) using the operator. : C.data or C.fun(); or members can be accessed through pointers using the operator ->: C_ptr->data, C_ptr->fun() Members - in general Member functions member functions of a class are part of its namespace members functions take the instance they are called from as an argument, although the instance is not explicitly listed in the argument list Financial Computing with C++, Lecture 4 - p6/19 the instance is a const argument, if the member is declared const, note the syntax: fun(...) const; member functions can return reference to the instance the function was called from, this can be achieved by dereferencing the pointer this to self. Don t give access to the class non-public members if it s not necessary. In other words: Prefer non-member non-friend functions to member functions. (Scott Meyers, Effective C++)

21 Financial Computing with C++, Lecture 4 - p11/19 Financial Computing with C++, Lecture 4 - p12/19 Members - in general Classes - ComplexNumber, members Data members data members can be plain values, references, pointers and const versions of these const and reference members must be declared at the construction of the instance it s recommended to make data members non-public, although in some cases public data members are useful (e.g. first and second are public members of pair<t1,t2>) 1 class ComplexNumber 2 { 3 4 private: 5 double dre_; 6 double dim_; 7 }; ComplexNumber has two private members, both are data members Classes - ComplexNumber, members Financial Computing with C++, Lecture 4 - p9/19 Member functions - constructors & destructors Financial Computing with C++, Lecture 4 - p10/19 1 class ComplexNumber 2 { 3 public: 4 ComplexNumber(); 5 explicit ComplexNumber(double, double=0.0); 6 7 ComplexNumber & operator+=(const ComplexNumber &); 8 9 std::ostream & print(std::ostream &) const; ComplexNumber a few member functions, each of them is public the member print() is a const function (does not modify the data members), the other functions or non-const members. constructors initialize instances of classes, destructors take care of destroying the instance constructors and destructors return nothing constructors initialize data members, if initialization is not done explicitly, the compiler will call the default constructor of the data members (even if there are assignments in the function body) initializing data from constructors has a special syntax (see examples) default constructors take no arguments If no constructor is declared, the compiler will create a default constructor, which initializes the data members calling their default constructors copy constructors take an instance of the same type (usually by const reference) and create a new instance from that If no copy constructor is declared, the compiler will create one, which initializes the data members calling their copy constructors. If no destructor is declared, the compiler will create one. Note that the compiler generated default constructor, copy constructor and destructor might not work properly in certain cases. (See Effective C++ by Scott Meyers for further details.)

22 Financial Computing with C++, Lecture 4 - p15/19 Financial Computing with C++, Lecture 4 - p16/19 Classes - ComplexNumber, constructors and destructors Classes - ComplexNumber, constructors and destructors The class is implemented in Practical02/ComplexNumber.cpp : 1 class ComplexNumber 2 { 3 public: 4 ComplexNumber(); 5 explicit ComplexNumber(double, double=0.0); A default constructor is declared (an defined somewhere), the compiler generated one might not initialize the data members to 0 A second constructor is declared, takes two double s, specifies a default value to the second argument. The argument with a default value might be omitted from the call, and its default value will be passed automatically. Note the explicit keyword. It protects against implicit type conversion (see Effective C++ by Scott Meyers, or Exceptional C++ by Herb Sutter for details). No copy constructor is declared, the one generated by the compiler will copy the data members, which is fine here. No destructor is declared, the compiler will generate one. 1 #include "ComplexNumber.hpp" ComplexNumber::ComplexNumber() : dre_(0.0), dim_(0.0) {} 5 6 ComplexNumber::ComplexNumber(double drearg, double dimarg) 7 : dre_(drearg), dim_(dimarg) {} The usual practice is to put the class declaration into a header file and the implementation into a cpp file. Note that the header file must be #include ed in the cpp file to give access to the declarations. A default constructor initializes both data members to 0. Nothing is done beyond initialization, that s why the function body is empty. The second constructor initializes the data members to the values provided by the arguments. If no second argument is specified by the call, the default value 0 will be taken. The function body is empty. Note the use of the namespace ComplexNumber:: when accessing the function members of ComplexNumber for implementation. Special members - overloaded operators Financial Computing with C++, Lecture 4 - p13/19 one can overload most of the C++ operators (see lecture 2 for a list of available operators) the declaration of operator members have a special syntax: 1 class ComplexNumber 2 { 3 public: 4 ComplexNumber & operator+=(const ComplexNumber &); i.e. operator members have a special name, built of the keyword operator and the sign of the operator. the typical two argument operators (e.g. +=, -=, <, <= etc.) can be called two in equivalent ways, e.g.: C1<C2 is equivalent to C1.operator<(C2) a special operator is the () operator () can be called in the form: C1.operator()(arguments) or C1(arguments) the latter form looks like a calling a simple function named C1, that s why classes with operator () implemented are referred to as function objects. the operator = with argument (const reference to) the user-defined class is the copy assignment. If no copy assignment is declared, the compiler will generate one. Special members - operator () example Example: Call option payoff 1 class CallOption 2 { 3 public: 4 CallOption(double darg=1.0) : dk_(darg) {} 5 6 double operator()(double darg) { 7 return (darg<dk_)? 0.0 : darg-dk_; 8 } 9 10 private: 11 double dk_; 12 }; Financial Computing with C++, Lecture 4 - p14/19 The class CallOption has an overloaded operator () member, taking a double and returning a double The operator () is called in the following example. 1 // construction and usage 2 CallOption Call1(1.0); //construction 3 cout << "Payoff at S=1.5, K=1 is " << Call1(1.5) << endl; 4 // alternatively, using a temp object: 5 cout << "Payoff at S=1.5, K=1 is " << CallOption(1.0)(1.5) << endl;

23 Financial Computing with C++, Lecture 4 - p19/19 ComplexNumber - operator implementation 1 ComplexNumber & ComplexNumber::operator+=(const ComplexNumber & cnarg) 2 { 3 dre_+=cnarg.dre_; 4 dim_+=cnarg.dim_; 5 return *this; 6 } operator += was made a member, because it accesses private members it returns a reference to the instance it was called from using the dereferenced this pointer this makes expressions like (a+=b)*=c valid once operator += is declared as member, the operator + can be implemented using operator +=, i.e. without accessing non-public members of ComplexNumber, therefore it is declared as global function and implemented as follows: 1 ComplexNumber operator+(const ComplexNumber & Arg1, 2 const ComplexNumber & Arg2) 3 { 4 ComplexNumber Res(Arg1); 5 return Res+=Arg2; 6 } ComplexNumber - operator implementation the operator << must be overloaded to display instances of user defined classes for displaying, usually the access of non-public members is required one way is to declare the global operator << a friend of the new class (see later) or implement a convenience public member: 1 std::ostream & ComplexNumber::print(std::ostream & os) const 2 { 3 return os << "(" << dre_ << ", " << dim_ << "i)"; 4 } now, operator << can be implemented using the member print note that this global function takes an ostream by reference and returns a reference to the same ostream the second argument of type ComplexNumber is the one to be displayed 1 std::ostream & operator<<(std::ostream & os, const ComplexNumber & cnarg) 2 { 3 return cnarg.print(os); 4 } Summary Financial Computing with C++, Lecture 4 - p17/19 Financial Computing with C++, Lecture 4 - p18/19 custom types data member public constructor operators class member function private destructor overloading operator() ostream operator << this

24 Financial Computing with C++, Lecture 5 - p3/18 Financial Computing with C++, Lecture 5 - p4/18 Outline Financial computing with C++ Template functions LG Gyurkó Template classes University of Oxford Non-type template arguments Michaelmas Term 2015 Template specialisation Outline of the course Financial Computing with C++, Lecture 5 - p1/18 Financial Computing with C++, Lecture 5 - p2/18 Outline of this lecture - Templates and static polymorphism Programming is learned by writing programs. Brian Kernighan Spectator: Mrs Gregory: Mr Gregory: I think it was Blessed are the cheesemakers. Aha, what s so special about the cheesemakers? Well, obviously it s not meant to be taken literally; it refers to any manufacturers of dairy products. Life of Brian

Financial computing with C++

Financial computing with C++ Financial Computing with C++, Lecture 4 - p1/19 Financial computing with C++ LG Gyurkó University of Oxford Michaelmas Term 2015 Financial Computing with C++, Lecture 4 - p2/19 Outline General properties

More information

Financial computing with C++

Financial computing with C++ Financial Computing with C++, Lecture 6 - p1/24 Financial computing with C++ LG Gyurkó University of Oxford Michaelmas Term 2015 Financial Computing with C++, Lecture 6 - p2/24 Outline Linked lists Linked

More information

Financial computing with C++

Financial computing with C++ Financial Computing with C++, Lecture 11 - p1/24 Financial computing with C++ LG Gyurkó University of Oxford Michaelmas Term 2015 Financial Computing with C++, Lecture 11 - p2/24 Outline Derived classes

More information

Functions and Recursion

Functions and Recursion Functions and Recursion 1 Outline Introduction Program Components in C++ Math Library Functions Functions Function Definitions Function Prototypes Header Files Random Number Generation Example: A Game

More information

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming

More information

Cours de C++ Introduction

Cours de C++ Introduction Cours de C++ Introduction Cécile Braunstein cecile.braunstein@lip6.fr Cours de C++ 1 / 20 Généralité Notes Interros cours 1/3 Contrôle TP 1/3 Mini-projet 1/3 Bonus (Note de Participation) jusqu à 2 points

More information

PHY4321 Summary Notes

PHY4321 Summary Notes PHY4321 Summary Notes The next few pages contain some helpful notes that summarize some of the more useful material from the lecture notes. Be aware, though, that this is not a complete set and doesn t

More information

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

Chapter 15 - C++ As A "Better C"

Chapter 15 - C++ As A Better C Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++ CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many

More information

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE? 1. Describe History of C++? The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was doing work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

Introduction to C ++

Introduction to C ++ Introduction to C ++ Thomas Branch tcb06@ic.ac.uk Imperial College Software Society October 18, 2012 1 / 48 Buy Software Soc. s Free Membership at https://www.imperialcollegeunion.org/shop/ club-society-project-products/software-products/436/

More information

PIC10B/1 Winter 2014 Exam I Study Guide

PIC10B/1 Winter 2014 Exam I Study Guide PIC10B/1 Winter 2014 Exam I Study Guide Suggested Study Order: 1. Lecture Notes (Lectures 1-8 inclusive) 2. Examples/Homework 3. Textbook The midterm will test 1. Your ability to read a program and understand

More information

A brief introduction to C++

A brief introduction to C++ A brief introduction to C++ Rupert Nash r.nash@epcc.ed.ac.uk 13 June 2018 1 References Bjarne Stroustrup, Programming: Principles and Practice Using C++ (2nd Ed.). Assumes very little but it s long Bjarne

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)

More information

PIC 10A Objects/Classes

PIC 10A Objects/Classes PIC 10A Objects/Classes Ernest Ryu UCLA Mathematics Last edited: November 13, 2017 User-defined types In C++, we can define our own custom types. Object is synonymous to variable, and class is synonymous

More information

Interview Questions of C++

Interview Questions of C++ Interview Questions of C++ Q-1 What is the full form of OOPS? Ans: Object Oriented Programming System. Q-2 What is a class? Ans: Class is a blue print which reflects the entities attributes and actions.

More information

Multimedia-Programmierung Übung 3

Multimedia-Programmierung Übung 3 Multimedia-Programmierung Übung 3 Ludwig-Maximilians-Universität München Sommersemester 2016 Ludwig-Maximilians-Universität München Multimedia-Programmierung 1-1 Today Ludwig-Maximilians-Universität München

More information

Introduction to C++ Systems Programming

Introduction to C++ Systems Programming Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions

More information

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Introduction to C++ Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 20, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O Outline EDAF30 Programming in C++ 2. Introduction. More on function calls and types. Sven Gestegård Robertz Computer Science, LTH 2018 1 Function calls and parameter passing 2 Pointers, arrays, and references

More information

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2 Numerical Computing in C and C++ Jamie Griffin Semester A 2017 Lecture 2 Visual Studio in QM PC rooms Microsoft Visual Studio Community 2015. Bancroft Building 1.15a; Queen s W207, EB7; Engineering W128.D.

More information

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works Type Aliases A name may be defined as a synonym for an existing type name. Traditionally, typedef is used for this purpose. In the new standard, an alias declaration can also be used C++11.Thetwoformsareequivalent.

More information

Chapter 3 - Functions

Chapter 3 - Functions Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number

More information

Outline. 1 About the course

Outline. 1 About the course Outline EDAF50 C++ Programming 1. Introduction 1 About the course Sven Gestegård Robertz Computer Science, LTH 2018 2 Presentation of C++ History Introduction Data types and variables 1. Introduction 2/1

More information

Introduction to C++ Introduction to C++ 1

Introduction to C++ Introduction to C++ 1 1 What Is C++? (Mostly) an extension of C to include: Classes Templates Inheritance and Multiple Inheritance Function and Operator Overloading New (and better) Standard Library References and Reference

More information

Part X. Advanced C ++

Part X. Advanced C ++ Part X Advanced C ++ topics Philip Blakely (LSC) Advanced C++ 158 / 217 References The following are highly regarded books. They are fairly in-depth, and I haven t read them in their entirity. However,

More information

Maciej Sobieraj. Lecture 1

Maciej Sobieraj. Lecture 1 Maciej Sobieraj Lecture 1 Outline 1. Introduction to computer programming 2. Advanced flow control and data aggregates Your first program First we need to define our expectations for the program. They

More information

G52CPP C++ Programming Lecture 20

G52CPP C++ Programming Lecture 20 G52CPP C++ Programming Lecture 20 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Wrapping up Slicing Problem Smart pointers More C++ things Exams 2 The slicing problem 3 Objects are not

More information

C The new standard

C The new standard C++11 - The new standard Lars Kühne Institut für Informatik Lehrstuhl für theoretische Informatik II Friedrich-Schiller-Universität Jena January 16, 2013 Overview A little bit of history: C++ was initially

More information

AN OVERVIEW OF C++ 1

AN OVERVIEW OF C++ 1 AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function

More information

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program: Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

More information

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED Outline - Function Definitions - Function Prototypes - Data

More information

Computing and Statistical Data Analysis Lecture 3

Computing and Statistical Data Analysis Lecture 3 Computing and Statistical Data Analysis Lecture 3 Type casting: static_cast, etc. Basic mathematical functions More i/o: formatting tricks Scope, namspaces Functions 1 Type casting Often we need to interpret

More information

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes Distributed Real-Time Control Systems Lecture 17 C++ Programming Intro to C++ Objects and Classes 1 Bibliography Classical References Covers C++ 11 2 What is C++? A computer language with object oriented

More information

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program: Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

More information

CS

CS CS 1666 www.cs.pitt.edu/~nlf4/cs1666/ Programming in C++ First, some praise for C++ "It certainly has its good points. But by and large I think it s a bad language. It does a lot of things half well and

More information

C++ Programming Lecture 1 Software Engineering Group

C++ Programming Lecture 1 Software Engineering Group C++ Programming Lecture 1 Software Engineering Group Philipp D. Schubert Contents 1. More on data types 2. Expressions 3. Const & Constexpr 4. Statements 5. Control flow 6. Recap More on datatypes: build-in

More information

CS93SI Handout 04 Spring 2006 Apr Review Answers

CS93SI Handout 04 Spring 2006 Apr Review Answers CS93SI Handout 04 Spring 2006 Apr 6 2006 Review Answers I promised answers to these questions, so here they are. I'll probably readdress the most important of these over and over again, so it's not terribly

More information

Professor Terje Haukaas University of British Columbia, Vancouver C++ Programming

Professor Terje Haukaas University of British Columbia, Vancouver  C++ Programming C++ Programming C++ code is essentially a collection of statements terminated by a semicolon, such as (spaces not needed): a = b + c; Most C++ code is organized into header files and cpp files, i.e., C++

More information

Tokens, Expressions and Control Structures

Tokens, Expressions and Control Structures 3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type

More information

Computer Science II Lecture 1 Introduction and Background

Computer Science II Lecture 1 Introduction and Background Computer Science II Lecture 1 Introduction and Background Discussion of Syllabus Instructor, TAs, office hours Course web site, http://www.cs.rpi.edu/courses/fall04/cs2, will be up soon Course emphasis,

More information

18. Dynamic Data Structures I. Dynamic Memory, Addresses and Pointers, Const-Pointer Arrays, Array-based Vectors

18. Dynamic Data Structures I. Dynamic Memory, Addresses and Pointers, Const-Pointer Arrays, Array-based Vectors 590 18. Dynamic Data Structures I Dynamic Memory, Addresses and Pointers, Const-Pointer Arrays, Array-based Vectors Recap: vector 591 Can be initialised with arbitrary size n 591 Recap: vector

More information

Computer Science II Lecture 2 Strings, Vectors and Recursion

Computer Science II Lecture 2 Strings, Vectors and Recursion 1 Overview of Lecture 2 Computer Science II Lecture 2 Strings, Vectors and Recursion The following topics will be covered quickly strings vectors as smart arrays Basic recursion Mostly, these are assumed

More information

Programming in C and C++

Programming in C and C++ Programming in C and C++ 5. C++: Overloading, Namespaces, and Classes Dr. Neel Krishnaswami University of Cambridge (based on notes from and with thanks to Anil Madhavapeddy, Alan Mycroft, Alastair Beresford

More information

Vector and Free Store (Pointers and Memory Allocation)

Vector and Free Store (Pointers and Memory Allocation) DM560 Introduction to Programming in C++ Vector and Free Store (Pointers and Memory Allocation) Marco Chiarandini Department of Mathematics & Computer Science University of Southern Denmark [Based on slides

More information

y

y The Unfit tutorial By Dr Martin Buist Initial version: November 2013 Unfit version: 2 or later This tutorial will show you how to write your own cost function for Unfit using your own model and data. Here

More information

Operator overloading

Operator overloading 1 Introduction 2 The copy constructor 3 Operator Overloading 4 Eg 1: Adding two vectors 5 The -> operator 6 The this pointer 7 Overloading = 8 Unary operators 9 Overloading for the matrix class 10 The

More information

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47 Lecture 8 Xiaoguang Wang STAT 598W February 13th, 2014 (STAT 598W) Lecture 8 1 / 47 Outline 1 Introduction: C++ 2 Containers 3 Classes (STAT 598W) Lecture 8 2 / 47 Outline 1 Introduction: C++ 2 Containers

More information

Getting started with C++ (Part 2)

Getting started with C++ (Part 2) Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching

More information

Operator overloading: extra examples

Operator overloading: extra examples Operator overloading: extra examples CS319: Scientific Computing (with C++) Niall Madden Week 8: some extra examples, to supplement what was covered in class 1 Eg 1: Points in the (x, y)-plane Overloading

More information

These are notes for the third lecture; if statements and loops.

These are notes for the third lecture; if statements and loops. These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

Programming with C++ as a Second Language

Programming with C++ as a Second Language Programming with C++ as a Second Language Week 2 Overview of C++ CSE/ICS 45C Patricia Lee, PhD Chapter 1 C++ Basics Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Introduction to

More information

Engineering Tools III: OOP in C++

Engineering Tools III: OOP in C++ Engineering Tools III: OOP in C++ Engineering Tools III: OOP in C++ Why C++? C++ as a powerful and ubiquitous tool for programming of numerical simulations super-computers (and other number-crunchers)

More information

Instantiation of Template class

Instantiation of Template class Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double

More information

Object-oriented Programming in C++

Object-oriented Programming in C++ Object-oriented Programming in C++ Working with C and C++ Wolfgang Eckhardt, Tobias Neckel March 23, 2015 Working with C and C++, March 23, 2015 1 Challenges of This Course Heterogeneity of the audience

More information

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

G52CPP C++ Programming Lecture 18. Dr Jason Atkin G52CPP C++ Programming Lecture 18 Dr Jason Atkin 1 Last lecture Operator Overloading Strings and streams 2 Operator overloading - what to know Know that you can change the meaning of operators Know that

More information

Programming, numerics and optimization

Programming, numerics and optimization Programming, numerics and optimization Lecture A-2: Programming basics II Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428 March

More information

Ch. 12: Operator Overloading

Ch. 12: Operator Overloading Ch. 12: Operator Overloading Operator overloading is just syntactic sugar, i.e. another way to make a function call: shift_left(42, 3); 42

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

CS 376b Computer Vision

CS 376b Computer Vision CS 376b Computer Vision 09 / 25 / 2014 Instructor: Michael Eckmann Today s Topics Questions? / Comments? Enhancing images / masks Cross correlation Convolution C++ Cross-correlation Cross-correlation involves

More information

Object-Oriented Programming

Object-Oriented Programming - oriented - iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 1 / 56 Overview - oriented 1 2 -oriented 3 4 5 6 7 8 Static and friend elements 9 Summary 2 / 56 I - oriented was initially created by Bjarne

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

More information

G52CPP C++ Programming Lecture 18

G52CPP C++ Programming Lecture 18 G52CPP C++ Programming Lecture 18 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Welcome Back 2 Last lecture Operator Overloading Strings and streams 3 Operator overloading - what to know

More information

Working with Batches of Data

Working with Batches of Data Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/fall_2012/csc1254.html 2 Abstract So far we looked at simple read a string print a string problems. Now we will look at more complex problems

More information

Pointers and References

Pointers and References Steven Zeil October 2, 2013 Contents 1 References 2 2 Pointers 8 21 Working with Pointers 8 211 Memory and C++ Programs 11 212 Allocating Data 15 22 Pointers Can Be Dangerous 17 3 The Secret World of Pointers

More information

Introduction to Programming using C++

Introduction to Programming using C++ Introduction to Programming using C++ Lecture One: Getting Started Carl Gwilliam gwilliam@hep.ph.liv.ac.uk http://hep.ph.liv.ac.uk/~gwilliam/cppcourse Course Prerequisites What you should already know

More information

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 3 - Functions

Chapter 3 - Functions Chapter 3 - Functions 1 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number Generation

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Variables. Data Types.

Variables. Data Types. Variables. Data Types. The usefulness of the "Hello World" programs shown in the previous section is quite questionable. We had to write several lines of code, compile them, and then execute the resulting

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms AUTO POINTER (AUTO_PTR) //Example showing a bad situation with naked pointers void MyFunction()

More information

C++ for Java Programmers

C++ for Java Programmers Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

04-19 Discussion Notes

04-19 Discussion Notes 04-19 Discussion Notes PIC 10B Spring 2018 1 Constructors and Destructors 1.1 Copy Constructor The copy constructor should copy data. However, it s not this simple, and we need to make a distinction here

More information

CS11 Introduction to C++ Fall Lecture 1

CS11 Introduction to C++ Fall Lecture 1 CS11 Introduction to C++ Fall 2006-2007 Lecture 1 Welcome! 8 Lectures (~1 hour) Slides posted on CS11 website http://www.cs.caltech.edu/courses/cs11 7 Lab Assignments on course website Available on Monday

More information

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak! //Example showing a bad situation with naked pointers CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms void MyFunction() MyClass* ptr( new

More information

AIMS Embedded Systems Programming MT 2017

AIMS Embedded Systems Programming MT 2017 AIMS Embedded Systems Programming MT 2017 Object-Oriented Programming with C++ Daniel Kroening University of Oxford, Computer Science Department Version 1.0, 2014 Outline Classes and Objects Constructors

More information

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan CSS 342 Data Structures, Algorithms, and Discrete Mathematics I Lecture 2 Yusuf Pisan Compiled helloworld yet? Overview C++ fundamentals Assignment-1 CSS Linux Cluster - submitting assignment Call by Value,

More information

Scientific Computing

Scientific Computing Scientific Computing Martin Lotz School of Mathematics The University of Manchester Lecture 1, September 22, 2014 Outline Course Overview Programming Basics The C++ Programming Language Outline Course

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 2. Overview of C++ Prof. Amr Goneid, AUC 1 Overview of C++ Prof. Amr Goneid, AUC 2 Overview of C++ Historical C++ Basics Some Library

More information

Chapter 2 - Control Structures

Chapter 2 - Control Structures Chapter 2 - Control Structures 1 Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures 2.5 if Selection Structure 2.6 if/else Selection Structure 2.7 while Repetition Structure

More information

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics PIC 10A Pointers, Arrays, and Dynamic Memory Allocation Ernest Ryu UCLA Mathematics Pointers A variable is stored somewhere in memory. The address-of operator & returns the memory address of the variable.

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

Introduction to Programming

Introduction to Programming Introduction to Programming session 6 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Spring 2011 These slides are created using Deitel s slides Sharif University of Technology Outlines

More information

Review&Preview 1/23/15, 4:08:07 PM 1. 3rd edition - standardized, and standard library allows programmer to start from a higher level

Review&Preview 1/23/15, 4:08:07 PM 1. 3rd edition - standardized, and standard library allows programmer to start from a higher level Review&Preview 1/23/15, 4:08:07 PM 1 Stroustrup: All four prefaces, Ch. 1. Then read "Tour" chapters 2, 3, 4 and 5 but skip 5.3 Concurrency. Watch for new C++11 usage. H: Using using. Stroustrup Introduction:

More information

Programming in C and C++

Programming in C and C++ Programming in C and C++ 1. Types Variables Expressions & Statements Dr. Anil Madhavapeddy University of Cambridge (based on previous years thanks to Alan Mycroft, Alastair Beresford and Andrew Moore)

More information

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory Announcements CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory There will be no lecture on Tuesday, Feb. 16. Prof. Thompson s office hours are canceled for Monday, Feb. 15. Prof.

More information

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved Chapter 1 C++ Basics Copyright 2010 Pearson Addison-Wesley. All rights reserved Learning Objectives Introduction to C++ Origins, Object-Oriented Programming, Terms Variables, Expressions, and Assignment

More information

EEE145 Computer Programming

EEE145 Computer Programming EEE145 Computer Programming Content of Topic 2 Extracted from cpp.gantep.edu.tr Topic 2 Dr. Ahmet BİNGÜL Department of Engineering Physics University of Gaziantep Modifications by Dr. Andrew BEDDALL Department

More information