CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting

Slides:



Advertisements
Similar presentations
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Advertisements

Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
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.
11 Introduction to Object Oriented Programming (Continued) Cats.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
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.
1 Introduction to Object Oriented Programming Chapter 10.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Motivation for Generic Programming in C++
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Chapter 13: Overloading and Templates
Chapter 6 CS 3370 – C++ Functions.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CMPE 135: Object-Oriented Analysis and Design October 3 Class Meeting
OBJECT ORIENTED PROGRAMMING
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.
Motivation and Overview
Programming with ANSI C ++
Chapter 1 C++ Basics Review
Operator overloading Conversions friend inline
CMPE Data Structures and Algorithms in C++ December 7 Class Meeting
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
CMPE Data Structures and Algorithms in C++ October 19 Class Meeting
CMPE Data Structures and Algorithms in C++ November 9 Class Meeting
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
CMPE 180A Data Structures and Algorithms in C++ February 15 Class Meeting Department of Computer Engineering San Jose State University Spring 2018 Instructor:
Templates.
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
CMPE Data Structures and Algorithms in C++ May 10 Class Meeting
CMPE 180A Data Structures and Algorithms in C++ March 8 Class Meeting
Introduction to Classes
Chapter 5 Function Basics
Classes, Encapsulation, Methods and Constructors (Continued)
CMPE 135: Object-Oriented Analysis and Design November 27 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor:
Namespaces How Shall I Name Thee?.
COP 3330 Object-oriented Programming in C++
9-10 Classes: A Deeper Look.
CMSC 341 C++ and OOP.
CS 144 Advanced C++ Programming January 31 Class Meeting
CMSC 202 Lesson 6 Functions II.
CS 144 Advanced C++ Programming February 12 Class Meeting
CS 144 Advanced C++ Programming February 21 Class Meeting
Lesson 25 Miscellaneous Topics
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Class rational part2.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CMPE 152: Compiler Design April 18 – 30 Labs
CMSC 341 C++ and OOP.
CS 144 Advanced C++ Programming March 21 Class Meeting
CS 144 Advanced C++ Programming April 11 Class Meeting
CS 144 Advanced C++ Programming April 30 Class Meeting
9-10 Classes: A Deeper Look.
CS 144 Advanced C++ Programming April 30 Class Meeting
(4 – 2) Introduction to Classes in C++
SPL – PS3 C++ Classes.
Data Structures & Programming
Introduction to Classes and Objects
Presentation transcript:

CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor: Ron Mak www.cs.sjsu.edu/~mak

chrono #include <iostream> #include <vector> TimeVector.cpp #include <iostream> #include <vector> #include <chrono> using namespace std; using namespace std::chrono; void initialize_vector(vector<int> v) { for (int i = 0; i < 10000000; i++) v.push_back(i); }

chrono, cont’d #include <iostream> #include <iomanip> #include <vector> #include <chrono> using namespace std; using namespace std::chrono; long time_vector_initialization(vector<int> v, int n); int main() {     vector<int> v;     for (long n = 10000; n <= 100000000; n *= 10)     {         long elapsed_time = time_vector_initialization(v, n);         cout << "Elapsed_time for " << setw(9) << n << " : "              << setw(4) << elapsed_time << " ms" << endl;     } } TimeVector.cpp

chrono, cont’d long time_vector_initialization(vector<int> v, int n) {     steady_clock::time_point start_time = steady_clock::now();     v.clear();     for (int i = 0; i < n; i++) v.push_back(i);     steady_clock::time_point end_time = steady_clock::now();     // Other options include: nanoseconds, microseconds     long elapsed_time =             duration_cast<milliseconds>(end_time - start_time).count();     return elapsed_time; }

Function Parameters You can pass a function as a parameter to another function. The function that you called (the one that received a function parameter) can then call the function that it was passed. The formal parameter for the passed function must have the function signature. Example: long timed_test(SortedVector& sv, const int size,                 void f(SortedVector& sv, const int size));

Function Parameters, cont’d long timed_test(SortedVector& sv, const int size,                 void f(SortedVector& sv, const int size)); In this example, function timed_test can call the function that it was passed: f(sv, size);

Example: Timing Functions A program that compares the timings of vectors and linked lists performing similar operations, such as node insertions and deletions.

Example: Timing Functions, cont’d Functions to invoke and time: void vector_prepends(SortedVector& sv, const int size); void list_prepends  (SortedList&   sl, const int size); void vector_appends (SortedVector& sv, const int size); void list_appends   (SortedList&   sl, const int size); void vector_gets    (SortedVector& sv, const int size) throw (string); void list_gets      (SortedList&   sl, const int size) throw (string); void vector_removes (SortedVector& sv, const int size); void list_removes   (SortedList&   sl, const int size); void vector_inserts (SortedVector& sv, const int size); void list_inserts   (SortedList&   sl, const int size);

Example: Timing Functions, cont’d Run the test suite. void run_test_suite() throw (string) {     run_test_functions("Prepend", vector_prepends, list_prepends);     run_test_functions("Append",  vector_appends,  list_appends);     run_test_functions("Get",     vector_gets,     list_gets);     run_test_functions("Remove",  vector_removes,  list_removes);     run_test_functions("Insert",  vector_inserts,  list_inserts); }

void run_test_functions(const string test_name,                         void fv(SortedVector& sv, const int size),                         void fl(SortedList& sl, const int size))     throw (string) { ...     // Loop over the data sizes for the tests.     for (int size : SIZES)     {         ...         // Run and time the vector test.         SortedVector sv;         long etv = timed_test(sv, size, fv);         // Run and time the linked list test.         SortedList sl;         long etl = timed_test(sl, size, fl);     }     cout << endl; }

long timed_test(SortedVector& sv, const int size,                 void f(SortedVector& sv, const int size)) throw (string) {     steady_clock::time_point start_time = steady_clock::now();     f(sv, size);     steady_clock::time_point end_time = steady_clock::now();     return duration_cast<milliseconds>(end_time - start_time).count(); } long timed_test(SortedList& sl, const int size,                 void f(SortedList& sl, const int size)) throw (string)     f(sl, size);

The auto Keyword In a declaration of a variable that is also being initialized, the compiler can infer the type of the variable from the initialization expression. type inference, AKA type determination Use auto instead of a complicated type name. Examples: Instead of: Use: vector<int>::iterator current = a_container.begin(); map<string, DistanceToCity>::iterator p = cities.lower_bound(new_city.get_name()); auto current = a_container.begin(); auto p = cities.lower_bound(new_city.get_name());

The decltype Pseudo-Function Takes a variable as an argument. Returns the type associated with the variable. Create another variable with the same type. Ensure that two variables have the same type. Example: map<string, int>::iterator start_point; decltype(start_point) end_point;

Function Definitions in Header Files If a member function is small, such as a constructor, a getter, or a setter, you can put its definition inside the class declaration. You can have definition code in the header file. Example: class InlineTest { public:     InlineTest(int v) : value(v) {}     int get_value() const { return value; }     void set_value(const int v) { value = v; } private:     int value; } InlineTest1.cpp

Function Definitions in Header Files, cont’d When you put a member function definition inside the class declaration, the compiler can inline the function code. Instead of compiling a member function call the usual way, the compiler will instead insert the function code in place of the call. This will speed execution (since no calls are executed) but increase the size of the compiled code.

The inline Keyword If you define a member function outside of the class declaration, you can use the inline keyword to ask the compiler to inline the function code. The compiler may ignore the keyword. It’s usually better to let the compiler decide what’s best for code optimization.

The inline Keyword, cont’d class InlineTest2 { public:     InlineTest2(int v) : value(v) {}     int get_value() const;     void set_value(const int v); private:     int value; }; inline int InlineTest2::get_value() const { return value; } inline void InlineTest2::set_value(const int v) { value = v; } InlineTest2.cpp

The “Big Three” Collectively called the “big three” of a class: overloaded assignment operator copy constructor destructor Rule of thumb: If you define a destructor, you should also define a copy constructor and an overloaded assignment operator. Ensure that all three perform in a similar fashion. Do not rely on the default implementations!

The “Big Three”, cont’d Why does this code crash? class Array1 { public:     Array1(int s, int v[]);     ~Array1(); private:     int size;     int *vals; }; Array1::Array1(int s, int v[])     size = s;     vals = new int[size];     std::copy(v, v + size, vals); } Array1.cpp

The “Big Three”, cont’d Why does this code crash? cont’d Array1::~Array1() {    delete[] vals;    vals = nullptr; } int main()    int vals[4] = { 1, 2, 3, 4 };    Array1 a1(4, vals);    Array1 a2(a1);    cout << "Done!" << endl;    return 0; Array1.cpp What happens when array a2 goes out of scope?

The “Big Three”, cont’d Explicitly define a copy constructor: class Array2 { public:     Array2(int s, int v[]);     Array2(const Array2& a);     ~Array2(); private:     int size;     int *vals; }; Array2::Array2(const Array2 &a)     size = a.size;     vals = new int[a.size];     std::copy(a.vals, a.vals + size, vals); } Array2.cpp

The “Big Three”, cont’d Array2.cpp int main() {     int vals[4] = { 1, 2, 3, 4 };     Array2 a1(4, vals);     Array2 a2(a1);     a1 = a2;    cout << "Done!" << endl;     return 0; } What happens when array a1 goes out of scope?

The “Big Three”, cont’d Overload the assignment operator: Array3.cpp class Array3 { public:     Array3(int s, int v[]);     Array3(const Array3& a);     ~Array3();     Array3& operator =(const Array3& a); private:     int size;     int *vals; };

The “Big Three”, cont’d What’s missing? Array3& Array3::operator =(const Array3 &a) {     size = a.size;     vals = new int[a.size];     std::copy(a.vals, a.vals + size, vals);     return *this; } What’s missing? What happens with an assignment like a = a;

The “Big Three”, cont’d Array3& Array3::operator =(const Array3 &a) {     if (&a != this)     {         size = a.size;         vals = new int[a.size];         std::copy(a.vals, a.vals + size, vals);     }     return *this; } Array3.cpp This concludes the Big Three! http://www.technical-recipes.com/2011/the-big-three-in-c/

The “Big Five” The Big Three is now the Big Five: http://www.cppsamples.com/common-tasks/rule-of-five.html Add: move constructor move assignment operator