Life cycle of an object construction: creating a new object. Strings: constructors & assignment another type that C and C++ don't provide

Similar documents
Where do we go from here?

Life cycle of an object

Evolution of Programming Languages

Where do we go from here?

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!

use static size for this buffer

COEN244: Class & function templates

Short Notes of CS201

CS201 - Introduction to Programming Glossary By


CS11 Advanced C++ Fall Lecture 1

CA341 - Comparative Programming Languages

G52CPP C++ Programming Lecture 18

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Instantiation of Template class

Chapter 13: Copy Control. Overview. Overview. Overview

A Tour of the C++ Programming Language

Fast Introduction to Object Oriented Programming and C++

CS

Templates and Vectors

Dynamic Data Structures

7 TEMPLATES AND STL. 7.1 Function Templates

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

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

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

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

STL: C++ Standard Library

A Tour of the C++ Programming Language

Ch. 12: Operator Overloading

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

Interview Questions of C++

Pointers. Developed By Ms. K.M.Sanghavi

Exceptions, Templates, and the STL

G52CPP C++ Programming Lecture 16

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

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

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

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

Advanced C++ Topics. Alexander Warg, 2017

pointers & references

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

AN OVERVIEW OF C++ 1

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

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

Written by John Bell for CS 342, Spring 2018

Chapter 5. The Standard Template Library.

COMP6771 Advanced C++ Programming

2 ADT Programming User-defined abstract data types

When we program, we have to deal with errors. Our most basic aim is correctness, but we must

CS93SI Handout 04 Spring 2006 Apr Review Answers

More on Templates. Shahram Rahatlou. Corso di Programmazione++

Introduction to C++ Systems Programming

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

C++ Templates. David Camp

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

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

CS11 Advanced C++ Spring 2018 Lecture 2

Resource Management With a Unique Pointer. Resource Management. Implementing a Unique Pointer Class. Copying and Moving Unique Pointers

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

Recharge (int, int, int); //constructor declared void disply();

CSE 303: Concepts and Tools for Software Development

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Introduce C# as Object Oriented programming language. Explain, tokens,

G Programming Languages Spring 2010 Lecture 11. Robert Soulé, New York University

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!

G52CPP C++ Programming Lecture 15

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

G52CPP C++ Programming Lecture 20

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

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

List, Stack, and Queues

CS3157: Advanced Programming. Outline

September 10,

The following is an excerpt from Scott Meyers new book, Effective C++, Third Edition: 55 Specific Ways to Improve Your Programs and Designs.

A brief introduction to C++

Problem Solving with C++

CSC1322 Object-Oriented Programming Concepts

EL2310 Scientific Programming

ALL ABOUT POINTERS C/C++ POINTERS

EL2310 Scientific Programming

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Absolute C++ Walter Savitch

The Standard Template Library. An introduction

explicit class and default definitions revision of SC22/WG21/N1582 =

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

VIRTUAL FUNCTIONS Chapter 10

STL Quick Reference for CS 241

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

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

12. A bit of C++ Oscar Nierstrasz. Friday, October 21, 11

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

VALLIAMMAI ENGINEERING COLLEGE

Praktikum: Entwicklung interaktiver eingebetteter Systeme

1. The term STL stands for?

COMP 2355 Introduction to Systems Programming

Initializing and Finalizing Objects

Advanced C++ 4/13/2017. The user. Types of users. Const correctness. Const declaration. This pointer and const.

Transcription:

Life cycle of an object construction: creating a new object implicitly, by entering the scope where it is declared explicitly, by calling new construction includes initialization copying: using existing object to make a new one "copy constructor" makes a new object from existing one of the same kind implicitly invoked in (some) declarations, function arguments, function return assignment: changing an existing object occurs explicitly with = meaning of explicit and implicit copying must be part of the representation default is member-wise assignment and initialization destruction: destroying an existing object implicitly, by leaving the scope where it is declared explicitly, by calling delete on an object created by new includes cleanup and resource recovery Strings: constructors & assignment another type that C and C++ don't provide implementation of a String class combines constructors, destructors, copy constructor assignment, operator = constant references handles, reference counts, garbage collection Strings should behave like strings in Awk, Perl, Java can assign to a string, copy a string, etc. can pass them to functions, return as results, storage managed automatically no explicit allocation or deletion grow and shrink automatically efficient can create String from "..." C char* string can pass String to functions expecting char*

"Copy constructor" when a class object is passed to a function, returned from a function, or used as an initializer in a declaration, a copy is made: String substr(string s, int start, int len) a "copy constructor" creates an object of class X from an existing object of class X obvious way to write it causes an infinite loop: class String { String(String s) {... // doesn't work ; copy constructor parameter must be a reference so object can be accessed without copying class String { String(const String& s) {... //... ; copy constructor is necessary for declarations, function arguments, function return values String class class String { private: char *sp; String() { sp=strdup(""); // String s; String(const char *t) { sp=strdup(t); // String s("abc"); String(const String &t) { sp=strdup(t.sp); // String s(t); ~String() { delete [] sp; String& operator =(const char *);// s="abc" String& operator =(const String &);// s1=s2 const char *s() { return sp; // as char* ; assignment is not the same as initialization changes the state of an existing object the meaning of assignment defined by a member function named operator= x = y means x.operator=(y)

Assignment operators String& String::operator =(const char *t) { // s = "abc" delete [] sp; sp = strdup(t); return *this; String& String::operator=(const String& t) { // s1 = s2 if (this!= &t) { // avoid s1 = s1 delete [] sp; sp = strdup(t.sp); return *this; in a member function, this points to current object, so *this is a reference to the object assignment operators almost always end with return *this which returns a reference to the LHS permits multiple assignment s1 = s2 = s3 String class complete class String { private: char *sp; String() { sp=strdup(""); // String s; String(const char *t) { sp=strdup(t); // String s("abc"); String(const String &t) { sp=strdup(t.sp); // String s(t); ~String() { delete [] sp; String& operator =(const char *);// s="abc" String& operator =(const String &);// s1=s2 const char *s() { return sp; // as char* ; String& String::operator =(const char *s) { if (sp!= s) { delete [] sp; strdup(s); return *this; String& String::operator =(const String &t) { if (this!= &t) { delete [] sp; strdup(t.sp); return *this;

continued main() { String s = "abc", t = "def", u = s, w; printf("%s %s %s [%s]\n", s.s(), t.s(), u.s(), w.s()); s = "1234"; s = s; printf("s=%s\n", s.s()); s = s.s(); printf("s2=%s\n", s.s()); printf("u=%s\n", u.s()); s = t = u = "asdf"; printf("%s %s %s\n", s.s(), t.s(), u.s()); Handles and reference counts how to avoid unnecessary copying for classes like strings, arrays, other containers copy constructor may allocate new memory even if unnecessary e.g., in f(const String& s) string value would be copied even if it won't be changed by f a handle class manages a pointer to the real data implementation class manages the real data string data itself counter of how many Strings refer to that data when String is copied, increment the ref count when String is destroyed, decrement the ref count when last reference is gone, free all allocated memory with a handle class, copying only increments reference count "shallow" copy instead of "deep" copy

Reference/Use counts class Srep { // string representation char *sp; // data int n; // ref count Srep(const char *); friend class String; ; Srep::Srep(const char *s) { if (s == NULL) s = ""; sp = strdup(s); n = 1; class String { Srep *r; String(const char *); String(const String &); ~String(); ; String& operator =(const String &); // s1 = s2; String& operator =(const char *); // s = "abc"; const char *s() { return r->sp; use counts, part 2 String::String(const char *s = "") { r = new Srep(s); // String s="abc"; String s1; String::String(const String &t) { // String s=t; t.r->n++; // ref count r = t.r; String::~String() { if (--r->n <= 0) { delete [] r->sp; delete r; String& String::operator =(const char *s) { if (r->n > 1) { // disconnect self r->n--; r = new Srep(s); else { delete [] r->sp; // free old String r->sp = strdup(s); return *this; String& String::operator =(const String &t) { t.r->n++; // protect against s = s if (--r->n <= 0) { // nobody else using it delete [] r->sp; delete r; r = t.r; return *this;

Rules for constructors and assignment operators all objects have to have a constructor if you don't specify a constructor the default constructor copies members by their constructors need a no-argument constructor for arrays constructors should initialize all members if constructor calls new, destructor must call delete use delete [ ] for an array allocated with new T[n] copy constructor X(const X&) makes an object from another one without making an extra copy if there's a complicated constructor there will have to be an assignment operator make sure that x = x works assignment is NOT the same as construction constructors called in declarations, function arguments and function returns, to make a new object assignments called only in assignment statements to clobber an existing object Inheritance a way to create or describe one class in terms of another "a D is like a B, with these extra properties..." "a D is a B, plus " B is the base class or superclass D is the derived class or subclass Perl & C++ use base/derived; Java uses super/sub inheritance is used for classes that model strongly related concepts objects share some common properties, behaviors,... and have some properties and behaviors that are different base class contains aspects common to all derived classes contain aspects different for different kinds

Inheritance and derived classes consider different kinds of Investment Vehicles stocks, bonds, commodities, currencies,... base class IV contains aspects common to all name description derived classes contain aspects that are different for different kinds stock: ticker symbol, exchange, common/preferred,... bond: coupon, maturity, callable, call date... fund: vector of IVs sometimes you care about the difference dividend rate vs. interest rate sometimes you don't closing price Derived classes class IV { string name; IV void price(); // other items common to all IV's ; class Stock : public IV { String ticker; // other items specific to Stocks ; class Bond : public IV { double coupon; bool callable; // other items specific to Bonds ; IV Bond IV Stock a Stock is a derived class of (a kind of) IV a Stock "is a" IV inherits all members of IV adds its own members a Bond is also a derived class of IV

More on derived classes derived classes can add their own data members can add their own member functions can override base class functions with functions of same name and argument types class Stock : public IV { String ticker; void price() {... // overrides IV::price() ; class Bond : public IV { bool callable; bool is_callable() {... void price() {... // overridesiv::price() ; Stock gm; Bond ibm; gm.price(); // calls Stock::price() ibm.price(); // calls Bond::price() Virtual Functions what if we have bunch of different IVs and want to price them all in a loop? virtual function mechanism lets each object carry information about what functions to apply class IV { virtual void price();... ; "virtual" means that a derived class may provide its own version of this function, which will be called automatically for instances of that derived class base class can provide a default implementation a "pure" base class must be derived from can't exist on its own

Polymorphism when a pointer or reference to a base-class type points to a derived-class object and you use that pointer or reference to call a virtual function this calls the derived-class function "polymorphism": proper function to call is determined at run-time e.g., pricing IVs on a linked list: price_all(iv *ip) { for ( ; ip!= NULL; ip = ip->next) ip->price(); virtual function mechanism automatically calls the right price() function for each object the loop does not change if more kinds of IVs are added Implementation of virtual functions each class object has one extra word that holds a pointer to a table of virtual function pointers ("vtbl") (only if class has virtual functions) each class with virtual functions has one vtbl a call to a virtual function calls it indirectly through the vtbl Bond B1 Bond B2 Stock S1 Stock S2 vtbl for class Bond price update vtbl for class Stock price update code code code code

Summary of inheritance a way to describe a family of types by collecting similarities (base class) and separating differences (derived classes) polymorphism: proper member functions determined at run time virtual functions are the C++ mechanism not every class needs inheritance may complicate without compensating benefit use composition instead of inheritance? an object contains an (has) an object rather than inheriting from it "is-a" versus "has-a" inheritance describes "is-a" relationships composition describes "has-a" relationships Templates (parameterized types, generics) another approach to polymorphism compile time, not run time a template specifies a class or a function that is the same for several types except for one or more type parameters e.g., a vector template defines a class of vectors that can be instantiated for any particular type vector<int> vector<string> vector<vector<int> > templates versus inheritance: use inheritance when behaviors are different for different types pricing different IVs is different use template when behaviors are the same, regardless of types accessing the n-th element of a vector is the same, no matter what type the vector is

Vector template class vector class defined as a template, to be instantiated with different types of elements template <typename T> class vector { T *v; // pointer to array int size; // number of elements vector(int n=1) { v = new T[size = n]; T& operator [](int n) { assert(n >= 0 && n < size); return v[n]; ; vector<int> iv(100); // vector of ints vector<complex> cv(20); // vector of complex vector<vector<int> > vvi(10); // vector of vector of int vector<double> d; // default size compiler instantiates whatever is used Template functions can define ordinary functions as templates e.g., max(t, T) template <typename T> T max(t x, T y) { return x > y? x : y; requires operator> for type T already there for C's arithmetic types don't need a type name to use it compiler infers types from arguments max(double, double) max(int, int) max(int, double) doesn't compile: no coercion compiler instantiates code for each different use in a program

Scoped pointer class allocates space when used frees it automatically when pointer goes out of scope template <typename T> class SP { T *tptr; SP(T *p) { tptr = p; ~SP() { printf("sp destructor %s\n", tptr->fs); delete tptr; T* operator ->() { printf("op->%s\n", tptr->fs); return tptr; ; class ptr { char *fs; ptr(char *s) { printf("construct ptr(%s)\n", fs=strdup(s)); ~ptr() { printf("destruct ptr(%s)\n", fs); delete fs; ; int main() { printf("start\n"); SP<ptr> ptr1p = new ptr("new ptr1"); SP<ptr> ptr2p = new ptr("new ptr2"); ptr1p->fs = "change ptr1 value"; printf("end\n"); Standard Template Library (STL) Alex Stepanov (GE > Bell Labs > HP > SGI > Compaq > Adobe) general-purpose library of containers (vector, list, set, map, ) generic algorithms (find, replace, sort, ) algorithms written in terms of iterators performing specified access patterns on containers rules for how iterators work, how containers have to support them generic: every algorithm works on a variety of containers, including built-in types e.g., find elements in char array, vector<int>, list< > iterators: generalization of pointer for uniform access to items in a container

Containers and algorithms STL container classes contain objects of any type sequences: vector, list, slist, deque sorted associative: set, map, multiset, multimap hash_set and hash_map are non-standard each class is a template that can be instantiated to contain any type of object generic algorithms find, find_if, find_first_of, search,... count, min, max, copy, replace, fill, remove, reverse, accumulate, inner_product, partial_sum, sort binary_search, merge, set_union, performance guarantees each combination of algorithm and iterator type specifies worst-case (O( )) performance bound e.g., maps are O(log n) access, vectors are O(1) access Iterators a generalization of C pointers for (p = begin; p < end; ++p) do something with *p range from begin() to just before end() ++iter advances to the next if there is one *iter dereferences (points to value) uses operator!= to test for end of range for (iter i = v.begin(); i!= v.end(); ++i) do something with *i #include <vector> #include <iterator> using namespace ::std; int main() { vector<double> v; for (int i = 1; i <= 10; i++) v.push_back(i); vector<double>::const_iterator it; double sum = 0; for (it = v.begin(); it!= v.end(); ++it) sum += *it; printf("%g\n", sum); [begin, end)

Iterators (2) no change to loop if type or representation changes set<double> v; set<double>::const_iterator it; for (it = v.begin(); it!= v.end(); ++it) sum += *it; not all containers support all iterator operations input iterator can only read items in order, can't store into them (input from file) output iterator can only write items in order, can't read them (output to a file) forward iterator can read/write items in order, can't go backwards (singly-linked list) bidirectional iterator can read/write items in either order (doubly-linked list) random access iterator can access items in any order (array) Example: STL sort #include <iostream> #include <iterator> #include <vector> #include <string> #include <algorithm> using namespace ::std; int main() { // sort stdin by lines vector<string> vs; string tmp; while (getline(cin, tmp)) vs.push_back(tmp); sort(vs.begin(), vs.end()); copy(vs.begin(), vs.end(), ostream_iterator<string>(cout, "\n")); vs.push_back(s) pushes s onto "back" (end) of vs 3rd argument of copy is a "function object" that calls a function for each iteration uses overloaded operator()

Function objects anything that can be applied to zero or more arguments to get a value and/or change the state of a computation can be an ordinary function pointer can be an object of a type defined by a class in which the function call operator operator() is overloaded template <typename T> class bigger { bool operator()(t const& x, T const& y) { return x > y; ; to sort strings in decreasing order, vector<string> vs; sort(vs.begin(), vs.end(), bigger<string>()); to sort numbers in decreasing order, vector<double> vd; sort(vd.begin(), vd.end(), bigger<double>()); Template metaprogramming do computation at compile time to avoid computation at run time evaluating constants, unrolling loops, building data structures // from effective c++ 3e, by scott meyers #include <iostream> using namespace ::std; template<unsigned n> struct Factorial { enum { value = n * Factorial<n-1>::value ; ; template<> struct Factorial<0> { enum { value = 1 ; ; int main() { std::cout << Factorial<5>::value << "\n"; std::cout << Factorial<10>::value << "\n";

Word frequency count: C++ STL #include <iostream> #include <map> #include <string> int main() { string temp; map<string, int> v; map<string, int>::const_iterator i; while (cin >> temp) v[temp]++; for (i = v.begin(); i!= v.end(); ++i) cout << i->first << " " << i->second << "\n"; Exception handling necessary so libraries can propagate errors back to users class subscriptrange { int n; subscriptrange(int n) { this->n = n; ; int& ivec::operator [](int n) { if (n < 0 n >= size) throw subscriptrange(n); else return v[n]; int g(ivec& v) { return v[1000]; int f() { ivec iv(100); try { printf("normal\n"); return g(iv); // normal return if no exceptions catch (subscriptrange sr) { printf("subscriptrange %d\n", sr.n); return 0; // if subscriptrange raised in g() or anything it cal catch (...) { // get here if some other printf("other\n"); return -1; // exception was raised

What to use, what not to use? Use classes const const references default constructors C++ -style casts bool new / delete C++ string type Don't use malloc / free multiple inheritance run time type identification references if not const overloaded operators (except for arithmetic types) default arguments (overload functions instead) Use sparingly / cautiously overloaded functions inheritance virtual functions exceptions STL