Intermediate Programming, Spring 2017*

Similar documents
Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

Homework 4. Any questions?

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CS24 Week 3 Lecture 1

Intermediate Programming, Spring 2017*

Programmazione. Prof. Marco Bertini

Class Destructors constant member functions

Pointers, Dynamic Data, and Reference Types

Intermediate Programming, Spring 2017*

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

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Intermediate Programming, Spring 2017*

Scope. Scope is such an important thing that we ll review what we know about scope now:

CSCE 110 PROGRAMMING FUNDAMENTALS

lecture04: Constructors and Destructors

CS

CS102 C++ Exception Handling & Namespaces

CSE 303: Concepts and Tools for Software Development

Fast Introduction to Object Oriented Programming and C++

Common Misunderstandings from Exam 1 Material

CE221 Programming in C++ Part 1 Introduction

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

Engineering Tools III: OOP in C++

C++ Constructor Insanity

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

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

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

Exercise 1.1 Hello world

Object Reference and Memory Allocation. Questions:

Chapter 18 - C++ Operator Overloading

Operator Overloading in C++ Systems Programming

Unified Modeling Language a case study

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

CS242 COMPUTER PROGRAMMING

GEA 2017, Week 4. February 21, 2017

Intermediate Programming, Spring 2017*

Constructors for classes

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

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!

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Intermediate Programming, Spring 2017*

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

04-17 Discussion Notes

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

Assignment operator string class c++ Assignment operator string class c++.zip

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

05-01 Discussion Notes

CSE 333 Lecture smart pointers

Vector and Free Store (Vectors and Arrays)

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

CSC 330 Object-Oriented Programming. Exception Handling CSC 330

Polymorphism Part 1 1

UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

CS 11 C++ track: lecture 1

Intermediate Programming, Spring 2017*

Week 3: Pointers (Part 2)

CSE 333 Lecture smart pointers

CPSC 427: Object-Oriented Programming

การทดลองท 8_2 Editor Buffer Array Implementation

Classes and Pointers: Some Peculiarities

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Member functions 21/11/2018. Accessing member variables. Accessing member variables

Fundamentals of Programming Session 24

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

Pointers. Developed By Ms. K.M.Sanghavi

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

Implementing an ADT with a Class

Classes in C++98 and C++11

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

CSC1322 Object-Oriented Programming Concepts

Short Notes of CS201

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

1. Which of the following best describes the situation after Line 1 has been executed?

COMP6771 Advanced C++ Programming

Programming in C++: Assignment Week 8

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

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

CS201 - Introduction to Programming Glossary By

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

AN OVERVIEW OF C++ 1

ADTs in C++ In C++, member functions can be defined as part of a struct

CS201 Some Important Definitions

Object-Oriented Programming for Scientific Computing

CS 225. Data Structures

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

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

Computer Programming with C++ (21)

a data type is Types

COMP6771 Advanced C++ Programming

Introduction to Programming

OBJECT ORIENTED PROGRAMMING

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

Program template-smart-pointers-again.cc

Transcription:

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 and you should not follow my lead on this.

Outline Review Constructors, destructors new and delete

Default Constructors The default constructor is called when no initialization parameters are passed #include "instructor.h" Instuctor inst; // Default constructor called instructor.h #ifndef INSTRUCTOR_INCLUDED #define INSTRUCTOR_INCLUDED #include <string> class Instructor std::string name; void print( void ) const; ; #endif // INSTRUCTOR_INCLUDED

Default Constructors The default constructor is called when no initialization parameters are passed If no constructor is given, C++ implicitly defines one which calls the default constructor of each of the members instructor.h #ifndef INSTRUCTOR_INCLUDED #define INSTRUCTOR_INCLUDED #include <string> class Instructor std::string name; void print( void ) const; ; #endif // INSTRUCTOR_INCLUDED

Default Constructors The default constructor is called when no initialization parameters are passed Or the class can provide its own Looks like a function: Whose name is the class name With no (void) arguments With no return type This should be public instructor.h #ifndef INSTRUCTOR_INCLUDED #define INSTRUCTOR_INCLUDED #include <string> class Instructor std::string name; Instructor( void ) name = "misha"; void print( void ) const; ; #endif // INSTRUCTOR_INCLUDED

Default Constructors The default constructor is called when no initialization parameters are passed Or the class can provide its own It can be defined in the class definition (if it's short) instructor.h #ifndef INSTRUCTOR_INCLUDED #define INSTRUCTOR_INCLUDED #include <string> class Instructor std::string name; Instructor( void ) name = "misha"; void print( void ) const; ; #endif // INSTRUCTOR_INCLUDED

Default Constructors The default constructor is called when no initialization parameters are passed Or the class can provide its own It can be defined in the class definition (if it's short) Or it can be declared in the.h file and defined the.cpp file instructor.cpp #include "instructor.h" Instructor::Instructor( void ) name = "misha"; instructor.h #ifndef INSTRUCTOR_INCLUDED #define INSTRUCTOR_INCLUDED #include <string> class Instructor std::string name; Instructor( void ) void print( void ) const; ; #endif // INSTRUCTOR_INCLUDED

Non-Default Constructors Constructors can also take arguments, allowing the caller to customize the object #include <iostream> #include "instructor.h" >>./a.out misha yotam >> Instructor i1, i2( "yotam"); std::cout << i1.name << std::endl; std::cout << i2.name << std::endl; return 0; instructor.h #ifndef INSTRUCTOR_INCLUDED #define INSTRUCTOR_INCLUDED #include <string> class Instructor std::string name; Instructor( void ) name = "misha"; Instructor( std::string n ) name = n; void print( void ) const; ; #endif // INSTRUCTOR_INCLUDED

Non-Default Constructors Before the object's constructor is called, C++ calls the default constructor of each member #include "myclass.h" C2 c2( 5 ); return 0; >>./a.out C1(0) C2(5) C1(5) >> myclass.h #include <iostream> class C1 int _value; void _init( int v ) _value = v; std::cout << "C1(" << v << ")" << std::endl; // Do a lot of work here C1( void ) _init(0); C1( int v ) _init(v); ; class C2 C1 _c1; C2( int v ) std::cout << "C2(" << v << ")" << std::endl; _c1 = C1( v ); ;

Non-Default Constructors Before the object's constructor is called, C++ calls the default constructor of each member We call C1's default constructor and then its non-default constructor We would like to invoke C1's non-default constructor directly #include "myclass.h" C2 c2( 5 ); return 0; >>./a.out C1(0) C2(5) C1(5) >> myclass.h #include <iostream> class C1 int _value; void _init( int v ) _value = v; std::cout << "C1(" << v << ")" << std::endl; // Do a lot of work here C1( void ) _init(0); C1( int v ) _init(v); ; class C2 C1 _c1; C2( int v ) std::cout << "C2(" << v << ")" << std::endl; _c1 = C1( v ); ;

Non-Default Constructors Initializer lists allow us to specify that a (non-default) constructor should be used to initialize the member directly Before the body of the constructor: a ":" followed by a comma-separated list of member constructors #include "myclass.h" C2 c2( 5 ); return 0; >>./a.out C1(5) C2(5) >> myclass.h #include <iostream> class C1 int _value; void _init( int v ) _value = v; std::cout << "C1(" << v << ")" << std::endl; // Do a lot of work here C1( void ) _init(0); C1( int v ) _init(v); ; class C2 C1 _c1; C2( int v ) : _c1(v) std::cout << "C2(" << v << ")" << std::endl; ;

Non-Default Constructors Initializer lists allow us to specify that a (non-default) constructor should be used to initialize the member directly They can be used to initialize primitive types (even though those don t have a constructor) #include "myclass.h" C2 c2( 5 ); return 0; >>./a.out C1(5) C2(5) >> myclass.h #include <iostream> class C1 int _value; void _init( int v ) _value = v; std::cout << "C1(" << v << ")" << std::endl; // Do a lot of work here C1( void ) _init(0); C1( int v ) _init(v); ; class C2 C1 _c1 ; int _value; C2( int v ) : _c1(v), _value(v) std::cout << "C2(" << v << ")" << std::endl; ;

Destructors A class constructor s job is to initialize the fields of the object It s common for a constructor to obtain a resource (allocate memory, open a file, etc.) that should be released when the object is destroyed A class destructor is a method called by C++ when the object goes out of scope or is otherwise deallocated

Destructors A class constructor s job is to initialize the fields of the object It s common for a constructor to obtain a resource size_t (allocate sz; memory, open a file, etc.) that should be released when the object is destroyed A class destructor is a method called by C++ when the object goes out of scope or is otherwise deallocated (e.g. using assert( delete values * ) ); Looks like a function: Whose name is the class name prepended with a "~" With no (void) arguments With no return type This should be public #include <iostream> #include <cassert> class Foo int* values; Foo( int s ) : sz( s ) values = malloc( sizeof(int)*sz ); ~Foo( void ) free( values ); ; Foo f( 10 ); return 0;

Destructors A class constructor s job is to initialize the fields of the object It s common for a constructor to obtain a resource int* (allocate values; memory, open a file, etc.) that should be released when the object is destroyed A class destructor is a method called by C++ when assert( the values object ); goes out of scope or is otherwise deallocated (e.g. using delete * ) As with other methods, it can be defined in the class definition or outside of it #include <iostream> #include <cassert> class Foo size_t sz; Foo( int s ) : sz( s ) values = malloc( sizeof(int)*sz ); ~Foo( void ); ; Foo::~Foo( void ) free( values ); Foo f( 10 ); return 0;

Outline Review new and delete

Memory Allocation In C, we allocate memory on the heap using malloc: void* malloc( size_t size ); This function does not need to know the type of the data Just the size of the memory we were requesting Similarly, we deallocate memory from the heap using free: void free( void* ptr ); This function does not need to know the type of the data Just the location that we are freeing

Memory Allocation In C++, we need to know the data-type to invoke the constructor We do this using the new operator: <DataType>* new DataType( <ConstructorParams> ); This allocates memory for a single object And it invokes the constructor If no constructor parameters are passed, the default constructor is invoked Though primitive types (e.g. ints, chars, etc.) don't have constructors, we can use new to allocate them on the heap #include <iostream> #include <string> using std::string; string* strptr = new string( "Hello" ); std::cout << *strptr << std::endl;... return 0;

Memory Allocation In C++, we need to know the data-type to invoke the destructor We do this using the delete operator: delete <DataType>*; This invokes the destructor of the object Though primitive types (e.g. ints, chars, etc.) don't have destructors, we can use delete to deallocate them from the heap And deallocates its memory #include <iostream> #include <string> using std::string; string* strptr = new string( "Hello" ); std::cout << *strptr << std::endl; delete strptr; return 0;

Memory Allocation We allocate multiple objects simultaneously using new[]: <DataType>* new DataType[<NumElems>]; This allocates memory for a NumElems objects And it invokes the default constructor for each one And we deallocate using delete[]: delete[] <DataType>*; This invokes the destructor for each object And then deallocates the memory for the entire array of objects

Memory Allocation #include <iostream> #include "instructor.h" using namespace std; int count; cout << "Instructor Count: "; cin >> count; Instructor** instr = new Instructor*[count]; for( int i=0 ; i<count ; i++ ) cout << "Name: "; string name; cin >> name; instr[i] = new Instructor( name ); for( int i=0 ; i<count ; i++ ) cout << instr[i]->name << endl; for( int i=0 ; i<count ; i++ ) delete instr[i]; delete[] instr; return 0; ;

Memory Allocation Memory allocated with new must be deallocated with delete #include <iostream> #include "instructor.h" using namespace std; int count; cout << "Instructor Count: "; cin >> count; Instructor** instr = new Instructor*[count]; for( int i=0 ; i<count ; i++ ) cout << "Name: "; string name; cin >> name; instr[i] = new Instructor( name ); for( int i=0 ; i<count ; i++ ) cout << instr[i]->name << endl; for( int i=0 ; i<count ; i++ ) delete instr[i]; delete[] instr; return 0; ;

Memory Allocation Memory allocated with new must be deallocated with delete Memory allocated with new[] must be deallocated with delete[] We are deleteing an array of pointers to Instructor objects The Instructor destructor is not called #include <iostream> #include "instructor.h" using namespace std; int count; cout << "Instructor Count: "; cin >> count; Instructor** instr = new Instructor*[count]; for( int i=0 ; i<count ; i++ ) cout << "Name: "; string name; cin >> name; instr[i] = new Instructor( name ); for( int i=0 ; i<count ; i++ ) cout << instr[i]->name << endl; for( int i=0 ; i<count ; i++ ) delete instr[i]; delete[] instr; return 0; ;

Piazza Resources section Resources tab Exercise 9-1