Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC/CMPE320 - Prof. McLeod

Similar presentations


Presentation on theme: "CISC/CMPE320 - Prof. McLeod"— Presentation transcript:

1 CISC/CMPE320 - Prof. McLeod
Winter 2013 CISC/CMPE320 2/17/2019 CISC/CMPE320 All Bitbucket projects and repositories have been created. Assignment 1 due today, 7pm. RAD due next Friday 7pm. See Wednesday’s lecture for more info. Fall 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod

2 CISC/CMPE320 - Prof. McLeod
Bitbucket If you are using a git client the URL will be in the form: Where is your project key and “####” is your team name in lower case. For example, for team Aberdeen: You will still need to supply your username and password to access your repository. Fall 2018 CISC/CMPE320 - Prof. McLeod

3 CISC/CMPE320 - Prof. McLeod
Bitbucket, Cont. Bitbucket should also tell you this URL as well. Atlassian (who wrote these tools) distributes a free git client called SourceTree that works well with Bitbucket. But this choice is up to you! For now, the team manager has admin privilege in Bitbucket and everyone else on the team has Read/Write privilege. The admin should be able to change these. This tool is integrated with Jira and Confluence. Fall 2018 CISC/CMPE320 - Prof. McLeod

4 CISC/CMPE320 - Prof. McLeod
Bitbucket, Cont. We will spend more time on repositories in general later in the course, when it is more likely that you will have code to store. But, in the meantime, don’t hesitate to play! And, give some thought as to how you will organize your repository. It is easier to start out organized that to try to do it later when you have lots of files. Fall 2018 CISC/CMPE320 - Prof. McLeod

5 CISC/CMPE320 - Prof. McLeod
Today Finish up function odds ‘n ends. Start Operator Overloading. Fall 2018 CISC/CMPE320 - Prof. McLeod

6 Back to Functions For a While
Continue with: Initializer Lists as parameters Lambda functions constexpr applied to functions C++11 C++11 C++11 C++14 Fall 2018 CISC/CMPE320 - Prof. McLeod

7 Initializer List Parameter Type
C++11 You can use std::initializer_list<type> as a parameter type to allow you to pass an initializer into a function. An initializer is a list of items of type contained in { }. #include <initializer_list> Already used by standard container classes like vector. See InitializerList.cpp. Fall 2018 CISC/CMPE320 - Prof. McLeod

8 CISC/CMPE320 - Prof. McLeod
Lambda Functions C++11 Wikipedia has a decent discussion on lambdas at: Also called “anonymous functions”, which they are. A function that is implemented without actually naming it. The basic syntax: [capture](parameters) -> returnType {function_body} Fall 2018 CISC/CMPE320 - Prof. McLeod

9 CISC/CMPE320 - Prof. McLeod
Lambda Functions, Cont. The capture part allows you to access variables that are scoped to where the lambda is created. You can capture stuff by value or by reference. The parameters section is just like a parameter list for a normal function. auto can be used as a parameter type in C++14. The returnType section is actually optional provided all return statements return the same type. Or, in C++14, you can use auto. The body of the function is contained within { }. Fall 2018 CISC/CMPE320 - Prof. McLeod

10 CISC/CMPE320 - Prof. McLeod
Lambda Functions, Cont. A lambda function can be assigned to a variable. The easiest thing to do is to specify the type of that variable as auto. See a simple example of a lambda function in SortAlgorithmTemplateExample.cpp. Fall 2018 CISC/CMPE320 - Prof. McLeod

11 constexpr Applied to Functions
This keyword can be used with a function or a class to tell the compiler that it is a compile time constant expression. A constant expression yields the same result during compilation as during run-time. For example, this function is a constant expression: int getFortyTwo() { return 42; } C++11 C++14 Fall 2018 CISC/CMPE320 - Prof. McLeod

12 constexpr Applied to Functions, Cont.
By stating instead: constexpr int getFortyTwo() { return 42; } The compiler will now evaluate this function and substitute the constant into any code that invokes this function. Results in “code optimization”, which is a big deal in C++. Can be used for an entire class or when declaring float or double constants. Fall 2018 CISC/CMPE320 - Prof. McLeod

13 constexpr Applied to Functions, Cont.
The function cannot be a void return function. In C++11 you could only have declarations and a single return statement. Now, in C++14, you can also have loops and conditionals. Fall 2018 CISC/CMPE320 - Prof. McLeod

14 Operator Overloading – Java
You should remember that the binary + operator is overloaded in Java: is 5 “two” + “three” is “twothree” 2 + “three” is “2three” “two” 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 2018 CISC/CMPE320 - Prof. McLeod

15 + 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 2018 CISC/CMPE320 - Prof. McLeod

16 + 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 2018 CISC/CMPE320 - Prof. McLeod

17 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 2018 CISC/CMPE320 - Prof. McLeod

18 Operators Overloaded in the string Class
From the STL reference in cplusplus.com: = [ ] += + ==, !=, <, >, <=, >= >> << Consult the docs to see what types are allowed on the lhs and rhs of overloaded binary operators. Fall 2018 CISC/CMPE320 - Prof. McLeod

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

20 Overloadable Operators, Cont.
These include unary operators, binary operators and both bitwise shift and stream insertion operators. 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 2018 CISC/CMPE320 - Prof. McLeod

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

22 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 2018 CISC/CMPE320 - Prof. McLeod

23 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 2018 CISC/CMPE320 - Prof. McLeod

24 Summary of MyComplex, So Far
Mixed type expressions (using your object) need conversion constructors, that will be invoked automatically to match types before the operator is evaluated. If you are going to use your binary operators in mixed type expressions it is best to overload them as non-member functions. If you do not wish to provide accessors, then non-member functions will have to be declared as friends. So, we know how to overload the binary arithmetic operators and <<. Fall 2018 CISC/CMPE320 - Prof. McLeod

25 Operator Functions, Cont.
Member overloading functions: For example, overloading the + operator for an object called MyClass: MyClass operator+(const MyClass& rhs) const; The object owning the function is the LHS operand in this case. Fall 2018 CISC/CMPE320 - Prof. McLeod

26 Member or Non-Member Overloading?
You will probably need to use both kinds. For example, assignment operators must be member functions, because you need to modify the LHS. Non-member functions cannot access private things in the class, unless they are a friend function. Fall 2018 CISC/CMPE320 - Prof. McLeod


Download ppt "CISC/CMPE320 - Prof. McLeod"

Similar presentations


Ads by Google