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

Size: px
Start display at page:

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

Transcription

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

2 Outline 1 Introduction: C++ 2 Containers 3 Classes (STAT 598W) Lecture 8 2 / 47

3 Outline 1 Introduction: C++ 2 Containers 3 Classes (STAT 598W) Lecture 8 3 / 47

4 Variable types Besides the types we have learned from C, we have a new type: bool (Boolean). Example: bool test=false; Recall: short, int, long float, double, long double char (STAT 598W) Lecture 8 4 / 47

5 Defining Objects Object: region of memory that has a type. double salary, wage; int month, day, year; std::string address; Sales_item curr_book; Note that the last type is defined by a library (std) and the last variable is a structure. (STAT 598W) Lecture 8 5 / 47

6 Defining Constants Sometimes we want to define variables that cannot be modified during the execution time. We declare them as const: const int numsize=600; We have to initialize any constant variable when it is defined. Constant variables are local to the file in which it is defined. To make the constant variable accesible throughout the program we use extern declaration. (STAT 598W) Lecture 8 6 / 47

7 IO Library The usual C++ library of input/output tasks is iostream. It let us to write/read to/from a stream. (screen or files). The most common objects are: cout: ostream object that writes to the standard output. cin: istream object that reads the standard output. >>: read input from the istream object. <<: write ouput to an ostream object. (STAT 598W) Lecture 8 7 / 47

8 Example #include <iostream> using namespace std; int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; } (STAT 598W) Lecture 8 8 / 47

9 References Alternative name of an object. Example: int ival=124; int &refval=ival; All the operations on a reference are operations on the underlying object. Example: refval=+2; int ii=refval; adds 2 to ival and assigns the new value of ival to ii. The references can refer to a const object. The references are important as functions arguments. (STAT 598W) Lecture 8 9 / 47

10 References: Example unsigned long x = 4; void func1(unsigned long& val) { val = 5; } func1(x); //or: void func2(unsigned long* val) { *val = 5; } func2(&x); Which one is better: Pass by reference vs. Pass by value? (STAT 598W) Lecture 8 10 / 47

11 Pointers and references What is the difference between these two codes: int ival=1024, ival2=2048; int *pi=&ival, *pi2=&ival2; pi=pi2; int &ri=ival, &ri2=ival2; ri=ri2; (STAT 598W) Lecture 8 11 / 47

12 Function Arguments Recall that the C/C++ functions receive their arguments by value. Also it is possible to use pointers as arguments. We pass a const type variables (the function will make a copy that can t be changed within the procedure) Also we can use references: void swap(int &v1, int &v2){ int tmp=v2; v2=v1; v1=tmp; } In this case we call the function like swap(x,y). Important: it is safer to use references than pointers. (STAT 598W) Lecture 8 12 / 47

13 Function Arguments For large-sized objects it is expensive to copy an argument of a function. Then we can use a reference to a constant variable to solve this problem. bool isshorter(const string &s1, const string &s2){ return s1.size() < s2.size(); } (STAT 598W) Lecture 8 13 / 47

14 Outline 1 Introduction: C++ 2 Containers 3 Classes (STAT 598W) Lecture 8 14 / 47

15 Introduction STL: Standard Template Library. C++ library of container classes, algorithms and iterators. Container: object containing objects. (C++ template) Most important containers: Vector String Bitset In this course we are interested in quantitative applications, then Vector will be our first container. (STAT 598W) Lecture 8 15 / 47

16 Vector Collection of objects of a single type, each object has an associated integer index. You have to include an appropiate header: #include <vector> using namespace std; The Vector class is a class Template (we ll see the definition later). It can store different object types: strings, ints, doubles, classes. (STAT 598W) Lecture 8 16 / 47

17 Vector Definitions: vector<t> name; // vector that holds objects of type T vector<t> v2(v1); //v2 is a copy of v1 vector<t> v3(n,i); //v3 has n elements with value i vector<t> v4(n); //v4 has n values with an // initialized value. Examples: vector<int> ivec1; vector<int> ivec2(10,-1); vector<string> svec(10,"hi"); Remark: Usually is more efficient to define an empty vector and add elements to it. (STAT 598W) Lecture 8 17 / 47

18 Operations on vectors v.empty(): returns true if v is empty. v.size(): returns the number of elements in v. v.push_back(t): adds element with value t to end of v. v[n]: returns element at position n in v. v1=v2: replaces elements in v1 by a copy of elements in v2. v1==v2: returns true if v1 and v2 are equal.!=, <, <=, >, >=: normal meanings (componentwise). (STAT 598W) Lecture 8 18 / 47

19 Remarks: All vectors start at 0. An element must exist in order to subscript it. Elements are not added when we assign through a subscript. (STAT 598W) Lecture 8 19 / 47

20 Iterators We can use subscripts to examine a vector, but we can use an iterator defined by the library. Advantage: all the containers have iterators, but not all the containers support indexing. Syntax (example): vector<int>::iterator iter; Each container class defines its own iterator type that can be used to access elements in the container. (STAT 598W) Lecture 8 20 / 47

21 Iterators Each container has a begin operator indicating an iterator to the first element. Example: vector<int>::iterator iter=ivec.begin(); The containers also have an end operator pointing to the place next to the last element of the container. An iterator uses the operator * to access the element to which it refers. *iter=2000; iter++; *iter=2001; (STAT 598W) Lecture 8 21 / 47

22 Example for(vector<int>::iterator iter=ivec.begin(); iter!=ivec.end();iter++) *iter=0; If we don t want to change the values of a vector, we can use a const iterator (it is defined for any container): for(vector<int>::const_iterator iter=ivec.begin(); iter!=ivec.end();iter++) cout<< *iter << endl; The usual arithmetic on pointers apply to iterators. (STAT 598W) Lecture 8 22 / 47

23 Exercise (Bond Pricing) Assume that you want to compute the price of a bond (B 0 ) with Face Value=1 and coupon payments at discrete times t 1,..., t N. Then this price becomes: B 0 = N C ti e rt i i=1 if a continuously compounded interest rate r is assumed. Solve this problem using vector containers. (STAT 598W) Lecture 8 23 / 47

24 Yield to maturity Given the market price B 0, we want to find the interest rate y (Yield to Maturity) such that: B 0 = N C ti e yt i i=1 Compute this rate using Bisection method. Assume that your cash flow produces a unique YTM. (STAT 598W) Lecture 8 24 / 47

25 Duration Compute the duration D and Macaulay D M as follows: N D = 1 B 0 D M = 1 B 0 i=1 N i=1 t i C ti e rt i t i C i e yt i and also the convexity Cv: N Cv = 1 B 0 i=1 t 2 i C i e yt i (STAT 598W) Lecture 8 25 / 47

26 Outline 1 Introduction: C++ 2 Containers 3 Classes (STAT 598W) Lecture 8 26 / 47

27 Class components We can distinguish two components on each class: Syntax: Implementation: private members that define the data and any function that is not intended for general use. Interface: all the functions defined on the public part of the class. class name_class{ public: //data members and member functions private: //data members and member functions }; (STAT 598W) Lecture 8 27 / 47

28 Remarks We cannot initialize the members of a class as part of their definition. The initialization process is done using Constructors. The public section of the class defines members that can be accessed by any part of the program. The private section contains members that can t be manipulated by the final user. (STAT 598W) Lecture 8 28 / 47

29 Example We will create a C++ class containing information about European options. (Duffy 2004) The class will contain the following member data: Stock price Volatility Strike price Time to expiry Risk-free interest rate Option market price. And it will contain some of the functions we have computed before: BS Price Greeks Implied volatility (STAT 598W) Lecture 8 29 / 47

30 Exercise class EuropeanOption{ private: double r; // Interest rate double sigma; // volatility double K; //strike price double T; //Maturity double S; //Stock price double C0; //option m. price public:... } (STAT 598W) Lecture 8 30 / 47

31 Class member functions The member functions have the usual components: return type, name, parameters and function body. The function prototype must be defined within the class. The function body can be defined outside. A member function which is defined inside the class is implicitly treated as inline function. (function which is expanded in line during compilation) (see (STAT 598W) Lecture 8 31 / 47

32 Class member functions Example: bool same_optionprice(const EuropeanOption &comp) const {return C0==comp.C0; } Note that the compiler re-writes a call from the previous function as: EuropeanOption eur1(); \\later we ll define the proper constructor eur1.same_optionprice(comp); \\real code EuropeanOption::same_optionprice(&eur1, comp); \\pseudo-code The hidden parameter &eur1 is called this. The parameter this is a pointer to the class. (STAT 598W) Lecture 8 32 / 47

33 Class member functions The const keyword (after the function definition) modifies the type of the this pointer. Then the class itself can t be changed when its member functions are used. The pointer this can be used to refer its members, but it is unnecessary. Example this->c0 = C0. The member functions can be defined outside the class, but you have to indicate that they belong to the class. (STAT 598W) Lecture 8 33 / 47

34 Class member functions Example: double EuropeanOption::Price() const { if(opttype=="c"){ return CallPrice(); } else return PutPrice(); } Recall :: is the scope operator. (STAT 598W) Lecture 8 34 / 47

35 Constructor The constructor function has (or have) the same name as its class, and it cannot return a type. Default constructor: vector<int> v1; //default constructor: empty vector string s; //default constructor: empty string Then we want to initialize an empty European option class. (STAT 598W) Lecture 8 35 / 47

36 Constructor Default constructor: EuropeanOption::EuropeanOption() { r=0; sigma=0;... } and it is declared in the class as follows (without the const keyword): public: EuropeanOption(); (STAT 598W) Lecture 8 36 / 47

37 Constructor Also you can use this alternative notation (inside the class definition). This definition is preferred: EuropeanOption(): r(0), sigma(0),... {} If you don t define any constructor, the compiler will do it for you using default values for each class member. The class declaration should be saved in a.h file. The member functions should be saved in a separate.cpp file. (STAT 598W) Lecture 8 37 / 47

38 Function Overloading Two functions that appear in the same scope are overloaded if they have the same name but they have different arguments. It s not the same to redeclare a function than to overload a function. Example: class termstructure(){ private: vector<double> rates; vector<double> time; public: termstructure(); double meanrate() const; double meanrate(const termstructure &); } (STAT 598W) Lecture 8 38 / 47

39 Function Overloading We can call the overloaded function as usual: termstructure t1,t2; double v1=t1.meanrate(); double v2=t1.meanrate(t1); Note that the only difference between both functions is the argument list. Remark: Since we will be using our class more than once, we don t want multiple definitions on the same file. We can use: #ifndef EUROPEANOPT_H #define EUROPEANOPT_H //Definition of the class #endif (STAT 598W) Lecture 8 39 / 47

40 More about Constructors The constructors can be overloaded. The only restriction is that every argument list is unique. Example: EuropeanOption(); EuropeanOption(const vector<double>& ); The constructors are executed automatically when a class is defined. EuropeanOption s1; Europeanoption s1(v1); If a class has only public members it can be initialized like any C structure: Data val1={1000, 2000, "My_name"}; (STAT 598W) Lecture 8 40 / 47

41 Copy Constructor Constructor that takes an object of the same class as parameter. C++ has two types of initialization: Direct initialization: it uses (). It directly invokes the proper constructor. Example: EuropeanOption option1; Europeanoption option2(20,50,0.01,0.3,1.23); Copy initialization: it uses =. It calls the proper constructor and it passes the temporarly object to the copy-constructor. Example (it only applies to single-argument classes): dim v1=20; EuropeanOption option1=v1; // here we are using the constructor: // EuropeanOption(const double &); (STAT 598W) Lecture 8 41 / 47

42 Copy Constructor The general syntax of a copy-constructor is: Class_name(const class_name &); If you don t define the copy-constructor, the compiler will define one for you using the copy-constructors for each member s class. (STAT 598W) Lecture 8 42 / 47

43 Destructor We need to define an operation that releases the memory allocated on each class we have constructed. Suppose we have this situation: EuropeanOption *p1= new Europeanoption; *p1(preopt); EuropeanOption p2(*p1); delete p1; the last step calls the destructor of our class. (STAT 598W) Lecture 8 43 / 47

44 Destructor The destructors are important when you allocate memory during the class operation. The compiler defines a default destructor. General syntax: ~Name_class() {...code... } Usually the destructor: (1) is defined as a virtual function and (2) the function is empty. (STAT 598W) Lecture 8 44 / 47

45 Example 1 Construct the class EuropeanOption with the following components: Data members. (S, K, r, T, σ and type of Option). 2 constructors: Default and user-defined. Copy constructor. Destructor Functions that computes the Option price, Delta and Vega. (STAT 598W) Lecture 8 45 / 47

46 Example 2 Objective: Construct a class of term structures (interest rates) with basic operations. Assume we have a term structure composed by continuously compounded rates: r 1,..., r n and times: t 1,..., t n. For any intermediate value we will use linear interpolation between the closest adjacent nodes. Define the discount factor and forward interest rate as: d t = e rtt f t1,t 2 = r t2 t 2 t 2 t 1 r t1 t 1 t 2 t 1 Include the usual constructors (2 at least), copy-constructor and destructor. (STAT 598W) Lecture 8 46 / 47

47 Example 3 Objective: Construct a class of Bonds with a corresponding term structure. Assume we have a market Bond price (B 0 ), a term structure K, coupons C 1,..., C m and payment times t 1,..., t m. Compute the YTM solving with the bisection method: B 0 = m C ti e yt i i=1 Compute the theoretical price B 0, the duration D, duration with YTM D y, convexity Cx and convexity with YTM Cx y. Use linear interpolation in your calculations. Include the usual constructors (2 at least), copy-constructor and destructor. (STAT 598W) Lecture 8 47 / 47

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

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is

More information

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Review: C++ Basic Concepts. Dr. Yingwu Zhu Review: C++ Basic Concepts Dr. Yingwu Zhu Outline C++ class declaration Constructor Overloading functions Overloading operators Destructor Redundant declaration A Real-World Example Question #1: How to

More information

pointers & references

pointers & references pointers & references 1-22-2013 Inline Functions References & Pointers Arrays & Vectors HW#1 posted due: today Quiz Thursday, 1/24 // point.h #ifndef POINT_H_ #define POINT_H_ #include using

More information

Topics. bool and string types input/output library functions comments memory allocation templates classes

Topics. bool and string types input/output library functions comments memory allocation templates classes C++ Primer C++ is a major extension of c. It is similar to Java. The lectures in this course use pseudo-code (not C++). The textbook contains C++. The labs involve C++ programming. This lecture covers

More information

III. Classes (Chap. 3)

III. Classes (Chap. 3) III. Classes III-1 III. Classes (Chap. 3) As we have seen, C++ data types can be classified as: Fundamental (or simple or scalar): A data object of one of these types is a single object. int, double, char,

More information

Chapter 10 Introduction to Classes

Chapter 10 Introduction to Classes C++ for Engineers and Scientists Third Edition Chapter 10 Introduction to Classes CSc 10200! Introduction to Computing Lecture 20-21 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this

More information

Lecture 7. Log into Linux New documents posted to course webpage

Lecture 7. Log into Linux New documents posted to course webpage Lecture 7 Log into Linux New documents posted to course webpage Coding style guideline; part of project grade is following this Homework 4, due on Monday; this is a written assignment Project 1, due next

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

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table

More information

C++ (Non for C Programmer) (BT307) 40 Hours

C++ (Non for C Programmer) (BT307) 40 Hours C++ (Non for C Programmer) (BT307) 40 Hours Overview C++ is undoubtedly one of the most widely used programming language for implementing object-oriented systems. The C++ language is based on the popular

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

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

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1 Lecture 12 Log into Linux. Copy files on csserver in /home/hwang/cs215/lecture12/*.* Reminder: Practical Exam 1 is Wednesday 3pm-5pm in KC-267. Questions about Project 2 or Homework 6? Submission system

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

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

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms STL: C++ Standard Library Standard Template Library (STL) Encapsulates complex data structures and algorithms is a library of generic container classes which are both efficient and functional C++ STL developed

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

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

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

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

UEE1302 (1102) F10: Introduction to Computers and Programming

UEE1302 (1102) F10: Introduction to Computers and Programming Computational Intelligence on Automation Lab @ NCTU Learning Objectives UEE1302 (1102) F10: Introduction to Computers and Programming Programming Lecture 00 Programming by Example Introduction to C++ Origins,

More information

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF

More information

Operator Overloading in C++ Systems Programming

Operator Overloading in C++ Systems Programming Operator Overloading in C++ Systems Programming Operator Overloading Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. Global Functions Overloading

More information

COEN244: Class & function templates

COEN244: Class & function templates COEN244: Class & function templates Aishy Amer Electrical & Computer Engineering Templates Function Templates Class Templates Outline Templates and inheritance Introduction to C++ Standard Template Library

More information

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OPERATOR OVERLOADING KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Dynamic Memory Management

More information

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

G52CPP C++ Programming Lecture 14. Dr Jason Atkin G52CPP C++ Programming Lecture 14 Dr Jason Atkin 1 Last Lecture Automatically created methods: A default constructor so that objects can be created without defining a constructor A copy constructor used

More information

Lecture 18 Tao Wang 1

Lecture 18 Tao Wang 1 Lecture 18 Tao Wang 1 Abstract Data Types in C++ (Classes) A procedural program consists of one or more algorithms that have been written in computerreadable language Input and display of program output

More information

W3101: Programming Languages C++ Ramana Isukapalli

W3101: Programming Languages C++ Ramana Isukapalli Lecture-6 Operator overloading Namespaces Standard template library vector List Map Set Casting in C++ Operator Overloading Operator overloading On two objects of the same class, can we perform typical

More information

Black Scholes Pricing Model European Option on Dividend paying Stock

Black Scholes Pricing Model European Option on Dividend paying Stock Black Scholes Pricing Model European Option on Dividend paying Stock This is a C++ implementation of the famous Black Scholes options pricing model for a European option on a dividend paying stock. The

More information

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that Reference Parameters There are two ways to pass arguments to functions: pass-by-value and pass-by-reference. pass-by-value A copy of the argument s value is made and passed to the called function. Changes

More information

Come and join us at WebLyceum

Come and join us at WebLyceum Come and join us at WebLyceum For Past Papers, Quiz, Assignments, GDBs, Video Lectures etc Go to http://www.weblyceum.com and click Register In Case of any Problem Contact Administrators Rana Muhammad

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

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

C++ For Science and Engineering Lecture 15

C++ For Science and Engineering Lecture 15 C++ For Science and Engineering Lecture 15 John Chrispell Tulane University Wednesday September 29, 2010 Function Review Recall the basics you already know about functions. Provide a function definition.

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

Object-Oriented Design (OOD) and C++

Object-Oriented Design (OOD) and C++ Chapter 2 Object-Oriented Design (OOD) and C++ At a Glance Instructor s Manual Table of Contents Chapter Overview Chapter Objectives Instructor Notes Quick Quizzes Discussion Questions Projects to Assign

More information

STL: C++ Standard Library

STL: C++ Standard Library STL: C++ Standard Library Encapsulates complex data structures and algorithms CSC 330 OO Software Design 1 We ve emphasized the importance of software reuse. Recognizing that many data structures and algorithms

More information

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2 CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2 Review of Lecture 14 Maps are associations between keys and values. Maps have fast insert, access and remove operations:

More information

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington CSE 333 Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington Administrivia New exercise posted yesterday afternoon, due Monday morning - Read a directory

More information

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine Homework 5 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 13, 2013 Question 1 Consider the following Java definition of a mutable string class. class MutableString private char[] chars

More information

SFU CMPT Topic: Classes

SFU CMPT Topic: Classes SFU CMPT-212 2008-1 1 Topic: Classes SFU CMPT-212 2008-1 Topic: Classes Ján Maňuch E-mail: jmanuch@sfu.ca Friday 15 th February, 2008 SFU CMPT-212 2008-1 2 Topic: Classes Encapsulation Using global variables

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

Cpt S 122 Data Structures. Introduction to C++ Part II

Cpt S 122 Data Structures. Introduction to C++ Part II Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function

More information

7.1 Optional Parameters

7.1 Optional Parameters Chapter 7: C++ Bells and Whistles A number of C++ features are introduced in this chapter: default parameters, const class members, and operator extensions. 7.1 Optional Parameters Purpose and Rules. Default

More information

CSC1322 Object-Oriented Programming Concepts

CSC1322 Object-Oriented Programming Concepts CSC1322 Object-Oriented Programming Concepts Instructor: Yukong Zhang February 18, 2016 Fundamental Concepts: The following is a summary of the fundamental concepts of object-oriented programming in C++.

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

Programming C++ Lecture 6. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Programming C++ Lecture 6. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor Programming C++ Lecture 6 Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor Jennifer.sartor@elis.ugent.be S Friends 2 Friends of Objects S Classes sometimes need friends. S Friends are defined outside

More information

Module Operator Overloading and Type Conversion. Table of Contents

Module Operator Overloading and Type Conversion. Table of Contents 1 Module - 33 Operator Overloading and Type Conversion Table of Contents 1. Introduction 2. Operator Overloading 3. this pointer 4. Overloading Unary Operators 5. Overloading Binary Operators 6. Overloading

More information

Practice Problems CS2620 Advanced Programming, Spring 2003

Practice Problems CS2620 Advanced Programming, Spring 2003 Practice Problems CS2620 Advanced Programming, Spring 2003 April 9, 2003 1. Explain the results of each vector definition: string pals[] = "pooh", "tigger", "piglet", "eeyore", "kanga" a) vector

More information

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor Programming C++ Lecture 3 Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor Jennifer.sartor@elis.ugent.be S Inheritance S Software reuse inherit a class s data and behaviors and enhance with new capabilities.

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

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University (5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University Key Concepts 2 Object-Oriented Design Object-Oriented Programming

More information

C++ basics Getting started with, and Data Types.

C++ basics Getting started with, and Data Types. C++ basics Getting started with, and Data Types pm_jat@daiict.ac.in Recap Last Lecture We talked about Variables - Variables, their binding to type, storage etc., Categorization based on storage binding

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

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

Implementing an ADT with a Class

Implementing an ADT with a Class Implementing an ADT with a Class the header file contains the class definition the source code file normally contains the class s method definitions when using Visual C++ 2012, the source code and the

More information

Lab 2: ADT Design & Implementation

Lab 2: ADT Design & Implementation Lab 2: ADT Design & Implementation By Dr. Yingwu Zhu, Seattle University 1. Goals In this lab, you are required to use a dynamic array to design and implement an ADT SortedList that maintains a sorted

More information

Advanced Systems Programming

Advanced Systems Programming Advanced Systems Programming Introduction to C++ Martin Küttler September 19, 2017 1 / 18 About this presentation This presentation is not about learning programming or every C++ feature. It is a short

More information

Templates and Vectors

Templates and Vectors Templates and Vectors 1 Generic Programming function templates class templates 2 the STL vector class a vector of strings enumerating elements with an iterator inserting and erasing 3 Writing our own vector

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 7 September 21, 2016 CPSC 427, Lecture 7 1/21 Brackets Example (continued) Storage Management CPSC 427, Lecture 7 2/21 Brackets Example

More information

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles Abstract Data Types (ADTs) CS 247: Software Engineering Principles ADT Design An abstract data type (ADT) is a user-defined type that bundles together: the range of values that variables of that type can

More information

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year Object Oriented Programming Assistant Lecture Omar Al Khayat 2 nd Year Syllabus Overview of C++ Program Principles of object oriented programming including classes Introduction to Object-Oriented Paradigm:Structures

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

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

Object Oriented Design

Object Oriented Design Object Oriented Design Chapter 6 Example Activity Diagram 1 Outline Chapter 6 Topics 6.6 C++ Standard Library Header Files 6.14 Inline Functions 6.16 Default Arguments 6.17 Unary Scope Resolution Operator

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

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14. Lecture 14 1 Exceptions Iterators Random numbers Casting Enumerations Pairs The Big Three Outline 2 Error Handling It is often easier to write a program by first assuming that nothing incorrect will happen

More information

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CHAPTER 4 FUNCTIONS. 4.1 Introduction CHAPTER 4 FUNCTIONS 4.1 Introduction Functions are the building blocks of C++ programs. Functions are also the executable segments in a program. The starting point for the execution of a program is main

More information

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture: CISC 2200 Data Structure Fall, 2016 C++ Review:3/3 1 From last lecture: pointer type and pointer variable (stores memory addresses of a variable (of any type, local or global, automatic/static/dynamic)

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

7 TEMPLATES AND STL. 7.1 Function Templates

7 TEMPLATES AND STL. 7.1 Function Templates 7 templates and STL:: Function Templates 7 TEMPLATES AND STL 7.1 Function Templates Support generic programming functions have parameterized types (can have other parameters as well) functions are instantiated

More information

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p. 9 Self-Test Exercises p. 11 History Note p. 12 Programming and

More information

CS 247: Software Engineering Principles. ADT Design

CS 247: Software Engineering Principles. ADT Design CS 247: Software Engineering Principles ADT Design Readings: Eckel, Vol. 1 Ch. 7 Function Overloading & Default Arguments Ch. 12 Operator Overloading U Waterloo CS247 (Spring 2017) p.1/17 Abstract Data

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

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016 Chapter 6: User-Defined Functions Objectives In this chapter, you will: Learn about standard (predefined) functions Learn about user-defined functions Examine value-returning functions Construct and use

More information

Module 9. Templates & STL

Module 9. Templates & STL Module 9 Templates & STL Objectives In this module Learn about templates Construct function templates and class templates STL 2 Introduction Templates: enable you to write generic code for related functions

More information

ADTs & Classes. An introduction

ADTs & Classes. An introduction ADTs & Classes An introduction Quick review of OOP Object: combination of: data structures (describe object attributes) functions (describe object behaviors) Class: C++ mechanism used to represent an object

More information

CIS 190: C/C++ Programming. Classes in C++

CIS 190: C/C++ Programming. Classes in C++ CIS 190: C/C++ Programming Classes in C++ Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors Headers in C++ done same way as in C including user.h files:

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

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation Review from Lectures 7 Algorithm Analysis, Formal Definition of Order Notation Simple recursion, Visualization

More information

CS 211 Winter 2004 Sample Final Exam (Solutions)

CS 211 Winter 2004 Sample Final Exam (Solutions) CS 211 Winter 2004 Sample Final Exam (Solutions) Instructor: Brian Dennis, Assistant Professor TAs: Tom Lechner, Rachel Goldsborough, Bin Lin March 16, 2004 Your Name: There are 6 problems on this exam.

More information

Lecture on pointers, references, and arrays and vectors

Lecture on pointers, references, and arrays and vectors Lecture on pointers, references, and arrays and vectors pointers for example, check out: http://www.programiz.com/cpp-programming/pointers [the following text is an excerpt of this website] #include

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

Exercise 6.2 A generic container class

Exercise 6.2 A generic container class Exercise 6.2 A generic container class The goal of this exercise is to write a class Array that mimics the behavior of a C++ array, but provides more intelligent memory management a) Start with the input

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Lecture 3: Introduction to C++ (Continue) Examples using declarations that eliminate the need to repeat the std:: prefix 1 Examples using namespace std; enables a program to use

More information

CPSC 427a: Object-Oriented Programming

CPSC 427a: Object-Oriented Programming CPSC 427a: Object-Oriented Programming Michael J. Fischer Lecture 17 November 1, 2011 CPSC 427a, Lecture 17 1/21 CPSC 427a, Lecture 17 2/21 CPSC 427a, Lecture 17 3/21 A bit of history C++ standardization.

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College November 22, 2017 Outline Outline 1 Chapter 12: C++ Templates Outline Chapter 12: C++ Templates 1 Chapter 12: C++ Templates

More information

CS197c: Programming in C++

CS197c: Programming in C++ CS197c: Programming in C++ Lecture 2 Marc Cartright http://ciir.cs.umass.edu/~irmarc/cs197c/index.html Administration HW1 will be up this afternoon Written assignment Due in class next week See website

More information

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures C++ Review 1 Purpose of Review Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures 2 Class The Class defines the data structure

More information

Evolution of Programming Languages

Evolution of Programming Languages Evolution of Programming Languages 40's machine level raw binary 50's assembly language names for instructions and addresses very specific to each machine 60's high-level languages: Fortran, Cobol, Algol,

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

Come and join us at WebLyceum

Come and join us at WebLyceum Come and join us at WebLyceum For Past Papers, Quiz, Assignments, GDBs, Video Lectures etc Go to http://www.weblyceum.com and click Register In Case of any Problem Contact Administrators Rana Muhammad

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

2 nd Week Lecture Notes

2 nd Week Lecture Notes 2 nd Week Lecture Notes Scope of variables All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous

More information

Your first C++ program

Your first C++ program Your first C++ program #include using namespace std; int main () cout

More information

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor Outline EDAF50 C++ Programming 4. Classes Sven Gestegård Robertz Computer Science, LTH 2018 1 Classes the pointer this const for objects and members Copying objects friend inline 4. Classes 2/1 User-dened

More information

Where do we go from here?

Where do we go from here? Where do we go from here? C++ classes and objects, with all the moving parts visible operator overloading templates, STL, standards, Java components, collections, generics language and performance comparisons

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