Operator overloading Conversions friend inline

Slides:



Advertisements
Similar presentations
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Advertisements

A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
Plab – Exercise 5 C++: references, operator overloading, friends.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
OOP Languages: Java vs C++
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
Chapter 14 More About Classes. Chapter 13 slide 2 Topics 13.1 Instance and Static Members 13.2 Friends of Classes 13.3 Memberwise Assignment 13.4 Copy.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Memory Management.
Yan Shi CS/SE 2630 Lecture Notes
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
Chapter 14: More About Classes.
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
Operator overloading Conversions friend inline
Operator overloading Conversions friend inline
Introduction to Classes
The dirty secrets of objects
Chapter 14: More About Classes.
Introduction to Classes
understanding memory usage by a c++ program
Automatics, Copy Constructor, and Assignment Operator
Classes and Data Abstraction
CS212: Object Oriented Analysis and Design
Andy Wang Object Oriented Programming in C++ COP 3330
COP 3330 Object-oriented Programming in C++
C++ Constructor Insanity CSE 333 Summer 2018
Java Programming Language
Andy Wang Object Oriented Programming in C++ COP 3330
COP 3330 Object-oriented Programming in C++
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lecture 8 Object Oriented Programming (OOP)
(4 – 2) Introduction to Classes in C++
SPL – PS3 C++ Classes.
Object Oriented Programming
Presentation transcript:

Operator overloading Conversions friend inline .

Operator Overloading Operators like +, - , * , are actually methods, and can be overloaded. Syntactic sugar.

What is it good for - 1 Natural usage. compare: a.set( add(b,c) ) to a= b+c v.elementAt(i)= 3 v[i]= 3

What is it good for - 2 Semantic integrity. A rule of thumb: When you need to make a deep copy of an object, you need to define all of these: Copy constructor Destructor Operator = Or in other words: when you need one, you need all.

What is it good for - 3 a and b can be primitives or Uniformity with base types (important for templates) template<typename T> const T& max(const T& a, const T& b) { return a<b ? a : b; } a and b can be primitives or user defined objects that have operator <

Rules Don't overload operators with non-standard behavior! (<< for adding,...) Check how operators work on primitives or in the standard library and give the same behavior in your class.

Example of usage in primitives/standard library >> << are used as bit operations for primitives numbers and for I/O in the standard library iostreams classes. [] is used as subscripting primitives arrays and vector class in the standard library. () is used for function calls and for functor objects in the standard library.

Prototype X& operator=(const X& rval) parameter for object on right side of operator return type method name

Invoking an Overloaded Operator Operator can be invoked as a member function: object1.operator=(object2); It can also be used in more conventional manner: object1= object2;

A skeleton for deep copy // Copy constructor A (const A& other) { // init // copy other } // Operator = A& operator=(const A& other) { if (this!=&other) { // preventing problems in a=a // clear } return *this; } // allows a= b= c= … // Destructor ~A() { // clear }

C++-11 Move ctor and assignment (not learned in this class) // Move constructor A (const A&& other) { ? } // Move operator = A& operator=(const A&& other) { http://www.cprogramming.com/c++11/rvalue- references-and-move-semantics-in-c++11.html

List & Complex examples

Operators ++ -- postfix prefix // Prefix: ++n HNum& operator++() { code that adds one to this HNum return *this; // return ref to curr } // Postfix : n++ const HNum operator++(int) { Hnum cpy(*this); // calling copy ctor return cpy; A flag that makes it postfix 13

Operators ++ -- postfix prefix // Prefix: ++n HNum& operator++() { code that adds one to this HNum return *this; // return ref to curr } // Postfix : n++ const HNum operator++(int) { Hnum cpy(*this); // calling copy ctor return cpy; // For HNum, it might be a good idea not to // implement postfix A flag that makes it postfix 14

Conversions of types is done in two cases: Explicit casting (we'll learn more about it in next lessons) 15

Conversions of types is done in two cases: Explicit casting (we'll learn more about it in next lessons) When a function gets X type while it was expecting to get Y type, and there is a casting from X to Y: void foo(Y y) ... X x; foo(x); // a conversion from X to Y is done 16

Conversion example (conv.cpp) 17

Conversions danger: unexpected behavior std::vector(unsigned int length) // ctor … void foo(const std::vector& v) // function ... foo(3) // Did the user really wanted to send a vector with 3 cells (all with value of 0) ? vector and an unsigned int are not logically the same object ! 18

Conversion example (conv_explicit.cpp) 19

User defined conversion class Fraction { ... // double --> Fraction conversion Fraction (const double& d) { } // Fraction --> double conversion operator double() const { 20

friend 21

friend functions Friend function in a class: Not a method of the class Have access to the class’s private and protected data members Defined inside the class scope Used properly does not break encapsulation 22

friend functions example: Complex revisited 23

friend classes A class can allow other classes to access its private data members The friendship is one sided 24

friend classes - example class IntTree { … friend class IntTreeIterator; }; // TreeIterator can access Tree's data members IntTreeIterator& IntTreeIterator::operator++() { ... return *this; } 25

Inline functions / methods 26

Inline functions / methods A hint to a compiler to put function’s code inline, rather than perform a regular function call. Objective: improve performance of small, frequently used functions. An inline function defined in .cpp file is not recognized in other source files. 27

C vs C++ : macro vs inlining compare: define SQRT(x) ((x)*(x)) SQRT(i++) // unexpected behavior to inline int sqrt(int x) { return x*x; } sqrt(i++) // good behavior 28

Inline methods You can hint to the compiler that a method is inline in class declaration (inside the { }; block of a class): class Tree { ... size_t size() const{ // automatically hints on inline return _size; } }; 29

Inline methods You can hint to the compiler that a method is inline after class declaration: class Tree { ... size_t size() const; }; inline size_t Tree::size() const { // still in the h file return _size; } 30

Tradeoffs: Inline vs. Regular Functions / Methods Regular functions – when called, compiler stores return address of call, allocates memory for local variables, etc. 31

Tradeoffs: Inline vs. Regular Functions / Methods Regular functions – when called, compiler stores return address of call, allocates memory for local variables, etc. Code for an inline function is copied into program in place of call – can enlarge executable program, but no function call overhead, hence usually faster execution. 32

Tradeoffs: Inline vs. Regular Functions / Methods Regular functions – when called, compiler stores return address of call, allocates memory for local variables, etc. Code for an inline function is copied into program in place of call – can enlarge executable program, but no function call overhead, hence usually faster execution. Can enlarge compile time. You compile the inline function again and again in every place it's used. 33