Boston High School Education Project

Slides:



Advertisements
Similar presentations
Chapter 7: User-Defined Functions II
Advertisements

Chapter 14: Overloading and Templates
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Review of C++ Programming Part II Sheng-Fang Huang.
February 27, 2014CS410 – Software Engineering Lecture #8: C++ Basics II 1 The vector Container Type When a vector is created, its elements are initialized.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Learners Support Publications Classes and Objects.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
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.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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?
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 15 - C++ As A "Better C"
C++ Lesson 1.
Programming with ANSI C ++
User-Written Functions
Chapter 7: User-Defined Functions II
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?
C++ Templates.
Objectives In this chapter, you will:
Templates.
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.
Introduction to C++ Systems Programming.
Chapter 14 Templates C++ How to Program, 8/e
Review: Two Programming Paradigms
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?
Programming Fundamentals Lecture #7 Functions
CS212: Object Oriented Analysis and Design
6 Functions.
– Introduction to Object Technology
C++ for Engineers and Scientists Second Edition
Lecture 4-7 Classes and Objects
(5 - 1) Object-Oriented Programming (OOP) and C++
Chapter 5 - Functions Outline 5.1 Introduction
CS212: Object Oriented Analysis and Design
ADT Implementations: Templates and Standard Containers
Introduction to Classes
6 Chapter Functions.
Lecture 18 Arrays and Pointer Arithmetic
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Constructors and Other Tools
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
(5 - 1) Object-Oriented Programming (OOP) and C++
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
7 Arrays.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Defining Classes and Methods
Submitted By : Veenu Saini Lecturer (IT)
The vector Container Type
CS410 – Software Engineering Lecture #5: C++ Basics III
Predefined Functions Revisited
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS410 – Software Engineering Lecture #3: C++ Basics I
Class rational part2.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Boston High School Education Project Alina Wang, VP of BHSE  alinawang.bhse@gmail.com As an IRS approved 501(c)3 non-profit organization, Boston High School Education (BHSE) is dedicated to building a cross-culture platform to help Chinese-origined students (both those in the United States and those in China) to obtain the holistic, high quality education that best maximize their unique individual potentials. September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

Boston High School Education Project BHSE is a fast growing organization with currently more than seven thousand active members participating in its weekly seminars and discussions. Based on its strong footing in Boston, it is expanding rapidly into China and the rest of the world.  BHSE is seeking a webpage development team to augment its expansion and to better serve its global members. It is expected to work independently to design, build and maintain BHSE's website. The team should have basic database skills and ideally iOS and Android app development skills.  September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

Boston High School Education Project BHSE will provide the team with  1. an excellent entrepreneurial opportunity to work with some fantastic people to obtain valuable real world experiences in the red-hot global e-learning industry and to contribute to your community by helping people, 2. a strong recommendation letter and the critical technical skills that support your future career endeavors, and  3. a potential full-time job pending the organization's future funding and growth. September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Enumeration Types Sometimes you may want to define for your object a set of states or actions. For example, you could define the following states for a Student Admonisher Robot: observeStudent shoutAtStudent followStudent rechargeBattery September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Enumeration Types Using the const qualifier, you could define the following constants: const int observeStudent = 1; const int shoutAtStudent = 2; const int followStudent = 3; const int rechargeBattery = 4; A function SetRobotState could then be defined as follows: bool SetRobotState(int newState) { … int currentState = newState; return executionSuccessful; } September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Enumeration Types However, mapping states onto integers has certain disadvantages: You cannot restrain the range of values that are passed to SetRobotState. There is no useful typing – if you define individual sets of states for multiple objects, each object could formally be set to any of these states, not only its individual ones. This problem can be solved with enumeration types. September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Enumeration Types Enumeration types can be defined as follows: enum robotState {observeStudent = 1, shoutAtStudent, followStudent, rechargeBattery}; This way we defined a new type robotState that can only assume four different values. These values still correspond to integers. For example, cout << followStudent; gives you the output ‘3’. September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Enumeration Types However, we are now able to restrain the values that are passed to SetRobotState to the four legal ones: bool SetRobotState(robotState newState) { … robotState currentState = newState; return executionSuccessful; } Any attempt to call SetRobotState with an integer value or a value of a different enumeration type will cause an error at compile time. September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

The vector Container Type The vector class is an alternative to the standard array type in C++. Its implementation uses the C++ template facility. This facility allows programmers to build generic (polymorphic) classes, that is, classes that are defined independently of the type of data they hold and operate on. When you define such a class, you can use placeholders (parameters) instead of explicit type specifiers. You can then instantiate objects of that class for any compatible data type. September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

The vector Container Type To use a vector, we have to include the following header file: #include <vector> We can use vectors in two quite different ways: the array idiom the STL idiom (STL = Standard Template Library) September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

The vector Container Type The array idiom is very similar to the built-in array type. We specify the type and size of the array and then manipulate elements of the array in the usual way: void main() { int vecSize = 8; vector<int> intVec(vecSize); intVec[0] = 1; intVec[1] = 1; for (int i = 2; i < intVec.size(); i++) intVec[i] = intVec[i – 1] + intVec[i – 2]; cout << intVec[intVec.size() - 1]; } Output: ’21’ (Fibonacci) September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

The vector Container Type When a vector is created, its elements are initialized with the default value for their type. However, we can specify a different value. Example: vector<char> charVec(10, ‘x’); initializes all ten elements of charVec with the value ‘x’. (By the way, the default value for characters is the blank character or space.) September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

The vector Container Type A vector can also be initialized by specifying the first and one past the last element of an existing array. Example: int intArray[8] = {10, 20, 30, 40, 50, 60, 70, 80}; vector<int> intVec(intArray + 1, intArray + 5); creates a vector of size four with elements 20, 30, 40, and 50. A vector can also be initialized with or assigned to another vector. September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

The vector Container Type The STL idiom is quite different from the array idiom. We start with defining an empty vector, for example: vector<double> dblVec; Then we can add items to the back of the vector (and thereby expanding the vector) using the push_back operation: dblVec.push_back(3.7); dblVec.push_back(2.2); dblVec.push_back(-5.1); results in vector of size 3 with elements 3.7, 2.2, -5.1. September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II The pair Type Another class in the standard library that uses the template facility is the pair class. It allows us to combine two values of either the same or different types within a single object. If we want to use pairs, we must include the following header file: #include <utility> Then we can start defining pairs such as: pair<string, double> FranksData(“Frank Miller”, 4.0); September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II The pair Type We use member access notation to access the individual members of a pair: string name = FranksData.first; double GPA = FranksData.second; We can manipulate the values of the members analogously: FranksData.second = 1.0; September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Typedef Names For pair objects, it is often helpful to use the typedef mechanism. This mechanism allows us to define mnemonic synonyms for built-in and user-defined data types. Example: typedef pair<string, double> studentGPA; studentGPA frank(“Frank Miller”, 4.0); studentGPA doris(“Doris Carpenter”, 3.5); studentGPA jeff(“Jeff Bridges”, 2.47); September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Typedef Names Typedef names can improve the readability of our programs. Notice that typedef names do not introduce new types but only synonyms for existing data types. For example, the following code is correct: typedef double GPA; GPA JohnsGPA = 3.4; double BobsGPA; BobsGPA = JohnsGPA; September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Class Types In C++, classes are defined as follows: class Classname { public: // public member variables and member functions … private: // private member variables and member functions protected: // protected member variables and member functions }; September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Class Types Public members are accessible from anywhere within the program. To achieve information hiding, you should generally limit the public members of a class to a small set of member functions that allow the manipulation of the class objects. Private members can be accessed only by the member functions and friends of its class. For information hiding, you should declare all member variables (data members) as private. Protected members behave as public members to a derived class and as private members to the rest of the program. September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Class Types The declaration of class data members is identical to the declaration of variables. Member functions are usually declared within the class definition and defined outside of it. Notice that when you define member functions outside the class definition, you have to provide the class name and the scope resolution operator (::). September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Class Types class StudentAdmonisher { public: bool SetRobotState(robotState); robotState GetRobotState(); private: robotState currentState; }; bool StudentAdmonisher::SetRobotState(robotState rs) { currentState = rs; return true; } robotState StudentAdmonisher::GetRobotState() return currentState; September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Class Types However, short member functions can also be placed inside the class body. Example: class StudentAdmonisher { public: bool SetRobotState(robotState rs) { currentState = rs; return true }; robotState GetRobotState() { return currentState }; private: robotState currentState; }; September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Functions Function definition and declaration Default arguments Functions as arguments Overloading functions Inlining Storage classes September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

Function Definition and Declaration The C++ code that describes what a function does is called the function definition. Its form is: type FunctionName(parameter-declaration-list) { statements } September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

Function Definition and Declaration A function can be declared before it is defined. It can be defined later in the file or can come from a library or a user-specified file. Such a declaration is called a function prototype and has the following general form: type FunctionName(argument-declaration-list); September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Default Arguments A formal parameter can be given a default argument. Usually, a default argument is a constant that occurs frequently when the function is called. Using a default argument saves writing this default value at each invocation. September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Default Arguments Example: int SqrOrPower(int n, int k = 2) // k = 2 is default. { assert(k > 1); // This function only works for // exponents > 1. if (k == 2) return (n * n); else return (SqrOrPower(n, k – 1) * n); } SqrOrPower(i + 5) // computes (i + 5) * (i + 5) SqrOrPower(i + 5, 3) // computes (i + 5) cubed September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Default Arguments Notice that only trailing parameters of a function can have default values. This rule allows the compiler to know which arguments are defaulted when the function is called with fewer than its complete set of arguments. Examples: void F1(int i, int j = 7); legal void F2(int i = 3, int j); illegal void F3(int i, int j = 3, int k = 7); legal void F4(int i = 1, int j = 2, int k = 3); legal void F5(int i, int j = 2, int k); illegal September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

Functions as Arguments Functions in C++ can be thought of as the addresses of the compiled code residing in memory. Functions are therefore a form of pointer. Functions can be passed as a pointer-value argument into another function. September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

Functions as Arguments double F(double x) { return (x*x + 1.0/x); } void plot(double Fcn(double), double x0, double incr, int n) for (int i = 0; i < n; i++) cout << “x: “ << x0 << “ f(x): “ << Fcn(x0) << endl; x0 += incr; int main() plot(F, 0.01, 0.01, 100); return 0; September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

Overloading Functions Overloading refers to using the same name for multiple meanings of an operator or a function. Example: double Average(const int a[], int size) { int sum = 0; for (int i = 0; i < size; ++i) sum += a[i]; return static_cast<double> (sum) / size; } double Average(const double a[], int size) { double sum = 0.0; for (int i = 0; i < size; ++i) sum += a[i]; return (sum / size); } September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

Overloading Functions The following code shows how Average() is invoked: int main() { int a[5] = {1, 2, 3, 4, 5}; double b[5] = {1.1, 2.2, 3.3, 4.4, 5.5}; cout << Average(a, 5) << “ int average” << endl; cout << Average(b, 5) << “ double average” << endl; return 0; } 3 int average 3.3 double average September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Inlining C++ provides the keyword inline to preface a function declaration. For functions declared as inline, the compiler will generate inline code instead of function calls. Inline functions lead to increased time efficiency and decreased memory efficiency. Question: How about recursive functions? Answer: There are no recursive inline functions because they would require infinite memory. September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Inlining Inlining has a similar effect as macro expansion: Inlining example: inline double cube(double x) { return (x * x * x); } Macro example: #define CUBE(X) ((X)*(X)*(X)) September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II

CS410 – Software Engineering Lecture #8: C++ Basics II Inlining So what is the difference between inlining and macro expansion? Answer: Macro expansion provides no type safety as is given by the C++ parameter-passing mechanism. Therefore, it is advisable to use inlining instead of macro expansion. September 27, 2016 CS410 – Software Engineering Lecture #8: C++ Basics II