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

Similar documents
Interview Questions of C++

CS3157: Advanced Programming. Outline

G52CPP C++ Programming Lecture 18

File I/O Christian Schumacher, Info1 D-MAVT 2013

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

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

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

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

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

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

Exercise 6.2 A generic container class

Review Questions for Final Exam

Review Questions for Final Exam KEY

Computer Science II Lecture 1 Introduction and Background

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

Introduction to Programming using C++

Computer Science II Lecture 2 Strings, Vectors and Recursion

From Pseudcode Algorithms directly to C++ programs

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

6.096 Introduction to C++ January (IAP) 2009

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

Your first C++ program

C++ For Science and Engineering Lecture 12

Run Time Environment

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Lecture on pointers, references, and arrays and vectors

Computer Programming

ENGI 1020 Introduction to Computer Programming J U L Y 5, R E Z A S H A H I D I

Due Date: See Blackboard

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

pointers & references

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

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

Linked List using a Sentinel

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

COMP322 - Introduction to C++ Lecture 01 - Introduction

Short Notes of CS201

COEN244: Class & function templates

C++ for Python Programmers

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

Use the dot operator to access a member of a specific object.

Module 9. Templates & STL

W3101: Programming Languages C++ Ramana Isukapalli

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:

CS201 - Introduction to Programming Glossary By

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

Introduction to C++ Introduction to C++ 1

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

Bruce Merry. IOI Training Dec 2013

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

CS 32. Lecture 5: Templates

Due Date: See Blackboard

Praktikum: Entwicklung interaktiver eingebetteter Systeme

CS 247: Software Engineering Principles. ADT Design

Understanding main() function Input/Output Streams

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

CPSC 427a: Object-Oriented Programming

Scientific Computing

CIS 130 Exam #2 Review Suggestions

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

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:

CS349/SE382 A1 C Programming Tutorial

Tokens, Expressions and Control Structures

CS Final Exam Review Suggestions - Spring 2014

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

CS 7B - Spring Final Exam

Intermediate Programming, Spring 2017*

CSCE 110 PROGRAMMING FUNDAMENTALS

C++: Overview and Features

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

C++ For Science and Engineering Lecture 15

Comp151. Generic Programming: Container Classes

III. Classes (Chap. 3)

Lab 2: ADT Design & Implementation

Exam 3 Chapters 7 & 9

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

2. COURSE DESIGNATION: 3. COURSE DESCRIPTIONS:

COMP322 - Introduction to C++

This is a CLOSED-BOOK-CLOSED-NOTES exam consisting of five (5) questions. Write your answer in the answer booklet provided. 1. OO concepts (5 points)

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

CS2141 Software Development using C/C++ C++ Basics

Introduction to C++ (Extensions to C)

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

CE221 Programming in C++ Part 1 Introduction

September 10,

Templates and Vectors

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

CS Exam 2 Study Suggestions

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

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

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Lecture 2. Binary Trees & Implementations. Yusuf Pisan

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

CSI33 Data Structures

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Programming, numerics and optimization

Come and join us at WebLyceum

! Search: find a given target item in an array, ! Linear Search: Very simple search method: ! Operators such as =, +, <, and others can be

Chapter 14 Sequential Access Files

Transcription:

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 the C++ extensions needed for the course. More details appear in chapters 1 2 of the textbook.

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

Types bool and string The type bool stores a Boolean. The values are true and false. These values are equivalent to 1 and 0. The type string stores a character string. A string literal looks like this: string. We will use strings for output formatting. The string library provides many other operations.

iostream Input/output is performed with the iostream library. Operator >> reads inputs separated by white space. Operator << writes outputs. Standard streams are cin, cout, and cerr. File redirection applies to cin and cout. cerr always writes to the screen. File i/o is similar.

Example 1 #i n c l u de <i ostream > using namespace s t d ; main ( ) { double x, y, z ; c i n >> x >> y >> z ; bool f l a g 1 = x < y, f l a g 2 = y < z ; bool f l a g 3 = f l a g 1 && f l a g 2 ; cout << c o n j u n c t i o n o f << x << < << y << and << y << < << z << i s << f l a g 3 << e n d l ;

Output 1 % g++ primer1.c % a.out 1 2 3 conjunction of 1 < 2 and 2 < 3 is 1 % a.out 2.5 1 3.5 conjunction of 2.5 < 1 and 1 < 3.5 is 0

Functions A function can declare an empty argument list. A function must declare a return type, possibly void. Arguments can be passed by reference, e.g int &x. This is an alternative to a pointer, e.g. int *x. A reference can be immutable, e.g. const int &x. This avoids copying large arguments. Function names can be overloaded if the argument lists differ.

Comments Comments can begin with /* and end with */. The rest of a line is commented with //.

Example 2 I #i n c l u de <i ostream > using namespace s t d ; double f ( double x ) { x += 1. 0 ; return x ; // i n t f ( double x ) cannot be d e c l a r e d double g ( double &x ) { x += 1. 0 ; return x ;

Example 2 II main ( ) { double y = 1. 0, z = 1. 0, ny = f ( y ), nz = g ( z ) ; cout << y = << y << ny = << ny << z = << z << nz = << nz << e n d l ; % a.out y = 1 ny = 2 z = 2 nz = 2

Memory Allocation Memory is allocated with new and is freed with delete. These functions are type safe. // a l l o c a t e a double double x = new double ; // f r e e i t delete x ; // a l l o c a t e an a r r a y o f ten d o u b l e s double x = new double [ 1 0 ] ; // f r e e i t delete [ ] x ;

Function Templates Typed languages are awkward for type independent functions. C++ partially fixes this problem with templates. A function template is a parameterized function definition. The parameters can be instantiated with legal types. The compiler creates one function for each instantiation.

Example 3 I #i n c l u de <i ostream > using namespace s t d ; template<typename T> void exchange (T& x, T& y ) { T tmp = x ; x = y ; y = tmp ; main ( ) { i n t x = 1, y = 2 ; exchange<int >(x, y ) ; cout << x << << y << e n d l ;

Example 3 II s t r i n g s = y e s, t = no ; exchange<s t r i n g >(s, t ) ; cout << s << << t << e n d l ; % a.out 2 1 no yes

Classes C++ supports object-oriented programming through classes. A class is an extension of a c struct. It resembles a class in Jave or Python. Classes define local variables and functions, called methods. A class can be a specialization of another class. We will survey the basics and leave the rest to the book.

Example 4 I #i n c l u de <i ostream > using namespace s t d ; c l a s s Palindrome { // l o c a l v a r i a b l e s i n t n ; char c ; bool odd ; p u b l i c : // c o n s t r u c t o r Palindrome ( s t r i n g s ) { odd = s. s i z e ()%2 == 1 ; n = odd? 1+s. s i z e ()/2 : s. s i z e ( ) / 2 ; c = new char [ n ] ; f o r ( i n t i = 0 ; i < n ; ++i ) c [ i ] = s [ i ] ;

Example 4 II // d e s t r u c t o r Palindrome ( ) { delete [ ] c ; // method s t r i n g output ( ) const { s t r i n g s ; f o r ( i n t i = 0 ; i < n ; ++i ) s. push back ( c [ i ] ) ; i n t m = odd? n 2 : n 1 ; f o r ( i n t i = m; i >= 0 ; i ) s. push back ( c [ i ] ) ; return s ;

Example 4 III // c l a s s wide method s t a t i c bool v a l i d ( const s t r i n g &s ) { i n t n = s. s i z e ( ), nn = n / 2 ; f o r ( i n t i = 0 ; i < nn ; ++i ) i f ( s [ i ]!= s [ n i 1]) return f a l s e ; return true ; ; main ( ) { s t r i n g s t [ ] = { abcb, abcba, abccba ; f o r ( i n t i = 0 ; i < 3 ; ++i ) cout << s t [ i ] << << Palindrome : : v a l i d ( s t [ i ] ) << e n d l ;

Example 4 IV Palindrome p ( s t [ 1 ] ), q ( s t [ 2 ] ) ; cout << p. output ( ) << << q. output ( ) << e n d l ; % a.out abcb 0 abcba 1 abccba 1 abcba abccba

Templated Classes Classes can be templated just like functions. The Standard Template Library (STL) contains many useful classes. The classic example is vector. The STL is used in the textbook. It cannot be used in homework.

Example 5 The powerset of a set is the set of its subsets. Example: the powerset of {1, 2, 3 is {{, {1, {2, {3, {1, 2, {1, 3, {2, 3, {1, 2, 3. We compute the powerset with a recursive algorithm. The powerset of the empty set is {. To compute the powerset of {a 1,..., a n, Compute the powerset P of {a 1,..., a n 1. Compute Q by adding an to each element of P. Return P Q. Top-level recursion in our example: P = {{, {1, {2, {1, 2, Q = {{3, {1, 3, {2, 3, {1, 2, 3.

Example 5 I #i n c l u de <v e c t o r > #i n c l u de <i ostream > using namespace s t d ; template<typename T> c l a s s Vector { v e c t o r <T> v ; p u b l i c : // c o n s t r u c t o r Vector ( ) { // c o n s t r u c t o r Vector (T t ) { v. push back ( t ) ; // c o n s t r u c t o r Vector ( i n t n, T t ) { f o r ( i n t i = 0 ; i < n ; ++i ) v. push back ( t [ i ] ) ;

Example 5 II // methods i n t s i z e ( ) const { return v. s i z e ( ) ; void push back (T t ) { v. push back ( t ) ; T & operator [ ] ( i n t i ) { return v [ i ] ; ; template<typename T> Vector<Vector<T> > p o w e rset ( Vector<T> v, i n t n ) { i f ( n == 0) return Vector<Vector<T> >(Vector<T> ( ) ) ; Vector<Vector<T> > ops = p o w e r s e t ( v, n 1 ), ps ( ops ) ; f o r ( i n t i = 0 ; i < ops. s i z e ( ) ; ++i ) { Vector<T> w = ops [ i ] ;

Example 5 III w. push back ( v [ n 1 ] ) ; ps. push back (w ) ; return ps ; template<typename T> ostream& operator<< ( ostream &os, Vector<T>& t ) { os << ( ; f o r ( i n t i = 0 ; i + 1 < t. s i z e ( ) ; ++i ) os << t [ i ] << ; i f ( t. s i z e ( ) > 0) os << t [ t. s i z e () 1]; os << ) << e n d l ; return os ;

Example 5 IV main ( ) { i n t vd [ ] = {1, 2, 3, 4 ; Vector<int > v ( 4, vd ) ; Vector<Vector<int > > ps = p o w e r s et ( v, v. s i z e ( ) ) ; cout << ps ;

Output 5 (() (1) (2) (1 2) (3) (1 3) (2 3) (1 2 3) (4) (1 4) (2 4) (1 2 4) (3 4) (1 3 4) (2 3 4) (1 2 3 4) )