A<int,60> array[100]; 100*60*sizeof(int)

Size: px
Start display at page:

Download "A<int,60> array[100]; 100*60*sizeof(int)"

Transcription

1 \ Y 1 Q Y Y 1 Template! "# & ')4,65')(754+8$')901:$;1$,<9=' $%!& ')( *+,)-0/ 132+'% Template file Ah: <class T1, class T2 T1 _d; T2 _c; ; A<double, int a; 1+@-,<P=$ & ')4D,65')(7G H/$,I& ')9JLM&-&,)% =$;N<2 ')* file Ah: <class T, int size T _array[size]; $ %?& ')(@*+,)-AB/ 1C2D+E'% F A<double,70 a1; A<double,100 a2; &ER5J, WRX $%7S=LL+8$R')90+@1C( G@')'<FT,)/8$R'<FU%EFV&ER5 & ')'+R5M% 5'I$ *1 Y double 18, 1:'1:' $% [ XRX $%ZSJLJ+8$')90+ 13( G@')'<FT,)/8$R'<FU%EF 70*sizeof(double) a1 sizeof double 1'1:' $% 100*sizeof(double) a2 sizeof 1/:FT181:B,)%1C2/CFU,<2 9J*I$%+$;&/ A<int,60 array[100]; $ %Z2D,)/E')=G@$ 2+EB,)-& ')( G ')'<FU,)/8$%7S=LL+82 =')^0' & ')2F S=J+ 13$]M,<^0G,<' 100*60*sizeof(int) 2@'+E*)')PH1I^0GD1 FV& '<^=FU+7,<*)')'18&/ & ')J(+L5M_L')4D,)1$;N<2@')*:/$`1:$/82D,)/E')JG $ A,<$RP0,)-1:'1:':1+ 1:*,<=a@/@1CP0,<GD1C2@0,)%EFVN<')')^0$;1:'1C0%E5L/8'/ default S=J+8$% ctor <typename T, T val class A { void foo() { std::cout << val << std::endl; ; A<int,7 a; 1+ -,<PJ$ & P0,<G7( *+@,)-A0/84D,65')(+82D,<'1:$b$9='TS=L;(@*+,)-AB/ 1

2 ) ; E ; ; ; G E A<char,'h' b; // A<char,78 c; // compilation error afoo(); bfoo(); 7 h 4 1 ' ) =!0 <!$# 8 8* &:,8 1 default file Ah: <class T = double, int size = 50 T _array[size]; ; A< d; // similar to: A<double,50 A<int h; // similar to: A<int,50 A<float,70 z; % 0/*1 )2 & 354!6'! * &:,8 1 3 default "!$# &%('*),+default 3 %('*=),+" 4 + A 4! C4!</ '( &4 B 8@'D) 1 1 B 3 file Ah: Type _data; A(Type data) : _data(data) { bool isbegger(type data); ; specialization - +0) / 4 -?8@) ) 1 A(1B 4 ' # <class Type bool A<Type::isBegger(Type data) { return data _data; #include Ah A<char* a( hi ); std::cout << aisbigger( bye ); 3LJ!$# 3DJ!# %%() ) 3@) -H-I),8 3D+0) / % % 0! %-F%!"/* %(' E _data 86) M)!"A '*%N86), data A 8@'D4@%-F: )!<" M -8@) isbigger() '*) / '*%P 3L86)2-Q),8 4@%%R 4 1 8@3@' 73S4 "!$# %! 7T@' hi! 8@) %R ),+0=(: %('D%*M B/ ),+$-?8@)Q%R %P8@)2-Q),8 %P8 %() ) 3*8 % 0/*1 )2 "% E : -8 U! =) /* T / &% ) ) 3@%V% %R863S) 1 0! 8!<A %R8 %P+0) / %P8U- isbigger() 8 -F) 1 73S% 1 ) ) %(3 bye!< -?T 2

3 & 4 H : H : H H! "! #$ #%'&()!* + ",- % file Ah (/0 123/ ): bool A<char*::isBigger(char *data) { return strcmp(data, _data) 0; 4 56&7 %8 99& A<int ai(5); A<char* as( hi ); aiisbigger(7); // generic isbigger() asisbigger( bye ); // specific isbigger() specialization #include <stringh char* %;<! = % #%+%; =!8 I #+J +%; CB8# " &(,- %$B!%DBEFG$ 8 % 9@ $ -#$CB A $ & # 9 %;<I#@ FG, ;F ; specialization matching N< ÖLBA%8 " A<String specialization FG ";"L +7 M ;$L B# L% %R 9 ;S -! # HQP global function E,*E 9"E'&7 EF+ %; ;? ;9 A &-@ operator << "99 L - %N; SLB88&( ; %6 9,& +%; <class Type void foo(type d) { H H foo(5); // the function foo(int) will be expand and called foo(55); // the function foo(double) will be expand and called foo(21); // the function foo(double) will be called but will not be expand again int i = 5; foo(&i); // the function foo(int*) will be expand and called (Type = int* ) 88@A E!VB &+7$ #,FGLF # ;F; <class Type void foo(type* p) { int d; foo(&d); // foo(int *) will be expand and called (Type = int) ;F ; L $ CB! $L"99 9@('+%; #* " Type 9F, U&-@ - $ 8@ M!VB &+7$ +%; #* NCB8#<& & '' #%& B &-+7 W!! XB &-+7 +%; 9 % &Y999 ;F; 3

4 A #include "LinkedListh" <typename T void foo1(linkedlist<t* list) { <typename T void foo2(linkedlist<linkedlist<t* list) { LinkedList<int* list; LinkedList<LinkedList<int* ll; foo1(list); // T = int foo1(ll); // T = LinkedList<int foo2(ll);; // T = int! " # $! '!*" &, - / & 0 & ) 12 3#! % '&(!)*") & % ) 2 2")55 &4! "* & &4 <class T1, class T2 void foo(t1 t) { T2 data; foo(5); // T2 =?? "! foo() <class T1, class T2 void foo(t2 t2, T1 t1) { T2 data; #! &(!)*") & 1 T2 foo()! "/, & / 0 9 &4:!*<, & - ; / " & T2 ") & & 2-!! %?,, &( #!) & & : = )9 & - compile-time polymorphism Templates 9 = =7 polymorphism -5< B "! $,=C D- / D & :! )!3?, & %# 2 0 1#! /!C# 2 : /" # / 7 D 0 -# 2# & 78/, & & : C- 9) /,=C; - polymorphism s, : E7? =, : E7 D0 #include <iostream <typename T void foo(t t) { tprint(); // compile time polymorphism polymorphism 4

5 ; void print() { std::cout << "I'm a happy instance of A" << std::endl; class B { void print() { std::cout << "I'm a sad instance of B" << std::endl; ; A a; B b; foo(a); foo(b); #include <iostream run-time-polymorphism s class DataStructure { virtual ostream& tostream(ostream& os) const = 0; ; ostream& operator << (ostream& os, const DataStructure& ds) { return dstostream(os); <typename T class List :public DataStructure { virtual ostream& tostream(ostream& os) const { return os<<"i'm a list of " << T::getType() << std::endl; ; <typename T class Tree : public DataStructure { virtual ostream& tostream(ostream& os) const { return os<<"i'm a tree of " << T::getType() << std::endl; ; static char* gettype() {return "A"; 5

6 N & 3 ; class B { static char* gettype() {return "B"; ; Tree<A ta; Tree<B tb; List<A la; List<B lb; std::cout << ta << tb << la << lb; return 0; '( ) *" +!"#"$% #include <iostream #include "LinkedListh" A BC19*DE2/ 13E249 compile-time run-time, DataStructure -/ #89;:2/<*=?624@ # G #!%)HI6J *"%L%F %I ' ) M virtual void print(ostream& os) const =0; ; ostream& operator << (ostream& os, const A* a) { a-print(os); return os; class B : public A { virtual void print(ostream& os) const { os << "B print"; ; class C : public A { virtual void print(ostream& os) const { os << "C print"; ; B b1,b2; C c1,c2; LinkedList<A* list; listinsert(&b1); listinsert(&c1); listinsert(&c2); listinsert(&b2); std::cout << list << std::endl; %F ' 6

7 ( ( - B print - C print - C print - B print ) *! * + *,- )! "! #%$ & ' * $ /10* :5;"<"//4=*76)=?<A@5B/C0D243"E"/27F 25A24=!@G)H%/10*243AE"F /C0I731)65"@37J A L " M" N! L + ' * '# #include <iostream #include <stdlibh <typename T class Less { bool operator() (const T& t1, const T& t2) const { return t1 < t2; ; <typename T class More { bool operator() (const T& t1, const T& t2) const { return t1 t2; ; class DigitLess { int digitsum(const int& t) const { return t-9*(t/10); // good only for two digits number bool operator() (const int& t1, const int& t2) const { return digitsum(t1) < digitsum(t2); ; <typename T void swap(t& t1, T& t2) { T tmp(t1); t1 = t2; t2 = tmp; <typename DS, typename CMP void sort(ds& ds, const CMP& cmp) { for(int i=0; i<dssize(); i++) for(int j=i+1; j<dssize(); j++) if(cmp(ds[j],ds[i])) swap(ds[j],ds[i]); 7

8 <typename T, int SIZE class Array { T _arr[size]; const T& operator[](int i) const { return _arr[i]; T& operator[](int i) { return _arr[i]; int size() {return SIZE; ; <typename T, int SIZE ostream& operator << (ostream& os, const Array<T,SIZE& arr) { for(int i=0; i<size; i++) os << ' ' << arr[i]; return os << ' '; Array<int,20 ar1; Array<double,20 ar2; for(int i=0; i<20; i++) ar2[i] = ar1[i] = i; sort(ar1,less<int()); sort(ar2,more<double()); std::cout << ar1 << " " << std::endl << ar2 << std::endl; sort(ar1,digitless()); std::cout << ar1 << std::endl;

Before we dive in. Preprocessing Compilation Linkage

Before we dive in. Preprocessing Compilation Linkage Templates Before we dive in Preprocessing Compilation Linkage 2 Motivation A useful routine to have is void swap( int& a, int &b ) int tmp = a; a = b; b = tmp; 3 Example What happens if we want to swap

More information

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

COSC 320 Exam 2 Key Spring Part 1: Hash Functions COSC 320 Exam 2 Key Spring 2011 Part 1: Hash s 1. (5 Points) Create the templated function object lessthan, you may assume that the templated data type T has overloaded the < operator. template

More information

Programming in C++: Assignment Week 8

Programming in C++: Assignment Week 8 Programming in C++: Assignment Week 8 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur 721302 partha.p.das@gmail.com April 12,

More information

ECE Fall 2017, Third Exam

ECE Fall 2017, Third Exam ECE 30862 Fall 2017, Third Exam DO NOT START WORKING ON THIS UNTIL TOLD TO DO SO. LEAVE IT ON THE DESK. THE LAST PAGE IS THE ANSWER SHEET. TEAR IT OFF AND PUT ALL ANSWERS THERE. TURN IN BOTH PARTS OF THE

More information

TDDD38 - Advanced programming in C++

TDDD38 - Advanced programming in C++ TDDD38 - Advanced programming in C++ Templates III Christoffer Holm Department of Computer and information science 1 Dependent Names 2 More on Templates 3 SFINAE 1 Dependent Names 2 More on Templates 3

More information

Bell Bjarne Stroustrup C C with classes Stroustrup C C with classes

Bell Bjarne Stroustrup C C with classes Stroustrup C C with classes C++ C++ BellBjarne Stroustrup C C with classes C with classes Stroustrup C C with classes C C++C++ ++CC CC+1 CC++ CC++ C++CCC++ C++ CC++ structc C++ C++ templateso.o C++compile-time-polymorphism const

More information

ECE Fall 2018, Test 3

ECE Fall 2018, Test 3 1 ECE 30862 Fall 2018, Test 3 DO NOT START WORKING ON THIS UNTIL TOLD TO DO SO. LEAVE IT ON THE DESK. THE LAST PAGE IS THE ANSWER SHEET. TEAR IT OFF AND PUT ALL ANSWERS THERE. TURN IN BOTH PARTS OF THE

More information

ECE 462 Fall 2011, Second Exam

ECE 462 Fall 2011, Second Exam ECE 462 Fall 2011, Second Exam DO NOT START WORKING ON THIS UNTIL TOLD TO DO SO. You have until 9:20 to take this exam. Your exam should have 10 pages total (including this cover sheet). Please let Prof.

More information

Programming in C++: Assignment Week 1

Programming in C++: Assignment Week 1 Programming in C++: Assignment Week 1 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur 721302 partha.p.das@gmail.com February 24,

More information

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; };

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; }; struct Buffer { Buffer(int s) { buf = new char[s]; ~Buffer() { delete [] buf; char *buf; ; struct FBuffer : public Buffer { FBuffer(int s) : Buffer(s) { f = fopen("file", "w"); ~FBuffer() { fclose(f);

More information

C++ Pub Quiz. A 90 minute quiz session ACCU April by Olve Maudal, with Lars Gullik Bjønnes. Sponsored by:

C++ Pub Quiz. A 90 minute quiz session ACCU April by Olve Maudal, with Lars Gullik Bjønnes. Sponsored by: C++ Pub Quiz by Olve Maudal, with Lars Gullik Bjønnes + + Sponsored by: A 90 minute quiz session ACCU April 2013 Here is my development environment: (Mac OS 10.8.2, x86_64) bash-3.2$ myc++ -v Using built-in

More information

Exercise 1.1 Hello world

Exercise 1.1 Hello world Exercise 1.1 Hello world The goal of this exercise is to verify that computer and compiler setup are functioning correctly. To verify that your setup runs fine, compile and run the hello world example

More information

Lambda functions. Zoltán Porkoláb: C++11/14 1

Lambda functions. Zoltán Porkoláb: C++11/14 1 Lambda functions Terminology How it is compiled Capture by value and reference Mutable lambdas Use of this Init capture and generalized lambdas in C++14 Constexpr lambda and capture *this and C++17 Zoltán

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

Structuur van Computerprogramma s 2

Structuur van Computerprogramma s 2 Structuur van Computerprogramma s 2 dr. Dirk Deridder Dirk.Deridder@vub.ac.be http://soft.vub.ac.be/ Vrije Universiteit Brussel - Faculty of Science and Bio-Engineering Sciences - Computer Science Department

More information

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples. Outline Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples. 1 Arrays I Array One type of data structures. Consecutive group of memory locations

More information

C++14 Reflections Without Macros, Markup nor External Tooling

C++14 Reflections Without Macros, Markup nor External Tooling C++14 Reflections Without Macros, Markup nor External Tooling Metaprogramming Tricks for POD Types Antony Polukhin Boost libraries maintainer (DLL, LexicalCast, Any, TypeIndex, Conversion) + Boost.CircularBuffer,

More information

ECE Fall 2014, Final Exam

ECE Fall 2014, Final Exam ECE 30862 Fall 2014, Final Exam DO NOT START WORKING ON THIS UNTIL TOLD TO DO SO. LEAVE IT ON THE DESK. THE LAST TWO PAGES ARE THE ANSWER SHEETS. TEAR THEM OFF AND PUT ALL ANSWERS ON THEM. TURN IN BOTH

More information

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID# Name UW ID# There are 6 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes,

More information

ECE Fall 20l2, Second Exam

ECE Fall 20l2, Second Exam ECE 30862 Fall 20l2, Second Exam DO NOT START WORKING ON THIS UNTIL TOLD TO DO SO. LEAVE IT ON THE DESK. You have until 12:20 to take this exam. Your exam should have 16 pages total (including this cover

More information

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference Pass by Value More Functions Different location in memory Changes to the parameters inside the function body have no effect outside of the function. 2 Passing Parameters by Reference Example: Exchange

More information

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18 CS31 Discussion Jie(Jay) Wang Week8 Nov.18 Outline Pointer Struct Memory Management When the program gets executed, it gets some amount of memory allocated for use. memory Program 1 Program 2 Memory Management

More information

COMP 2355 Introduction to Systems Programming

COMP 2355 Introduction to Systems Programming COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Today Templates Operator Overloading 2 Templates Syntactically similar to Java generics

More information

CS 225. September 24 Iterators. Data Structures. Wade Fagen-Ulmschneider

CS 225. September 24 Iterators. Data Structures. Wade Fagen-Ulmschneider CS 225 Data Structures September 24 Iterators Wade Fagen-Ulmschneider CS 225 So Far List ADT Linked Memory Implementation ( Linked List ) O(1) insert/remove at front/back O(1) insert/remove after a given

More information

Arrays and Pointers in C. Alan L. Cox

Arrays and Pointers in C. Alan L. Cox Arrays and Pointers in C Alan L. Cox alc@rice.edu Objectives Be able to use arrays, pointers, and strings in C programs Be able to explain the representation of these data types at the machine level, including

More information

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers CS 61C: Great Ideas in Computer Architecture C Arrays, Strings, More Pointers Instructor: Justin Hsia 6/20/2012 Summer 2012 Lecture #3 1 Review of Last Lecture C Basics Variables, Functions, Flow Control,

More information

UNDEFINED BEHAVIOR IS AWESOME

UNDEFINED BEHAVIOR IS AWESOME UNDEFINED BEHAVIOR IS AWESOME Piotr Padlewski piotr.padlewski@gmail.com, @PiotrPadlewski ABOUT MYSELF Currently working in IIIT developing C++ tooling like clang-tidy and studying on University of Warsaw.

More information

CS 7B - Spring Final Exam

CS 7B - Spring Final Exam CS 7B - Spring 2018 - Final Exam Write your responses to following questions on this paper, or attach extra, as needed. sentences where appropriate and write out code using proper style and syntax. 1.

More information

C - Func(on, Pointer, Array. Zhaoguo Wang

C - Func(on, Pointer, Array. Zhaoguo Wang C - Func(on, Pointer, Array Zhaoguo Wang A gigantic main loop https://github.com/qemu/qemu/blob/master/vl.c Function Readability Reusability Function int add(int a, int b) { int r = a + b; return r; name

More information

Lecture 2 Polymorphism, Traits, Policies, and Inheritance

Lecture 2 Polymorphism, Traits, Policies, and Inheritance Lecture 2 Polymorphism, Traits, Policies, and Inheritance Kenny Erleben Department of Computer Science University of Copenhagen c K. Erleben, May 4, 2006 p. 1/36 Polymorphism 1 Traditional, define abstract

More information

1. The term STL stands for?

1. The term STL stands for? 1. The term STL stands for? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d 2. Which of the following statements regarding the

More information

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy CAAM 420 Fall 2012 Lecture 29 Duncan Eddy November 7, 2012 Table of Contents 1 Templating in C++ 3 1.1 Motivation.............................................. 3 1.2 Templating Functions........................................

More information

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors Arrays Returning arrays Pointers Dynamic arrays Smart pointers Vectors To declare an array specify the type, its name, and its size in []s int arr1[10]; //or int arr2[] = {1,2,3,4,5,6,7,8}; arr2 has 8

More information

Advanced C++ Topics. Alexander Warg, 2017

Advanced C++ Topics. Alexander Warg, 2017 www.kernkonzept.com Advanced C++ Topics Alexander Warg, 2017 M I C R O K E R N E L M A D E I N G E R M A N Y Overview WHAT IS BEHIND C++ Language Magics Object Life Time Object Memory Layout INTRODUCTION

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

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Bernhard Boser & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic

More information

Exercise 7 References, Arrays, Vectors

Exercise 7 References, Arrays, Vectors Exercise 7 References, Arrays, Vectors Informatik I für Mathematiker und Physiker (HS 2015) Yeara Kozlov Slides courtesy of Kaan Yücer & Endri Dibra B. Gaertner, ETH Zürich, 2015 Agenda HW #5 Feedback

More information

Programming in C++: Assignment Week 8

Programming in C++: Assignment Week 8 Programming in C++: Assignment Week 8 Total Marks : 20 September 9, 2017 Question 1 Consider the following code segment. Mark 2 void myfunction(int test) { try { if (test) throw test; else throw "Value

More information

C Arrays, Strings, More Pointers Instructor: Steven Ho

C Arrays, Strings, More Pointers Instructor: Steven Ho C Arrays, Strings, More Pointers Instructor: Steven Ho Review of Last Lecture C Basics Variables, Functions, Flow Control, Types, and Structs Only 0 and NULL evaluate to FALSE Pointers hold addresses Address

More information

eingebetteter Systeme

eingebetteter Systeme Praktikum: Entwicklung interaktiver eingebetteter Systeme C++-Tutorial (falk@cs.fau.de) 1 Agenda Classes Pointers and References Functions and Methods Function and Operator Overloading Template Classes

More information

1 Basic considerations

1 Basic considerations C++ syntax-only reference card v.0.28 P. Areias (UEVORA) October 12, 2013 1 Basic considerations 1.1 Notation indicates that the programmer must provide an appropriate replacement. example expr can be

More information

Your first C and C++ programs

Your first C and C++ programs Your first C and C++ programs 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++,

More information

E.C. 2 E.C. 1 E.C.

E.C. 2 E.C. 1 E.C. Computer Chocolate Code-off: Rules: 1. Create one answer sheet with all group member names on it! (to be given to another group for grading No running code (you may use class notes) 2. You may ask yes/no

More information

1d: tests knowing about bitwise fields and union/struct differences.

1d: tests knowing about bitwise fields and union/struct differences. Question 1 1a: char ptr[] = Hello World ; char a = ptr[1], b = *(ptr+6); Creates an array of 12 elements, 11 visible letters and a character value 0 at the end. i true ii true iii false iv false v true

More information

Comp151. Generic Programming: Container Classes

Comp151. Generic Programming: Container Classes Comp151 Generic Programming: Container Classes Container Classes Container classes are a typical use for class templates, since we need container classes for objects of many different types, and the types

More information

The paramaterless ctor (aka default ctor)

The paramaterless ctor (aka default ctor) Initialization List The paramaterless ctor (aka default ctor) 2 int main() B b; class A public: A() std::cout

More information

Move semantics. Zoltán Porkoláb: C++11/14 1

Move semantics. Zoltán Porkoláb: C++11/14 1 Move semantics Pointers and arrays References Value vs move semantics Right value references Move semantics in C++11 Perfect forwarding Traps and pitfalls Zoltán Porkoláb: C++11/14 1 Arrays An array is

More information

Lesson 11: Generic Programming. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Lesson 11: Generic Programming. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology Lesson 11: Generic Programming 1 Function templates 2 General Occasionally, we like to write once but have many functions/methods with arguments of different types, instead of writing multiple overloaded

More information

ECE 461 Fall 2011, Final Exam

ECE 461 Fall 2011, Final Exam ECE 461 Fall 2011, Final Exam DO NOT START WORKING ON THIS UNTIL TOLD TO DO SO. LEAVE IT ON THE DESK. You have until 9:00PM to take this exam. Your exam should have 17 pages total (including this cover

More information

Why C++ is much more fun than C (C++ FAQ)?

Why C++ is much more fun than C (C++ FAQ)? From C to C++ Why C++ is much more fun than C (C++ FAQ)? 1. Classes & methods - OO design 2. Generic programming - Templates allow for code reuse 3. Stricter type system (e.g. function args) 4. Some run-time

More information

CS 61c: Great Ideas in Computer Architecture

CS 61c: Great Ideas in Computer Architecture Arrays, Strings, and Some More Pointers June 24, 2014 Review of Last Lecture C Basics Variables, functioss, control flow, types, structs Only 0 and NULL evaluate to false Pointers hold addresses Address

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1........ COMP6771 Advanced C++ Programming Week 4 Part Three: Function Objects and 2016 www.cse.unsw.edu.au/ cs6771 2........ Converting Class Types to bool Convenient to use a class object as bool to

More information

ECE Fall 2018, Test 2

ECE Fall 2018, Test 2 1 ECE 30862 Fall 2018, Test 2 DO NOT START WORKING ON THIS UNTIL TOLD TO DO SO. LEAVE IT ON THE DESK. THE LAST PAGE IS THE ANSWER SHEET. TEAR IT OFF AND PUT ALL ANSWERS THERE. TURN IN BOTH PARTS OF THE

More information

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers and Arrays CS 201 This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers Powerful but difficult to master Used to simulate pass-by-reference

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

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 Summer 2005 3:00pm 4:15pm Tuesday, July 5 Name: NetID: Lab Section

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer June 3, 2013 OOPP / C++ Lecture 9... 1/40 Const Qualifiers Operator Extensions Polymorphism Abstract Classes Linear Data Structure Demo Ordered

More information

Design Patterns in C++

Design Patterns in C++ Design Patterns in C++ Template metaprogramming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa April 6, 2011 G. Lipari (Scuola Superiore Sant Anna) Template metaprogramming

More information

Design Patterns in C++

Design Patterns in C++ Design Patterns in C++ Template metaprogramming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa April 6, 2011 G. Lipari (Scuola Superiore Sant Anna) Template metaprogramming

More information

CS 110 Computer Architecture. Lecture 4: Introduction to C, Part III. Instructor: Sören Schwertfeger.

CS 110 Computer Architecture. Lecture 4: Introduction to C, Part III. Instructor: Sören Schwertfeger. CS 110 Computer Architecture Lecture 4: Introduction to C, Part III Instructor: Sören Schwertfeger http://shtech.org/courses/ca/ School of Information Science and Technology SIST ShanghaiTech University

More information

Chapter 4: Arrays, Pointers and Strings

Chapter 4: Arrays, Pointers and Strings Chapter 4: Arrays, Pointers and Strings C Programming Language Šimon Řeřucha v3.2 March 1, 2016 A C program is like a fast dance on a newly waxed dance floor by people carrying razors. Waldi Ravens. Advanced

More information

Debug C++ Without Running. Anastasia Kazakova

Debug C++ Without Running. Anastasia Kazakova Debug C++ Without Running Anastasia Kazakova JetBrains @anastasiak2512 Agenda 1. Tricky C++ language. Show samples! 2. Seems to help but it doesn t. Why? Running / Debugging Static / dynamic code analysis

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

Polymorphism. Polymorphism. Person. class Person { void outputdetails(std::ostream& os=std::cout) const;

Polymorphism. Polymorphism. Person. class Person { void outputdetails(std::ostream& os=std::cout) const; Polymorphism! " "#$&%#(')"+,"( -/)02#3#"4%!567 783 4)"+,":;FGE/#3@H#/C 783I+&7 J$C A" Polymorphism K LM2LNOP,NGO M?O!QRTSU&U&O 67V7F (!DFG+&7/ 7G!E=WX+FDXFGE

More information

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture CS 61C: Great Ideas in Computer Architecture Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c And in Conclusion, 2 Processor Control Datapath Components of a Computer PC Registers Arithmetic

More information

2. Which of the following will print the value 2 for the above code?

2. Which of the following will print the value 2 for the above code? Computers are good at following instructions, but not at reading your mind. - Donald Knuth IMPORTANT QUESTIONS ON C LANGUAGE 1. What is the output of this program? char *ptr; char string[] = "How are you?";

More information

Overloaded Operators, Functions, and Students

Overloaded Operators, Functions, and Students , Functions, and Students Division of Mathematics and Computer Science Maryville College Outline Overloading Symbols 1 Overloading Symbols 2 3 Symbol Overloading Overloading Symbols A symbol is overloaded

More information

Templates. Rupesh Nasre. OOAIA January 2018

Templates. Rupesh Nasre. OOAIA January 2018 Templates Rupesh Nasre. OOAIA January 2018 Queue Interface Queue(); ~Queue(); void void insert(int x); x); int int remove(); int int a[100]; int int head, head, tail; tail; ; ; Queue Implementation Queue()

More information

Low-Level Programming in C

Low-Level Programming in C Low-Level Programming in C Menu C Programming Language Pointers in C Pointer Arithmetic Type checking in C 2 No support for: C Language Array bounds checking Null dereferences checking Data abstraction,

More information

09:30 12:00. CRingBuf 類 : a) CRingBuf(int size): b) ~CRingBuf(): c) bool get(int &data): read pointer 數,

09:30 12:00. CRingBuf 類 : a) CRingBuf(int size): b) ~CRingBuf(): c) bool get(int &data): read pointer 數, 立 C++ 1 99/06/22 09:30 12:00 什, 數 1. 不 參 2. 不 ( ) 3. 不 不 不 料 ( 不 不見, ), 4. 離 不 留 5. 論 律 理 6. CRingBuf 類 : 串流 路 料 理, ring buffer 來 料, ring buffer, 來 料, 念 write pointer 料, write pointer, read pointer 讀 料,

More information

September 19,

September 19, September 19, 2013 1 Problems with previous examples Changes to the implementation will require recompilation & relinking of clients Extensions will require access to the source code Solutions Combine

More information

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1. COMP6771 Advanced C++ Programming Week 7 Part One: Member Templates and 2016 www.cse.unsw.edu.au/ cs6771 2. Member Templates Consider this STL code: 1 #include 2 #include 3 #include

More information

C ++ Workshop Day 1 out of 5

C ++ Workshop Day 1 out of 5 C ++ Workshop Day 1 out of 5 Object Thierry Géraud, Roland Levillain, Akim Demaille {theo,roland,akim}@lrde.epita.fr EPITA École Pour l Informatique et les Techniques Avancées LRDE Laboratoire de Recherche

More information

Car. 9-2.cpp. Car cars[3]; p11-1.cpp. int main( ) { int i;

Car. 9-2.cpp. Car cars[3]; p11-1.cpp. int main( ) { int i; C++ 1. again 2. 3. Intelligent Electronic Systems Group 2 using namespace (Q)std::cout"std::" (A)stdcout #include using namespace std; //std C++ :: using namespace std; cout

More information

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine

More information

JTSK Programming in C II C-Lab II. Lecture 3 & 4

JTSK Programming in C II C-Lab II. Lecture 3 & 4 JTSK-320112 Programming in C II C-Lab II Lecture 3 & 4 Xu (Owen) He Spring 2018 Slides modified from Dr. Kinga Lipskoch Planned Syllabus The C Preprocessor Bit Operations Pointers and Arrays (Dynamically

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

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std; r6.14 For the operations on partially filled arrays below, provide the header of a func tion. d. Remove all elements that are less than a given value. Sol a. void remove_items_less_than(int arr[], int

More information

C++ Modern and Lucid C++ for Professional Programmers

C++ Modern and Lucid C++ for Professional Programmers Informatik C++ Modern and Lucid C++ for Professional Programmers part 9 Prof. Peter Sommerlad Institutsleiter IFS Institute for Software Rapperswil, HS 2015 Functors and Parameterizing STL Functors, Lambdas,

More information

First of all, it is a variable, just like other variables you studied

First of all, it is a variable, just like other variables you studied Pointers: Basics What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the address (rather than the value)

More information

for (int i = 1; i <= 3; i++) { do { cout << "Enter a positive integer: "; cin >> n;

for (int i = 1; i <= 3; i++) { do { cout << Enter a positive integer: ; cin >> n; // Workshop 1 #include using namespace std; int main () int n, k; int sumdigits; for (int i = 1; i n; cin.clear (); cin.ignore (100,

More information

Principles of Programming Languages, Exercise 1 (8+8 pts) Exercise 2 (8 pts) Exercise 3 (8 pts)

Principles of Programming Languages, Exercise 1 (8+8 pts) Exercise 2 (8 pts) Exercise 3 (8 pts) Principles of Programming Languages, 2012.07.23 Notes: - Total available time: 2h. - You may use any written material you need. - You cannot use computers, phones or laptops during the exam. Exercise 1

More information

Templates. Zoltán Porkoláb: C++11/14 1

Templates. Zoltán Porkoláb: C++11/14 1 Templates From macros to templates Parameter deduction, instantiation,specialization Class templates, partial specialization Explicit instantiation Dependent types Scope resolution, lookup Mixins CRTP

More information

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver CS 61C: Great Ideas in Computer Architecture C Pointers Instructors: Vladimir Stojanovic & Nicholas Weaver http://inst.eecs.berkeley.edu/~cs61c/sp16 1 Agenda Pointers Arrays in C 2 Address vs. Value Consider

More information

SFU CMPT Topic: Class Templates

SFU CMPT Topic: Class Templates SFU CMPT-212 2008-1 1 Topic: Class Templates SFU CMPT-212 2008-1 Topic: Class Templates Ján Maňuch E-mail: jmanuch@sfu.ca Monday 3 rd March, 2008 SFU CMPT-212 2008-1 2 Topic: Class Templates Class templates

More information

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Pointers and Strings Prentice Hall, Inc. All rights reserved. Pointers and Strings 1 sizeof operator Pointer Expressions and Pointer Arithmetic Relationship Between Pointers and Arrays Arrays of Pointers Case Study: Card Shuffling and Dealing Simulation sizeof operator

More information

Computer Systems C S Cynthia Lee

Computer Systems C S Cynthia Lee Computer Systems C S 1 0 7 Cynthia Lee 2 Today s Topics FUNCTION POINTERS AND CALLBACKS (This is a continuation of our theme of making generic functions in C) New tool: Function pointer mechanism for callbacks

More information

DAY 3. CS3600, Northeastern University. Alan Mislove

DAY 3. CS3600, Northeastern University. Alan Mislove C BOOTCAMP DAY 3 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh and Pascal Meunier s course at Purdue Memory management 2 Memory management Two

More information

STRICT_VARIANT. A simpler variant in C++ Chris Beck

STRICT_VARIANT. A simpler variant in C++ Chris Beck STRICT_VARIANT A simpler variant in C++ Chris Beck https://github.com/cbeck88/strict_variant What is a variant? A variant is a heterogenous container. std::vector many objects of one type std::variant

More information

Object Oriented Programming in C++

Object Oriented Programming in C++ Object Oriented Programming in C++ Jan Faigl Department of Computer Science Faculty of Electrical Engineering Czech Technical University in Prague Lecture 11 B3B36PRG C Programming Language Jan Faigl,

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

Biostatistics 615/815 Lecture 13: Numerical Optimization

Biostatistics 615/815 Lecture 13: Numerical Optimization Biostatistics 615/815 Lecture 13: Numerical Optimization Hyun Min Kang October 27th, 2011 Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 1 / 35 The Problem Hyun Min Kang Biostatistics

More information

Arizona s First University. ECE 373. Operation Overloading. The Revolution Operation Will Be Televised (and, these slides will be online later today)

Arizona s First University. ECE 373. Operation Overloading. The Revolution Operation Will Be Televised (and, these slides will be online later today) Arizona s First University. ECE 373 Operation Overloading The Revolution Operation Will Be Televised (and, these slides will be online later today) Previously, on ECE373 Families of operators can be overloaded

More information

Arrays and Memory Management

Arrays and Memory Management Arrays and Memory Management 1 Pointing to Different Size Objects Modern machines are byte-addressable Hardware s memory composed of 8-bit storage cells, each has a unique address A C pointer is just abstracted

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

Value categories. PRvalues. Lvalues

Value categories. PRvalues. Lvalues Value categories v5 Every C++14 expression belongs to exactly one of the following classifications, known as value categories: lvalue, xvalue, prvalue. There's an overlap between these, so a higher level

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Multiple Inheritance July 26, 2004 22.9 Multiple Inheritance 2 Multiple inheritance Derived class has several base classes Powerful,

More information

Exception handling. void sophisticatedalgorithm (char* name) { std::ifstream in(name); // using the file for an algorithm

Exception handling. void sophisticatedalgorithm (char* name) { std::ifstream in(name); // using the file for an algorithm a / p p U / / / Z ] Y R ] R / / a R 3 / S S S S p p handling 36=?3C=ED= +698B5 %+2436587:95

More information