CS 144 Advanced C++ Programming April 11 Class Meeting

Slides:



Advertisements
Similar presentations
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Advertisements

Getting Started The structure of a simple wxWidgets program, Look at where and how a wxWidgets application starts and ends, how to show the main window,
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Generic Programming and Library Design Brian Bartman
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Operator Overloading CS 3370 – C++ Chapter 14.
C++ Lesson 1.
Variables, Identifiers, Assignments, Input/Output
Chapter 9: Value-Returning Functions
LESSON 06.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 6 CS 3370 – C++ Functions.
CMPE 135: Object-Oriented Analysis and Design September 26 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
CMPE Data Structures and Algorithms in C++ September 14 Class Meeting
C++ Templates.
CMPE 135: Object-Oriented Analysis and Design October 3 Class Meeting
Predefined Functions Revisited
Basic Elements of C++.
Iterative Constructs Review
Chapter 5 Function Basics
CMPE Data Structures and Algorithms in C++ December 7 Class Meeting
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
Pointers and Pointer-Based Strings
Subroutines in Computer Programming
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
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:
CMPE 152: Compiler Design February 6 Class Meeting
Basic Elements of C++ Chapter 2.
CMPE Data Structures and Algorithms in C++ May 10 Class Meeting
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
CMPE 180A Data Structures and Algorithms in C++ February 1 Class Meeting Department of Computer Engineering San Jose State University Spring 2018 Instructor:
Random Number Generation
Chapter 5 Function Basics
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Name: Rubaisha Rajpoot
Chapter 4 Implementing Free Functions
Variables, Identifiers, Assignments, Input/Output
CMPE 135: Object-Oriented Analysis and Design November 27 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor:
Wednesday 09/23/13.
Computing Fundamentals
Operator Overloading.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Pointers and Pointer-Based Strings
CS 144 Advanced C++ Programming February 5 Class Meeting
CS 144 Advanced C++ Programming February 12 Class Meeting
CMPE 135: Object-Oriented Analysis and Design March 19 Class Meeting
CMPE 152: Compiler Design February 28 / March 5 Lab
CS 144 Advanced C++ Programming January 31 Class Meeting
CS 144 Advanced C++ Programming February 12 Class Meeting
CS 144 Advanced C++ Programming February 21 Class Meeting
CS 144 Advanced C++ Programming March 28 Class Meeting
CMPE 135: Object-Oriented Analysis and Design March 14 Class Meeting
CS 144 Advanced C++ Programming April 25 Class Meeting
CS 144 Advanced C++ Programming April 9 Class Meeting
CS 144 Advanced C++ Programming March 21 Class Meeting
Programming Fundamental
CS 144 Advanced C++ Programming April 16 Class Meeting
CS 144 Advanced C++ Programming April 30 Class Meeting
CMPE 152: Compiler Design March 7/12 Lab
Presentation transcript:

CS 144 Advanced C++ Programming April 11 Class Meeting Department of Computer Engineering San Jose State University Spring 2019 Instructor: Ron Mak www.cs.sjsu.edu/~mak

wx-RPS: Linux

wx-RPS: Mac OS X

wx-RPS: Windows 10 Why does the app look so ugly on Windows?

Review: Inversion of Control The software framework controls the execution flow, not the programmer. The programmer registers callback functions, mostly as event handlers, with the framework. Bind an event handler to a control, such as a button. The framework invokes a callback function at the appropriate time. Example: When an event occurs (such as a button click by the user), call the event handler that’s bound to the event.

wxWidgets Button Event Handlers Create the buttons: Bind the event handlers: wxButton *rock_button     = new wxButton(button_panel, wxID_ANY,                                          choice_to_wxString(ROCK)); wxButton *paper_button    = new wxButton(button_panel, wxID_ANY,                                          choice_to_wxString(PAPER)); wxButton *scissors_button = new wxButton(button_panel, wxID_ANY,                                          choice_to_wxString(SCISSORS)); rock_button->Bind    (wxEVT_BUTTON, &ButtonPanel::on_rock, this); paper_button->Bind   (wxEVT_BUTTON, &ButtonPanel::on_paper, this); scissors_button->Bind(wxEVT_BUTTON, &ButtonPanel::on_scissors, this); void ButtonPanel::on_rock(wxCommandEvent& event) {     update_button_choice_text(ROCK); }

wxWidgets Menu Event Handlers Menu item identifiers: Bind with an event table: enum {     ID_Hello    = 1,     ID_Bonjour  = 2,     ID_GutenTag = 3 }; wxBEGIN_EVENT_TABLE(HelloWorldFrame, wxFrame)     EVT_MENU(ID_Hello,    HelloWorldFrame::OnHello)     EVT_MENU(ID_Bonjour,  HelloWorldFrame::OnBonjour)     EVT_MENU(ID_GutenTag, HelloWorldFrame::OnGutenTag)     EVT_MENU(wxID_ABOUT,  HelloWorldFrame::OnAbout)     EVT_MENU(wxID_EXIT,   HelloWorldFrame::OnExit) wxEND_EVENT_TABLE() void HelloWorldFrame::OnBonjour(wxCommandEvent& event) {     wxMessageBox( "Bonjour, monde!",                   "OnBonjour", wxOK | wxICON_INFORMATION ); }

wxWidgets Sample Programs Examine the sample programs to answer “how to” questions. Example: How do you prompt for and get a integer value from the user? This can be done using a dialog box. Therefore, look at sample program dialogs. Dialogs.cpp (lines 977-980) long res = wxGetNumberFromUser( wxT("This is some text, actually a lot of text.\n") wxT("Even two rows of text."), wxT("Enter a number:"), wxT("Numeric input test"), 50, 0, 100, this );

wxWidgets Sample Programs, cont’d

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: steady_clock::time_point start_time = steady_clock::now(); auto start_time = steady_clock::now(); From the initialization expression steady_clock::now() the compiler can infer that start_time has type steady_clock::time_point.

The decltype Pseudo-Function decltype takes a variable as an argument. Returns the type associated with the variable. Use to create another variable with the same type. Ensure that two variables have the same type. Example: auto start_time = steady_clock::now(); ... decltype(start_time) end_time = steady_clock::now();

Function Objects A function object is an instance of a class that overloads the function call operator. The function call operator is the pair of parentheses () That’s the operator used to call a function. Yes, you can overload the function call operator! Example: A class to generates pseudo-random integer values within a given range. The function call returns the next integer value.

Function Object: Random Integers RandomInt.h #include <ctime> using namespace std; class RandomInt { public:     RandomInt(int min, int max) : min(min), max(max)     {         srand(time(NULL));  // seed the random number generator     }     int operator()(); private:     int min, max; }; inline int RandomInt::operator()()     return min + rand()%(max - min + 1); }

Function Object: Random Integers, cont’d RandomIntTester.cpp #include <iostream> #include "RandomInt.h" int main() {     RandomInt ri(5, 12);     for (int i = 0; i < 10; i++) cout << ri() << endl;     return 0; } 7 11 8 10 12 5

Function Objects, cont’d A function object is an object, so therefore, You can store it in a variable. You can pass it as an argument to a function. You can return it as the value of a function. A function object can maintain state across multiple invocations. Stored in its member variables.

Function Object: Summation A function object can hold state. Summation.h class Summation { public:     Summation() : sum(0) {}     int operator ()(int amount); private:     int sum; }; inline int Summation::operator()(int amount)     sum += amount;     return sum; }

Function Object: Summation, cont’d #include <iostream> #include <iomanip> #include "Summation.h" using namespace std; int main() {     Summation adder;     for(int i = 0; i < 10; i++)     {         cout << "Add " << i << ": " << setw(2) << adder(i) << endl;     }     return 0; } Add 0:  0 Add 1:  1 Add 2:  3 Add 3:  6 Add 4: 10 Add 5: 15 Add 6: 21 Add 7: 28 Add 8: 36 Add 9: 45

Regular Expressions Use regular expressions to search strings for substrings that match various patterns. Common patterns: Big C++, 2nd edition by Cay Horstmann and Timothy Budd John Wiley & Sons, 2005

Regular Expression Example #include <iostream> #include <string> #include <regex> using namespace std; int main() {     string str = "This new subject is fun";     regex pattern("This (.*)(sub)(.*) is fun");     smatch result;     if (regex_search(str, result, pattern))     {         for (int i = 0; i < result.size(); i++)         {             cout << result[i] << endl;         }     }     return 0; } This new subject is fun new  sub ject

Lambda Expressions Person.h #include <string> using namespace std; enum class Gender { M, F }; class Person { public:     Person(string f, string l, Gender g) : first(f), last(l), gender(g) {}     virtual ~Person() {}     string first;     string last;     Gender gender; };

Lambda Expressions, cont’d #include <iostream> #include <vector> #include "Person.h" vector<Person> init(); ostream& operator <<(ostream& outs, Person &p); vector<Person> match(const vector<Person> people, bool f(const Person &p)); bool is_male(const Person &p); bool is_C(const Person &p); int main() {     vector<Person> people = init();     vector<Person> males;     vector<Person> cs;     cout << "Males:" << endl;     males = match(people, is_male);     for (Person& p : males) cout << p << endl;     cout << endl << "Last name starts with C:" << endl;     cs = match(people, is_C);     for (Person& p : cs) cout << p << endl; } PersonTest1.cpp

Lambda Expressions, cont’d PersonTest1.cpp vector<Person> init() {     vector<Person> v;     v.push_back(Person("Ron",    "Mak",     Gender::M));     v.push_back(Person("Marie",  "Curie",   Gender::F));     v.push_back(Person("Agatha", "Cristie", Gender::F));     v.push_back(Person("Barack", "Obama",   Gender::M));     return v; } ostream& operator <<(ostream& outs, Person &p)     outs << "  {" << "first=" << p.first << ", last=" << p.last          << ", gender=" << (p.gender == Gender::F ? "F" : "M") << "}";     return outs; vector<Person> match(const vector<Person> people, bool f(const Person &p))     vector<Person> matches;     for (const Person& p : people) if (f(p)) matches.push_back(p);     return matches;

Lambda Expressions, cont’d bool is_male(const Person &p) {     return p.gender == Gender::M; } bool is_C(const Person &p)     return p.last[0] == 'C'; PersonTest1.cpp Males:   {first=Ron, last=Mak, gender=M}   {first=Barack, last=Obama, gender=M} Last name starts with C:   {first=Marie, last=Curie, gender=F}   {first=Agatha, last=Cristie, gender=F}

Lambda Expressions, cont’d #include <iostream> #include <vector> #include "Person.h" vector<Person> init(); ostream& operator <<(ostream& outs, Person &p); vector<Person> match(const vector<Person> people, bool f(const Person &p)); int main() {     vector<Person> people = init();     vector<Person> males;     vector<Person> cs;     cout << "Males:" << endl;     males = match(people, [] (const Person &p) -> bool                           {                               return p.gender == Gender::M;                           });     for (Person& p : males) cout << p << endl;     cout << endl << "Last name starts with C:" << endl;     cs = match(people, [] (const Person &p) -> bool                        {                            return p.last[0] == 'C';                        });     for (Person& p : cs) cout << p << endl; } PersonTest2.cpp