Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

Similar documents
More Advanced Class Concepts

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

Ch. 12: Operator Overloading

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

Overloaded Operators, Functions, and Students

Operators. The Arrow Operator. The sizeof Operator

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11

Overloading & Polymorphism

Operator overloading. Instructor: Bakhyt Bakiyev

Review. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

The University Of Michigan. EECS402 Lecture 15. Andrew M. Morgan. Savitch Ch. 8 Operator Overloading Returning By Constant Value

OBJECT ORIENTED PROGRAMMING USING C++

Operation Overloading.

Function Declarations. Reference and Pointer Pitfalls. Overloaded Functions. Default Arguments

2 ADT Programming User-defined abstract data types

Object-Oriented Design (OOD) and C++

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

Overloading Operators in C++

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

PIC10B/1 Winter 2014 Exam I Study Guide

A <Basic> C++ Course

A <Basic> C++ Course

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

G52CPP C++ Programming Lecture 13

Chapter 18 - C++ Operator Overloading

Program construction in C++ for Scientific Computing

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

CS 247: Software Engineering Principles. ADT Design

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

7.1 Optional Parameters

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

G52CPP C++ Programming Lecture 17

More class design with C++ Starting Savitch Chap. 11

Instantiation of Template class

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

Do not write in this area TOTAL. Maximum possible points: 75

CS250 Final Review Questions

Object-Oriented Principles and Practice / C++

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Question: How can we compare two objects of a given class to see if they are the same? Is it legal to do: Rectangle r(0,0,3,3);,, Rectangle s(0,0,4,4)

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

14.1. Chapter 14: static member variable. Instance and Static Members 8/23/2014. Instance and Static Members

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

Chapter 11: More About Classes and Object-Oriented Programming

IS0020 Program Design and Software Tools Midterm, Fall, 2004

CS11 Intro C++ Spring 2018 Lecture 5

CS105 C++ Lecture 7. More on Classes, Inheritance

Due Date: See Blackboard

CPSC 427: Object-Oriented Programming

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

Short Notes of CS201

Object oriented programming

Operator overloading

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Interview Questions of C++

Pointers. Developed By Ms. K.M.Sanghavi

CS201 - Introduction to Programming Glossary By

VIRTUAL FUNCTIONS Chapter 10

Intermediate Programming & Design (C++) Classes in C++

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

Laboration 1 Introduction: matrices

CSc 328, Spring 2004 Final Examination May 12, 2004

During the course of writing the Matrix class we will cover some interesting C++ topics. Specically: constructors and destructors, operator

Where do we go from here?

Object-Oriented Principles and Practice / C++

Lecture 3 ADT and C++ Classes (II)

Pointers, Dynamic Data, and Reference Types

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

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!

UNIT- 3 Introduction to C++

CS250 Final Review Questions

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

Problem Solving with C++

CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

Evolution of Programming Languages

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

CS250 Final Review Questions

SFU CMPT Topic: Class Templates

Plan of the day. Today design of two types of container classes templates friend nested classes. BABAR C++ Course 103 Paul F. Kunz

Due Date: See Blackboard

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

CPSC 427: Object-Oriented Programming

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

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

C++ Programming Lecture 4 Software Engineering Group


Roxana Dumitrescu. C++ in Financial Mathematics

Assignment of Objects

Absolute C++ Walter Savitch

Ch 8. Operator Overloading, Friends, and References

Fast Introduction to Object Oriented Programming and C++

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

Computer Science II CSci 1200 Lecture 18 Operators and Friends

Programming, numerics and optimization

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

Operator Overloading in C++ Systems Programming

Transcription:

Vectors of Objects As we have mentioned earlier, you should almost always use vectors instead of arrays. If you need to keep track of persons (objects of class Person), you must decide what to store in the vector: Person objects or pointers to Person objects. vector<person> v; // vector of Person objects Person p1("bob"); v.push_back(p1); // p1 is copied v.push_back(person("alice")); // the object is moved for (const auto& p : v) { cout << p.getname() << endl; Class Person must have a copy (and/or move) constructor. Large objects can be slow to copy. You cannot store objects of subclasses of Person. (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 141 / 281 Vectors of Pointers to Objects You can instead store pointers to heap-allocated Person objects. vector<person*> pv; Person* p = new Person("Bob"); pv.push_back(p); for (auto pptr : pv) { cout << pptr->getname() << endl; for (auto pptr : pv) { delete pptr; Only pointers are copied, no copy constructor is needed. You can store pointers to objects of subclasses of Person. But you must delete all objects before the vector goes out of scope. (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 142 / 281 Vectors of unique ptrs C++11 You don t have to worry about deletion if you use unique pointers: vector<unique_ptr<person>> upv; upv.push_back(unique_ptr<person>(new Person("Bob"))); for (const auto& upptr : upv) { cout << upptr->getname() << endl; Same advantages as vector of raw pointers, but you don t have to worry about deletion. But you must keep in mind that unique pointers cannot be copied, just moved. Shared pointers can naturally also be used. Arrays of Objects Everything becomes much more di vectors. Consider: cult if you use arrays instead of Person v[100]; // the size must be a compile-time constant, // the class must have a default constructor If the size isn t known at compile time, the array must be created on the heap and later deleted: size_t size = ; Person* pv = new Person[size]; delete[] pv; The brackets on delete informs the compiler that an array is deleted, rather than a single object. The compiler generates code to call the destructor for each object in the array. (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 143 / 281 (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 144 / 281

Dynamic Array of Pointers to Objects More Advanced Class Concepts If you use an array to store pointers to Person objects, and the array should be dynamic, you must do something like the following: Person** pv = new Person*[size]; // pointer to first element of // array, which is a pointer // to a Person object size_t nbrobj = 0; pv[nbrobj++] = new Person("Bob"); for (size_t i = 0; i!= nbrobj; ++i) { cout << pv[i]->getname() << endl; for (size_t i = 0; i!= nbrobj; ++i) { delete pv[i]; delete[] pv; Operator overloading Inheritance Templates (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 145 / 281 (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 146 / 281 Operator Overloading Rules for Overloading In most programming languages some operators are overloaded. For instance, the arithmetic operators are usually overloaded for integers and reals di erent addition operations are performed in the additions 1 + 2 and 1.0 + 2.0. The compiler uses the types of the operands to determine the correct operation. In C++, overloading can be extended to user-defined types (classes). Thus, a + b can mean addition of complex numbers, addition of matrices, etc. All operators can be overloaded except :: (scope resolution),. (member selection),.* (member selection through pointer to member),? :, sizeof. It is not possible to invent new operator symbols or to change the priority between operators. At least one of the operands of an overloaded operator must be an object (not a pointer to an object). So you cannot redefine the meaning of operators for built-in types, e.g., int + int. (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 147 / 281 (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 148 / 281

Operator Functions Example, Class Integer Each usage of an overloaded operator is translated into a call of an operator function. We have already seen one example, operator=. Operator functions are normal functions with strange names like operator=, operator+, Operator functions may be members or nonmembers. Rules: Assignment (=), subscript ([]), call (()), and member access arrow (->) must be members. Operators that change the state of their object should be members: +=, ++, Operators that don t change the state of any object should be nonmembers: +, *, ==, In the following, we will use a class Integer to demonstrate overloading. The class is merely a wrapper for integer values, but it could easily be extended, for example to perform modulo arithmetic. Or, add an attribute to create a Complex class. Integer(int v = 0) : value(v) { private: int value; ; The constructor is not explicit, so you can write Integer a = 5 as well as Integer a(5). (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 149 / 281 (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 150 / 281 Input and Output Implementing operator>> You should be able to read and write Integer objects just as other variables, like this: Integer a; cin >> a; // compiled into operator>>(cin, a); cout << a; // compiled into operator<<(cout, a); The functions operator>> and operator<< must be nonmembers (otherwise they would have to be members of the istream/ostream classes, and you cannot modify the library classes). The input stream should be an istream so input can come from a file stream or a string stream, the output stream should be an ostream. The functions must be friends of the Integer class, so they can access the private member value. In the file integer.h: friend istream& operator>>(istream& is, Integer& i); ; istream& operator>>(istream& is, Integer& i); In the file integer.cc: istream& operator>>(istream& is, Integer& i) { is >> i.value; return is; Comments on next slide. (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 151 / 281 (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 152 / 281

Comments, operator>> Implementing operator<< The operator function returns a reference to the stream so operations can be chained: cin >> a >> b; // operator>>(operator>>(cin, a), b) The stream parameter is passed by reference (it is changed by the function). The Integer object is passed by reference (it is changed by the function). (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 153 / 281 friend ostream& operator<<(ostream& os, const Integer& i); ; ostream& operator<<(ostream& os, const Integer& i) { os << i.value; return os; The function returns a reference to the stream so operations can be chained (cout << a << b). The stream is passed by reference (it is changed by the function). The Integer object is passed by constant reference (it shouldn t be copied and not changed by the function). The function doesn t do any extra formatting. (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 154 / 281 Compound Assignment and Arithmetic Operators Implementing operator+= The class Integer should implement the arithmetic operators (+, -, ), and to be consistent with other types also the compound assignment operators (+=, -=,... ). The arithmetic operators don t change the state of any object and should be non-members, the compound assignment operators change the state and should be members. Always start by implementing the compound assignment operators, then use them to implement the arithmetic operators. Example: Integer a, b, sum; a += b; // compiled into a.operator+=(b); sum = a + b; // sum = operator+(a, b); Integer& operator+=(const Integer& rhs) { value += rhs.value; return *this; ; Like operator=, the function returns a reference to the current object so operations can be chained (a += b += c). (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 155 / 281 (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 156 / 281

Implementing operator+ Increment, Prefix and Postfix Integer operator+(const Integer& lhs, const Integer& rhs) { Integer sum = lhs; // local non-constant object sum += rhs; // add rhs return sum; // return by value The function returns a new Integer object, not a reference (you cannot return a reference to a local object). The parameters are passed by constant reference (not copied and not changed). (Pass by value would be ok here since the objects are small, but we use constant reference for all objects to be consistent.) The function need not be a friend of Integer since it calls operator+=, which is a member function and has access to the private member value. ++x Increment x, returnthemodifiedvalue. x++ Make a copy of x, incrementx, return the copy. Since you have to copy an object, x++ is less e cient than ++x. Integer& operator++(); // prefix, ++x Integer operator++(int); // postfix, x++ ; Both functions have the same name; the postfix form has a dummy parameter (just an indicator, not used in the function). The prefix function returns a reference to the incremented value. The postfix function makes a copy of the object and returns the copy. (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 157 / 281 (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 158 / 281 Implementing operator++ Equality Operators Integer& Integer::operator++() { ++value; return *this; Integer Integer::operator++(int) { Integer ret = *this; ++*this; // use the prefix operator to do the work return ret; There is no need to give the dummy parameter to the postfix function aname. We could just as well have written ++value instead of ++*this in the second function. But ++*this has the same e ect and is better if incrementing is more complicated. friend bool operator==(const Integer& lhs, const Integer& rhs); ; bool operator==(const Integer& lhs, const Integer& rhs) { return lhs.value == rhs.value; bool operator!=(const Integer& lhs, const Integer& rhs) { return! (lhs == rhs); Global functions since they don t change any state. If you implement one of these you should implement both. Implement one in terms of the other. (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 159 / 281 (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 160 / 281

Subscripting Implementing operator[] We use the class String from slide 123 for the example on subscripting. class String { char& operator[](size_t index); const char& operator[](size_t index) const; private: char* chars; ; Note two versions, for non-const and const objects. Overloading on const is possible. char& String::operator[](size_t index) { return chars[index]; const char& String::operator[](size_t index) const { return chars[index]; It is essential that the non-const version returns a reference, so subscripting can be used on the left-hand side of an assignment. Here, the const version could just as well return a value, since the returned value is small. (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 161 / 281 (Roger.Henriksson@cs.lth.se) C++ Programming 2015/16 162 / 281