CISC/CMPE320 - Prof. McLeod

Slides:



Advertisements
Similar presentations
Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to.
Advertisements

OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
Review of C++ Programming Part II Sheng-Fang Huang.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Today: –Two simple binding examples. –Function Hiding.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (3 left – as of last night). Office hours.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Operator Overloading.
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading Introduction
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Introduction to C++ Systems Programming.
Object Oriented Programming COP3330 / CGS5409
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Class: Special Topics Copy Constructors Static members Friends this
CISC/CMPE320 - Prof. McLeod
Operator Overloading BCA Sem III K.I.R.A.S.
CISC/CMPE320 - Prof. McLeod
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
CISC/CMPE320 - Prof. McLeod
Operator Overloading; String and Array Objects
Operator Overloading; String and Array Objects
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Constructors and Destructors
Classes and Objects.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Submitted By : Veenu Saini Lecturer (IT)
Operator Overloading; String and Array Objects
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
CISC101 Reminders Assignment 3 due today.
SPL – PS3 C++ Classes.
Presentation transcript:

CISC/CMPE320 - Prof. McLeod Winter 2013 CISC/CMPE320 9/22/2018 CISC/CMPE320 Notices: Assignment 1 due this Friday at 7pm. The rest of the assignments have been moved ahead a week. Teamwork: You were busy yesterday! RAD will be due at the end of next week as a document in Confluence. I will discuss this document in class and there is more description on the group project page. Fall 2017 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod

Is Software Ever Finished? Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Today More About Functions Start Operator Overloading Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Back to Functions A few odds ‘n ends: Return types Overloading Variable scope static local variables Default arguments Pointers to functions Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Return Types If nothing is to be returned then use void as the return type. Never return a pointer or a reference to a variable that is local to the function! Use of the returned value will lead to “undefined behaviour”. (However, you can return a pointer to an object created on the heap, using the “new” keyword. – Later…) Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Function Overloading You can overload functions in the same class by changing the parameter list, not by changing the return type. Each overloaded function must still have its own prototype. You cannot overload parent member functions in a child class in C++. You will “shadow” or “hide” the functions instead! More later… Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Variable Scope A variable declared inside a function is local to that function. A variable declared outside any function or class definition is global in scope. Fall 2017 CISC/CMPE320 - Prof. McLeod

Aside – static Variables You can declare a local variable to be static inside a function. See StaticLocalTest.cpp Note that the static variable is only set to zero once. Look at the program in a debugger? Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod static Variables, Cont. This type of variable is placed in a special area of memory that holds all global variables and functions. This memory is allocated before your program even runs and is only emptied out when your program is finished. The size of this area of memory does not change. Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Default Arguments For example: int getVol(int len, int width = 1, int height = 1); This can be done by overloading alone. You can accept the default value or replace it. Default arguments must come after normal parameters, and cannot have a normal parameter mixed in the list. Note – do not assign default arguments again in the function implementation. See DefaultArguments.cpp Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Pointers to Functions You can define a pointer to a function. See FunctionsAsPointers.cpp Note that the return type and parameter list must match up. The example does not look useful, but you can also pass function pointers as parameters. The function could be a comparator, for example. Fall 2017 CISC/CMPE320 - Prof. McLeod

Pointers to Functions, Cont. This is mostly a C usage. There are better ways to do this including function templates and by taking advantage of polymorphism – this is all C++! See another example: SortAlgorithmTemplateExample.cpp modified from an example in cplusplus.com. Fall 2017 CISC/CMPE320 - Prof. McLeod

Operator Overloading – Java You should remember that the binary + operator is overloaded in Java: 2 + 3 is 5 “two” + “three” is “twothree” 2 + “three” is “2three” “two” + 3 is “two3” So the result of the operator depends on the type of the operands. If there is a String literal on one side then the other side is converted to a String and a concatenation takes place. Fall 2017 CISC/CMPE320 - Prof. McLeod

+ and string Objects in C++ string two("two"); string three("three"); string sum1 = two + three; // sum1 is "twothree" string sum2 = 2 + three; // doesn’t work Note that a string literal is actually a char* object and the + operator does not work with them if they are on both sides of the operator. Fall 2017 CISC/CMPE320 - Prof. McLeod

+ and string Objects in C++, Cont. However, something like: string two("two"); string sum1 = two + "five"; does work. The char* literal is converted to a string object first. So, the operation that takes place depends on the types of the operands on either side of the operator. Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Operator Overloading This flexibility is a result of operator overloading in the string class – a task carried out by the class designer to make the class easier to use. In Java operator overloading is almost non-existent. For example, if a, b and c are BigIntegers, to carry out the operation: x = a + b * c; you would have to write x = a.add(b.multiply(c)); Fall 2017 CISC/CMPE320 - Prof. McLeod

Overloadable Operators in C++ - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new new[] delete delete[] Fall 2017 CISC/CMPE320 - Prof. McLeod

Overloadable Operators, Cont. These include unary operators, binary operators and both bitwise shift and stream insertion operators (already overloaded!). An overloaded operator is only used when one or the other, or both, operands are of your class type. (ie. you cannot overload int + int – sorry…) You cannot create new operators. You cannot change associativity or the order of precedence. You cannot make a unary operator a binary one, or visa-versa. Fall 2017 CISC/CMPE320 - Prof. McLeod

Overloadable Operators, Cont. And, you only overload the ones that make sense for your class!!! Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Operator Functions You can overload an operator as a member or a non-member function. Overloading function header syntax: return_type operatoroperator_symbol(parameters) For example, non-member overloading the + operator for an object called MyClass: MyClass operator+(const MyClass& lhs, const MyClass& rhs); Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod MyComplex Class Demo See the start of a simple class to hold complex numbers: myComplex.h and myComplex.cpp: First version uses non-member overloading and accessors. Second version uses member overloading of the + operator. Third version uses non-member friends and no accessors. Fall 2017 CISC/CMPE320 - Prof. McLeod