Programming C++ Lecture 2. Howest, Fall 2014 Instructor: Dr. Jennifer B. Sartor

Similar documents
Programming C++ Lecture 2. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

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

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

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

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

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

Intermediate Programming, Spring 2017*

C++11 Move Constructors and Move Assignment. For Introduction to C++ Programming By Y. Daniel Liang

Introducing C++ to Java Programmers

CS201 Latest Solved MCQs

CS2255 HOMEWORK #1 Fall 2012

Object-Oriented Programming

Short Notes of CS201

Object Oriented Design

CS201 - Introduction to Programming Glossary By

Pointers. Developed By Ms. K.M.Sanghavi

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

CS201 Some Important Definitions

Chapter 2. Procedural Programming

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CS 376b Computer Vision

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

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

Separate Compilation Model

Lecture 15a Persistent Memory & Shared Pointers

CS24 Week 3 Lecture 1

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

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

Exam 3 Chapters 7 & 9

Collected By Anonymous

Class Destructors constant member functions

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

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

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

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

G52CPP C++ Programming Lecture 13

Fast Introduction to Object Oriented Programming and C++

Templates and Vectors

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

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Computer Programming C++ Classes and Objects 6 th Lecture

How to engineer a class to separate its interface from its implementation and encourage reuse.

C++_ MARKS 40 MIN

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

CS3215. Outline: 1. Introduction 2. C++ language features 3. C++ program organization

A A B U n i v e r s i t y

Come and join us at WebLyceum

CSE 303: Concepts and Tools for Software Development

CS201- Introduction to Programming Current Quizzes

Linked List using a Sentinel

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

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

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

AN OVERVIEW OF C++ 1

OBJECT ORIENTED PROGRAMMING

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

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Discussion 10: Exam 2 Review

Introduction to C ++

CA341 - Comparative Programming Languages

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

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

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

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

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

Midterm Review. PIC 10B Spring 2018

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

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

COMP322 - Introduction to C++

04-17 Discussion Notes

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

Object Oriented Design

Pointers, Dynamic Data, and Reference Types

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

CSCE 110 PROGRAMMING FUNDAMENTALS

Introduction to C++ Systems Programming

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

CSCI 123 Introduction to Programming Concepts in C++

CMSC 202 Midterm Exam 1 Fall 2015

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

Objects and streams and files CS427: Elements of Software Engineering

Pointers and References

EECE.3220: Data Structures Spring 2017

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:

CS

Object Oriented Programming using C++

CSE 100: C++ TEMPLATES AND ITERATORS

Object-Oriented Programming in C++

Programming Abstractions

04-19 Discussion Notes

COMP6771 Advanced C++ Programming

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

Introduction to Linked Lists. Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms

Praktikum: Entwicklung interaktiver eingebetteter Systeme

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

Transcription:

Programming C++ Lecture 2 Howest, Fall 2014 Instructor: Dr. Jennifer B. Sartor Jennifer.sartor@elis.ugent.be S

Arrays and Pointers void myprint(const char *); int main() { char *phrasey = C++Fun ; myprint(phrasey); return 0; void myprint(const char * s1) { while ((*s1)!= \0 ) { cout << *s1; s1++; 2

Arrays and Pointers void copy1(char*, const char *); void copy1(char * s1, int main() { char phrase1[10]; char *phrase2 = Hello ; copy1(phrase1, phrase2); cout << phrase1 << endl; return 0; const char * s2) { for(int i =0; s2[i]!= \0 ; i++) { s1[i] = s2[i]; 3

Arrays and Pointers void copy2(char*, const char *); int main() { char phrase3[10]; char *phrase4 = GBye ; copy2(phrase3, phrase4); cout << phrase3 << endl; return 0; void copy2(char * s1, const char * s2) { for(; *s2!= \0 ; s1++, s2++) { *s1 = *s2; 4

const with Pointers and Data S 4 types S nonconstant pointer to nonconstant data S nonconstant pointer to constant data S constant pointer to nonconstant data S constant pointer to constant data 5

Nonconst Ptr, Nonconst Data void converttouppercase(char *); int main() { char phrase[] = Hello world ; converttouppercase(phrase); cout << phrase << endl; return 0; void converttouppercase( char * sptr) { while (*sptr!= \0 ) { if ((*sptr) == o ) { *sptr = O ; sptr++; 6

Nonconst Ptr, Const Data void printchars(const char *); void printchars ( int main() { const char phrase[] = Hello world ; printchars(phrase); cout << endl; return 0; const char * sptr) { for( ; *sptr!= \0 ; sptr++) { cout << *sptr; 7

Const Ptr, Nonconst Data void printchars(const char *); int main() { const char phrase[] = Hello world ; printchars(phrase); cout << endl; return 0; void printchars ( char * const sptr) { //can we change array elems? //can we do sptr++? for( ; *sptr!= \0 ; sptr++) { *sptr = toupper(*sptr); cout << *sptr; 8

Const Ptr, Nonconst Data void printchars(const char *); void printchars ( char * const sptr) { int main() { const char phrase[] = Hello world ; printchars(phrase); cout << endl; return 0; //can we change array elems? YES //can we do sptr++? NO for(int i =0; (sptr[i])!= \0 ; i++) { sptr[i] = toupper(sptr[i]); cout << sptr[i]; //or *(sptr + i) 9

Const Ptr, Nonconst Data int main() { int x, y; int * const ptr = &x; //const pointer has to be initialized *ptr = 7; //modifies x no problem ptr = &y; //compiler ERROR const ptr cannot be reassigned return 0; S Arrays are constant pointers to nonconstant data 10

Const Ptr, Const Data int main() { int x = 5, y; const int * const ptr = &x; //const pointer has to be initialized cout << *ptr << endl; //no problems nothing modified *ptr = 7; //compiler ERROR const data cannot be changed ptr = &y; //compiler ERROR const ptr cannot be reassigned x++; //is this ok? return 0; 11

Vector #include <vector> using std::vector; vector<int> integers1(5); //already initialized to zero cout << integers1.size() << endl; //type is actually size_t integers1[3] = 89; //will NOT check bounds, but.at(3) will vector<int> integers2(integers1); //copies 1 into 2 12

Vector and Iterators #include <vector> [0] [1] [2] [3] [4] using std::vector; vector<int> integers1(5); //already initialized to zero vector<int>::iterator iter_i; //pointer into vector for(iter_i = integers1.begin(); iter_i!= integers1.end(); iter_i++) { cout << (*iter_i) << ; cout << endl; iter_i iter_i 13

Vector and Iterators #include <vector> using std::vector; iter_i [0] [1] [2] [3] [4] vector<int> integers1(5); //already initialized to zero vector<int>::iterator iter_i; //pointer into vector for(iter_i = integers1.begin(); iter_i!= integers1.end(); iter_i++) { cout << ++(*iter_i) << ; //you can use iterator to modify elements! cout << endl; 14

2D Vector and Iterators #include <vector> using std::vector; vector< vector<int> > matrix(numrows, vector<int>(numcols)); for (vector< vector<int> >::const_iterator row =matrix.begin(); row!= matrix.end(); row++) { for (vector<int>::const_iterator col = (*row).begin(); col!= (*row).end(); col++) { //do something with *col cout << *col << ; 15

Dynamic Memory Management S Like Java, puts things on the heap instead of the stack (so can be returned from functions!) S Unlike Java, you manage memory yourself no garbage collection S Helps create dynamic structures, arrays of correct size S Use new and delete S new finds memory of correct size, returns pointer to it 16

Dynamic Allocation double *pi = new double(3.14159); int *num = new int(); *num = 9; int *grades = new int[40]; S Finds space for 40 integers, returns address of first element to int pointer grades S Memory allocated for array NOT initialized (unknown) S Remember array name is a constant pointer to 0 th element of array 17

Dynamic Deallocation double *pi = new double(3.14159); int *num = new int(); *num = 9; int *grades = new int[40]; //finds space for 40 integers, returns address of first element to int pointer grades delete pi; delete num; delete [] grades; //NEED [] when deleting an array! 18

Dynamic Deallocation int *grades = new int[40]; //finds space for 40 integers, returns address of first element to int pointer grades delete [] grades; //NEED [] when deleting an array! grades = NULL; //good to null so no dangling pointers S You MUST pair every new with a delete S Not releasing dynamically allocated memory back to the heap can cause memory leaks. This is BAD. 19

Pointers to Pointers int **array2d = new int*[6]; S array2d is a pointer to a pointer, or a pointer to an array of pointers for (int i = 0; i < 6; i++) { array2d[i] = new int[7]; //initialize each row to array of ints array2d[0][0] = 8; S Dynamically allocated 2d arrays are NOT contiguous in memory 20

Pointers to Pointers array2d [0][0] [0][1] [0][2] [0][3] [0][4] [0][5] [0][6] int **array2d = new int*[6]; for (int i = 0; i < 6; i++) { array2d[i] = new int[7]; [1][0] [1][1] [1][2] [1][3] [1][4] [1][5] [1][6] //Dynamically allocated 2d arrays NOT contiguous in memory (each new is contiguous) 21...

Dealloc Pointers to Pointers int **array2d = new int*[6]; for (int i = 0; i < 6; i++) { array2d[i] = new int[7]; //initialize each row to array of ints array2d[0][0] = 8; for (int i = 0; i < 6; i++) { delete [] array2d[i]; delete [] array2d; //You MUST pair every new with a delete 22

Function Templates S S S We can do function overloading int boxvolume(int side) { return side * side * side; double boxvolume(double side) { return side * side * side; Why define 2 functions that look identical, but have different types? Overloading that is more compact and convenient = function templates. Only write it once! 23

Function Templates S S S S Template template <class T> //or template <typename T> T boxvolume(t side) { return side * side * side; C++ compiler automatically generates separate function template specializations for each type the function is called with. T is placeholder for actual data type int result = boxvolume(3); double result = boxvolume(6.2); 24

Inputting from a File #include <fstream> #include <stdio> #include <stdlib> using namespace std; int main() { ifstream inputfile( file.in, ios::in); if (!inputfile) { cerr << "File could not be opened" << endl; exit(1); //or return -1 int numppl; inputfile >> numppl; cout << "Num people is " << numppl << endl; return 0; 25

Classes! S Classes encapsulate objects S Member variables S Member functions S Getter and setter functions S Constructors and destructors S Access specifiers S Public versus private 26

A Class #include <iostream> #include <string> using namespace std; class Course { public: void setcoursename(string name) { coursename = name; string getcoursename() { return coursename; private: string coursename; ; int main() { string nameofcourse; Course mycourse; cout << mycourse.getcoursename(); 27 cout << endl; cout << Enter course name: ; getline( cin, nameofcourse ); mycourse.setcoursename( nameofcourse); cout << mycourse.getcoursename(); cout << endl; return 0;

A Class #include <iostream> #include <string> using namespace std; class Course { public: void setcoursename(string name) { coursename = name; string getcoursename() { return coursename; private: string coursename; ; int main() { string nameofcourse; Course mycourse; cout << mycourse.getcoursename(); 28 cout << endl; cout << Enter course name: ; getline( cin, nameofcourse ); mycourse.setcoursename( nameofcourse); cout << mycourse.getcoursename(); cout << endl; return 0;

A Class #include <iostream> #include <string> using namespace std; class Course { public: void setcoursename(string name) { coursename = name; string getcoursename() { return coursename; private: string coursename; ; int main() { string nameofcourse; Course mycourse; cout << mycourse.getcoursename(); 29 cout << endl; cout << Enter course name: ; getline( cin, nameofcourse ); mycourse.setcoursename( nameofcourse); cout << mycourse.getcoursename(); cout << endl; return 0;

A Class #include <iostream> #include <string> using namespace std; class Course { public: void setcoursename(string name) { coursename = name; string getcoursename() { return coursename; private: string coursename; ; int main() { string nameofcourse; Course mycourse; cout << mycourse.getcoursename(); 30 cout << endl; cout << Enter course name: ; getline( cin, nameofcourse ); mycourse.setcoursename( nameofcourse); cout << mycourse.getcoursename(); cout << endl; return 0;

A Class #include <iostream> #include <string> using namespace std; class Course { public: void setcoursename(string name) { coursename = name; string getcoursename() { return coursename; private: string coursename; ; int main() { string nameofcourse; Course mycourse; cout << mycourse.getcoursename(); 31 cout << endl; cout << Enter course name: ; getline( cin, nameofcourse ); mycourse.setcoursename( nameofcourse); cout << mycourse.getcoursename(); cout << endl; return 0;

Objects with Pointers, References Course mycourse; mycourse.getcoursename(); Course &courseref = mycourse; courseref.getcoursename(); Course *courseptr = &mycourse; courseptr->getcoursename(); Course* mycourse1 = new Course( ); mycourse1->getcoursename(); S Inside your class, don t return a reference or pointer to private data members! BAD style. 32

A Class with a Constructor class Course { public: Course( string name ) { setcoursename(name); void setcoursename(string name) { coursename = name; string getcoursename() { return coursename; private: string coursename; ; int main() { Course mycourse1( C++ Programming ); string nameofcourse; 33 cout << Enter course name: ; getline( cin, nameofcourse ); Course mycourse2(nameofcourse); cout << mycourse1.getcoursename(); cout << endl; cout << mycourse2.getcoursename(); cout << endl; return 0;

Constructors S S S S S S Special class methods with class name Cannot return anything Initialize state of object when created Usually public Called implicitly for every object creation Default constructor: no parameters, automatically created by compiler if no constructor S Calls default constructor of object data members of class 34

Destructors S Similar to constructor: tilde followed by class name (~Course( ) { ) S Receives no parameters, cannot return value. S Only 1 destructor and must be public. S Called implicitly when object destroyed (goes out of scope) S Does NOT release object s memory, but performs housekeeping. S Compiler implicitly creates empty one if none exists. 35

Destructors S To see order in which constructors/destructors called, see http://users.elis.ugent.be/~jsartor/howest/ constructordestructor.cpp S When in particular are they useful? S When you need to deallocate memory (call delete) because you called new in your class (probably for a pointer member variable) 36

Interface vs. Implementation S Interface defines and standardizes way to interact says what services are available and how to request them. S Implementation how services are carried out. S Separate them: interface = *.h, implementation = *.cpp S *.h includes function prototypes and data members S *.cpp defines member functions (use :: binary scope resolution operator to tie functions to class definition) 37

A Class //Course.h #include <string> using namespace std; class Course { public: Course( string name ); void setcoursename( string name); string getcoursename(); private: string coursename; ; //Course.cpp #include Course.h Course::Course( string name ) { setcoursename(name); void Course::setCourseName(string name) { coursename = name; string Course::getCourseName() { return coursename; 38

Test Program //test program can be in another file testcourse.cpp #include <iostream> using namespace std; #include Course.h int main() { Course mycourse1( CS105: Programming in C++ ); string nameofcourse; cout << Enter course name: ; getline( cin, nameofcourse ); Course mycourse2(nameofcourse); cout << mycourse1.getcoursename(); cout << endl; cout << mycourse2.getcoursename(); cout << endl; return 0; 39

Preprocessor Wrapper //Course.h #include <string> using namespace std; #ifndef COURSE_H #define COURSE_H class Course { public: Course( string name ); void setcoursename( string name); string getcoursename(); private: string coursename; ; #endif S Prevent header code from being included into same source code file more than once S Use uppercase, usually file name with. replaced by _ 40

Abstraction and Encapsulation S Abstraction = creation of a well-defined interface for object S Encapsulation = keep implementation details private S Data members and helper functions private S Promotes software reusability S Can change class data representation and/or implementation without changing code that uses class S Good software engineering 41

Compilation and Linking S Compiler uses included interface.h files to compile.cpp file into object code S Course.h + testcourse.cpp -> testcourse.o S Course.h + Course.cpp -> Course.o S Linker takes object code of testcourse.cpp and Course.cpp and STL and puts it together into an executable. S testcourse.o + Course.o + stl.o -> testc.exe 42