Intermediate Programming, Spring 2017*

Similar documents
Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

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

Intermediate Programming, Spring 2017*

CS 247: Software Engineering Principles. ADT Design

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

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

And Even More and More C++ Fundamentals of Computer Science

UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading

Program construction in C++ for Scientific Computing

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Constructors.

Constants, References

ECE Fall 20l2, Second Exam

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

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Composition I. composition is a way to combine or compose multiple classes together to create new class

PIC 10A Objects/Classes

Intermediate Programming, Spring 2017*

IS0020 Program Design and Software Tools Midterm, Fall, 2004

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

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

Computer Science II CSci 1200 Lecture 18 Operators and Friends

A <Basic> C++ Course

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

A <Basic> C++ Course

Intermediate Programming, Spring 2017*

Programming Abstractions

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

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

Programming, numerics and optimization

Financial computing with C++

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

Implementing Abstract Data Types (ADT) using Classes

Abstract Data Types (ADT) and C++ Classes

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

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

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

11.2. Overloading Operators

Classes in C++98 and C++11

Comp151. Generic Programming: Container Classes

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

Come and join us at WebLyceum

Special Member Functions

The University of Nottingham

ECE 462 Fall 2011, Second Exam

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

Short Notes of CS201

Classes Ch

Intermediate Programming, Spring 2017*

C++ 8. Constructors and Destructors

CS11 Intro C++ Spring 2018 Lecture 5

Engineering Tools III: OOP in C++

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

Introduction Of Classes ( OOPS )

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

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

CS201 - Introduction to Programming Glossary By

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

ADTs & Classes. An introduction

G52CPP C++ Programming Lecture 13

COMP 2355 Introduction to Systems Programming

CE221 Programming in C++ Part 1 Introduction

Due Date: See Blackboard

Constructors and Destructors. Chapter 6

Inheritance and Overloading. Week 11

CS 162, Lecture 25: Exam II Review. 30 May 2018

Operator overloading

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

Assignment 4: SkipList

CS 247: Software Engineering Principles. Modules

Assignment of Structs

GEA 2017, Week 4. February 21, 2017

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

Object-Oriented Principles and Practice / C++

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

Operator overloading: extra examples

Protection Levels and Constructors The 'const' Keyword

Copy Constructor, Assignment, Equality

CS250 Final Review Questions

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

Advanced Systems Programming

TEMPLATE IN C++ Function Templates

Object-Oriented Programming. Lecture 4 Dr Piotr Cybula

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

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

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

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

ECE Fall 2014, Final Exam

Due Date: See Blackboard

Intermediate Programming, Spring 2017*

More on Functions. Lecture 7 COP 3014 Spring February 11, 2018

Midterm Review. PIC 10B Spring 2018

Object Orientated Analysis and Design. Benjamin Kenwright

Operator Overloading in C++ Systems Programming

CS250 Final Review Questions

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 The this keyword Overloading Implicitly defined constructors

The this keyword When defining a class method, we can get access to a pointer to the object that "owns" the method using the keyword this. foo.h class Foo int _i; void set( int i ) _i = i; int get( void ) const return this->_i; ;

Outline The this keyword Overloading Implicitly defined constructors

Overloading In C++, the compiler can distinguish between functions which have the same name but different numbers/types of parameters The compiler will use the argument types to determine which function to call main.cpp void PrintType( int ) cout << "int" << endl; void PrintType( float ) cout << "float" << endl; int main(void) PrintType( 1 ); PrintType( 1.f ); return 0; >>./a.out int float >>

Overloading In C++, the compiler can distinguish between functions which have the same name but different numbers/types of parameters The compiler will use the argument types to determine which function to call It cannot distinguish between functions based on their output type main.cpp int GetType( void ) return 1; float GetType( void ) return 1.f; int main(void) int i = GetType(); >> g++ -std=c++11 -Wall -Wextra float f = main.cpp GetType(); main.cpp: In function float return GetType() 0; : main.cpp:5:7: error: ambiguating new declaration of float GetType() float GetType ( void ) return 1.f; ^~~ >>

Overloading In C++, the compiler can distinguish between functions which have the same name but different numbers/types of parameters Similarly, it can distinguish between class methods which have the same name but different numbers/types of parameters class Foo main.cpp void printtype( int ) cout << "int" << endl; void printtype( float ) cout << "float" << endl; ; int main(void) Foo f; f.printtype( 1 ) ; f.printtype( 1.f ); return 0; >>./a.out int float >>

Overloading Some classes "naturally" define operators Using full-fledged names can get cumbersome and hard to read Point2D( float x, float y ); float x( void ) const return _v[0]; float y( void ) const return _v[1]; ; Point2D Add( Point2D p1, Point2D p2 ); Point2D Scale( Point2D p, float s ); #include "" main.cpp int main( void ) Point2D p(0,0), q(1,1); Point2D avg = Scale( Add(p,q), 0.5f ); cout << "( " << avg.x() << ", " << avg.y() << " )" << endl; return 0; >>./a.out ( 0.5, 0.5 ) >>

Overloading In C++, we can also overload operators: +, -, *, /, <,, &, [], ==,!=, <<, etc. Point2D( float x, float y ); float operator[] ( int i ) const return _v[i]; ; Point2D operator + ( Point2D p1, Point2D p2 ); Point2D operator - ( Point2D p1, Point2D p2 ); Point2D operator * ( Point2D p, float s ); Point2D operator / ( Point2D p, float s ); Point2D operator * ( float s, Point2D p ); #include "" main.cpp int main( void ) Point2D p(0,0), q(1,1); Point2D avg = ( p + q ) / 2; cout << "( " << avg[0] << ", " << avg[1] << " )" << endl; return 0; >>./a.out ( 0.5, 0.5 ) >>

Overloading We can also have class methods be operators The first argument is the object itself Point2D( float x, float y ); float operator[] ( int i ) const return _v[i]; Point2D operator + ( Point2D p ) const; Point2D operator - ( Point2D p ) const; Point2D operator * ( float s ) const; Point2D operator / ( float s ) const; ; Point2D operator * ( float s, Point2D p ); #include "" main.cpp int main( void ) Point2D p(0,0), q(1,1); Point2D avg = ( p + q ) / 2; cout << "( " << avg[0] << ", " << avg[1] << " )" << endl; return 0; >>./a.out ( 0.5, 0.5 ) >>

Overloading In terms of implementation: Point2D::Point( float x, float y ) _v[0] = x, _v[1] = y ; Point2D( float x, float y ); float operator[] ( int i ) const return _v[i]; Point2D operator + ( Point2D p ) const; Point2D operator - ( Point2D p ) const; Point2D operator * ( float s ) const; Point2D operator / ( float s ) const; ; Point2D operator * ( float s, Point2D p );

Overloading In terms of implementation: Point2D( float x, float y ); float operator[] ( int i ) const return _v[i]; Point2D operator + ( Point2D p ) const; Point2D operator - ( Point2D p ) const; Point2D operator * ( float s ) const; Point2D operator / ( float s ) const; ; Point2D operator * ( float s, Point2D p ); Point2D::Point( float x, float y ) _v[0] = x, _v[1] = y ; Point2D Point2D::operator + ( Point2D p ) const return Point2D( _v[0] + p._v[0], _v[1] + p._v[1] );

Overloading In terms of implementation: Point2D( float x, float y ); float operator[] ( int i ) const return _v[i]; Point2D operator + ( Point2D p ) const; Point2D operator - ( Point2D p ) const; Point2D operator * ( float s ) const; Point2D operator / ( float s ) const; ; Point2D operator * ( float s, Point2D p ); Point2D::Point( float x, float y ) _v[0] = x, _v[1] = y ; Point2D Point2D::operator + ( Point2D p ) const return Point2D( _v[0] + p._v[0], _v[1] + p._v[1] ); Point2D Point2D::operator * ( float s ) const return Point2D( _v[0] * s, _v[1] * s );

Overloading In terms of implementation: Point2D( float x, float y ); float operator[] ( int i ) const return _v[i]; Point2D operator + ( Point2D p ) const; Point2D operator - ( Point2D p ) const; Point2D operator * ( float s ) const; Point2D operator / ( float s ) const; ; Point2D operator * ( float s, Point2D p ); Point2D::Point( float x, float y ) _v[0] = x, _v[1] = y ; Point2D Point2D::operator + ( Point2D p ) const return Point2D( _v[0] + p._v[0], _v[1] + p._v[1] ); Point2D Point2D::operator * ( float s ) const return Point2D( _v[0] * s, _v[1] * s ); Point2D Point2D::operator - ( Point2D p ) const return (*this) + ( p * -1.f );

Overloading In terms of implementation: Point2D( float x, float y ); float operator[] ( int i ) const return _v[i]; Point2D operator + ( Point2D p ) const; Point2D operator - ( Point2D p ) const; Point2D operator * ( float s ) const; Point2D operator / ( float s ) const; ; Point2D operator * ( float s, Point2D p ); Point2D::Point( float x, float y ) _v[0] = x, _v[1] = y ; Point2D Point2D::operator + ( Point2D p ) const return Point2D( _v[0] + p._v[0], _v[1] + p._v[1] ); Point2D Point2D::operator * ( float s ) const return Point2D( _v[0] * s, _v[1] * s ); Point2D Point2D::operator - ( Point2D p ) const return (*this) + ( p * -1.f ); Point2D Point2D::operator / ( float s ) const return (*this) * (1.f/s);

Overloading In terms of implementation: Point2D( float x, float y ); float operator[] ( int i ) const return _v[i]; Point2D operator + ( Point2D p ) const; Point2D operator - ( Point2D p ) const; Point2D operator * ( float s ) const; Point2D operator / ( float s ) const; ; Point2D operator * ( float s, Point2D p ); Point2D::Point( float x, float y ) _v[0] = x, _v[1] = y ; Point2D Point2D::operator + ( Point2D p ) const return Point2D( _v[0] + p._v[0], _v[1] + p._v[1] ); Point2D Point2D::operator * ( float s ) const return Point2D( _v[0] * s, _v[1] * s ); Point2D Point2D::operator - ( Point2D p ) const return (*this) + ( p * -1.f ); Point2D Point2D::operator / ( float s ) const return (*this) * (1.f/s); Point2D operator * ( float s, Point2D p ) return p*s;

Overloading In terms of implementation: Point2D( float x, float y ); float operator[] ( int i ) const return _v[i]; Point2D operator + ( Point2D p ) const; Point2D operator - ( Point2D p ) const; Point2D operator * ( float s ) const; Point2D operator / ( float s ) const; ; Point2D operator * ( float s, Point2D p ); Point2D::Point( float x, float y ) _v[0] = x, _v[1] = y ; Point2D Point2D::operator + ( Point2D p ) const return Point2D( _v[0] + p._v[0], _v[1] + p._v[1] ); Point2D Point2D::operator * ( float s ) const return Point2D( _v[0] * s, _v[1] * s ); Point2D Point2D::operator - ( Point2D p ) const return (*this) + ( p * -1.f ); Point2D Point2D::operator / ( float s ) const return (*this) * (1.f/s); Point2D operator * ( float s, Point2D p ) return p*s; Note: In this implementation, we have opted for consistency over efficiency. (e.g. subtraction is implemented by first multiplying by -1 and then adding)

Overloading We would also like to support streaming output using the << operator This is a function that takes two arguments The output stream the object to be written And returns a reference to the output stream (so we can chain outputs) ; ostream& operator << ( ostream& os, Point2D p ) os << "( " << p[0] << ", " << p[1] << " )"; return os;

Overloading We would also like to support streaming output using the << operator This is a function that takes two arguments The output stream the object to be written And returns a reference to the output stream (so we can chain outputs) Using the friend keyword, we can give a function / operator access to the private class members friend ostream& operator << ( ostream& os, Point2D p ); ; ostream& operator << ( ostream& os, Point2D p ) os << "( " << p._v[0] << ", " << p._v[1] << " )"; return os;

Overloading Operator overloading allows us to write succinct, but still readable, code #include "" main.cpp int main( void ) Point2D p(0,0), q(1,1); Point2D avg = Scale( Add(p,q), 0.5f ); cout << "( " << avg.x() << ", " << avg.y() << " )" << endl; return 0; main.cpp #include "" int main( void ) Point2D p(0,0), q(1,1); cout << ( p + q ) / 2 << endl; return 0;

Outline The this keyword Overloading Implicitly defined constructors

Implicitly defined constructors A default constructor is a constructor that takes no arguments It is called when an object is created without an argument list Point2D( void ) _v[0] = _v[1] = 0; ; main.cpp #include "" int main( void ) Point2D p; return 0;

Implicitly defined constructors A default constructor is a constructor that takes no arguments It is called when an object is created without an argument list Point2D( void ) _v[0] = _v[1] = 0; ; main.cpp #include "" int main( void ) Point2D p; return 0; You want to provide your own default constructor if the members require special initialization: They are plain-old-data that won't be initialized They need to be constructed in a non-default way

Implicitly defined constructors If no constructor is provided, C++ will implicitly define a default constructor: It will be public It will call the default constructor for all members that have one (i.e. all members that are not plain-old-data types)

Implicitly defined constructors A copy constructor is a constructor that takes a single argument -- the object from which to copy the member data It is called when: An object is initialized An object is passed as an argument to a function An object is returned by a function call Point2D( const Point2D& p ) _v[0] = p._v[0], _v[1] = p._v[1]; ; main.cpp #include "" int main( void ) Point2D p(1,1); Point2D q(p); Point2D r = q; return 0;

Implicitly defined constructors A copy constructor is a constructor that takes a single argument -- the object from which to copy the member data It is called when: An object is initialized An object is passed as an argument to a function An object is returned by a function call Point2D( const Point2D& p ) _v[0] = p._v[0], _v[1] = p._v[1]; ; You want to provide your own copy constructor when you need to perform a deep copy. main.cpp #include "" int main( void ) Point2D p(1,1); Point2D q(p); Point2D r = q; return 0;

Implicitly defined constructors If not provided, C++ will also implicitly declare a copy constructor: It will be public It will copy the values of the argument's members into the members of the object being constructed

Implicitly defined constructors An assignment operator is an operator that assigns the object's members using the members of another object It is called when the = sign is used Point2D& operator = ( const Point2D& p ) _v[0] = p._v[0], _v[1] = p._v[1]; ; main.cpp #include "" int main( void ) Point2D p(1,1); Point2D q(p), r; r = q; return 0;

Implicitly defined constructors An assignment operator is an operator that assigns the object's members using the members of another object It is called when the = sign is used Point2D& operator = ( const Point2D& p ) _v[0] = p._v[0], _v[1] = p._v[1]; ; You want to provide your own assignment operator when you need to perform a deep copy. main.cpp #include "" int main( void ) Point2D p(1,1); Point2D q(p), r; r = q; return 0;

Implicitly defined constructors If not provided, C++ will also implicitly declare an assignment operator: It will be public It will copy the values of the argument's members into the members of the object being constructed

Implicitly defined constructors Note: If a variable is declared and assigned at the same time, the constructor is used If it's declared first and assigned later, assignment is used main.cpp Point2D( const Point2D& p ); ; Point2D& operator = ( const Point2D& p ); #include "" int main( void ) Point2D p(1,1); Point2D q = p, r; r = p; return 0;

Final Project