Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting"— Presentation transcript:

1 CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
Department of Computer Engineering San Jose State University Fall 2017 Instructor: Ron Mak

2 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 < ; i++) v.push_back(i); }

3 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 <= ; 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

4 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; }

5 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));

6 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);

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

8 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);

9 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); }

10 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; }

11 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);

12 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());

13 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;

14 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

15 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.

16 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.

17 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

18 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!

19 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

20 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?

21 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

22 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?

23 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; };

24 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;

25 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!

26 The “Big Five” The Big Three is now the Big Five: Add: move constructor move assignment operator


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

Similar presentations


Ads by Google