Object Oriented Design

Similar documents
Cpt S 122 Data Structures. Introduction to C++ Part II

Functions and Recursion

Cpt S 122 Data Structures. Templates

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Introduction to C++ Systems Programming

C++ How to Program, 9/e by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming Session 23

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Short Notes of CS201

Chapter 15 - C++ As A "Better C"

CS201 - Introduction to Programming Glossary By

Programmazione. Prof. Marco Bertini

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Fundamentals of Programming Session 25

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

CAP Fundamentos de Programação Estruturada Aula 9 STL. Lubia Vinhas

Object Oriented Design

by Pearson Education, Inc. All Rights Reserved. 2

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Object-Oriented Design (OOD) and C++

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

AN OVERVIEW OF C++ 1

Fast Introduction to Object Oriented Programming and C++

10. Functions (Part 2)

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

The format for declaring function templates with type parameters

Chapter 3 Function Overloading

Introduction to C++ Friends, Nesting, Static Members, and Templates Topic #7

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

CSCE 110 PROGRAMMING FUNDAMENTALS

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Getting started with C++ (Part 2)

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

Implementing Abstract Data Types (ADT) using Classes

C++ Programming Fundamentals

CE221 Programming in C++ Part 1 Introduction

Fundamentals of Programming Session 24

CS2141 Software Development using C/C++ Compiling a C++ Program

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Unit 6. Thinking with Functions

CS242 COMPUTER PROGRAMMING

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CHAPTER 4 FUNCTIONS. 4.1 Introduction

TEMPLATE IN C++ Function Templates

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Programmer-Defined Functions

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Chapter 3 - Functions

IS 0020 Program Design and Software Tools

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

EECS402 Lecture 02. Functions. Function Prototype

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

Module Operator Overloading and Type Conversion. Table of Contents

Your first C++ program

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

QUIZ. Can you find 5 errors in this code?

Chapter 3 - Functions. Chapter 3 - Functions. 3.1 Introduction. 3.2 Program Components in C++

Binomial pricer (1.1)

TEMPLATES AND EXCEPTION HANDLING

BITG 1113: Function (Part 2) LECTURE 5

C++ Quick Guide. Advertisements

19. Templates. Object Oriented Programming: Function templates

The Notion of a Class and Some Other Key Ideas (contd.) Questions:

CSI33 Data Structures

C++ Scope Resolution Operator ::

9. Arrays. Compound Data Types: type name [elements]; int billy [5];

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

CPSC 427: Object-Oriented Programming

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Recharge (int, int, int); //constructor declared void disply();

Variables. Data Types.

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

C++ Programming for Non-C Programmers. Supplement

Chapter 6 : Modularity Using Functions (pp )

Chapter 2. Procedural Programming

CPSC 427: Object-Oriented Programming

Structured bindings with polymorphic lambas

Generics Pearson Education, Inc. All rights reserved.

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Object-Oriented Programming

6.5 Function Prototypes and Argument Coercion

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

2 ADT Programming User-defined abstract data types

Function Overloading

Partha Sarathi Mandal

Lecture 3 ADT and C++ Classes (II)

Tokens, Expressions and Control Structures

Programming in C++: Assignment Week 8

Chapter 2: Introduction to C++

Chapter 2 Basic Elements of C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Modules:Dependent ADL

I/O Streams and Standard I/O Devices (cont d.)

Transcription:

Object Oriented Design Chapter 6 Example Activity Diagram 1

Outline Chapter 6 Topics 6.6 C++ Standard Library Header Files 6.14 Inline Functions 6.16 Default Arguments 6.17 Unary Scope Resolution Operator 6.18 Function Overloading 6.19 Function Templates 6.6 C++ Standard Library Header Files The C++ Standard Library is divided into many portions, each with its own header file. The header files contain the function prototypes for the related functions that form each portion of the library. The header files also contain definitions of various class types and functions, as well as constants needed by those functions. A header file instructs the compiler on how to interface with library and user-written components. Figure 6.7 lists some common C++ Standard Library header files. 2

3

4

6.14 Inline Functions C++ provides inline functions to help reduce function call overhead especially for small functions. Placing the qualifier in-line before a function s return type in the function definition advises the compiler to generate a copy of the function s code in place (when appropriate) to avoid a function call. Trade-off Multiple copies of the function code are inserted in the program (often making the program larger) rather than there being a single copy of the function to which control is passed each time the function is called. The complete function definition must appear before the code is inlined so that the compiler knows how to expand a function call into its inlined code. 5

Example #include <iostream> using namespace std; inline int Max(int x, int y) { return (x > y)? x : y; } // Main function for the program int main( ) { cout << "Max (20,10): " << Max(20,10) << endl; cout << "Max (0,200): " << Max(0,200) << endl; cout << "Max (100,1010): " << Max(100,1010) << endl; return 0; } 6.16 Default Arguments It isn t uncommon for a program to invoke a function repeatedly with the same argument value for a particular parameter. Such a parameter has a default argument, i.e., a default value to be passed to that parameter. When a program omits an argument for a parameter with a default argument in a function call, the compiler rewrites the function call and inserts the default value of that argument. Default arguments must be the rightmost (trailing) arguments in a function s parameter list. 6

7

Drawbacks Explicitly specifying all arguments is clearer If the default values for a function change, all client code using the function must be recompiled 6.17 Unary Scope Resolution Operator It s possible to declare local and global variables of the same name. C++ provides the unary scope resolution operator (::) to access a global variable when a local variable of the same name is in scope. Using the unary scope resolution operator (::) with a given variable name is optional when the only variable with that name is a global variable But using (::) would be clearer 8

6.18 Function Overloading C++ enables several functions of the same name to be defined, as long as they have different signatures. This is called function overloading. The C++ compiler selects the proper function to call by examining the number, types and order of the arguments in the call. Function overloading is used to create several functions of the same name that perform similar tasks, but on different data types. 9

10

6.18 Function Overloading (cont.) Overloaded functions are distinguished by their signatures. A signature is a combination of a function s name and its parameter types (in order). The compiler encodes each function identifier with the number and types of its parameters (sometimes referred to as name mangling or name decoration) to enable type-safe linkage. Ensures that the proper overloaded function is called and that the types of the arguments conform to the types of the parameters. Creating overloaded functions with identical parameter list and different return types is a compilation error compiler cannot distinguish those two 11

Mangled function names produced in assembly language by GNU C++ Will This Work #include<iostream> int foo() { return 10; } char foo() { return 'a'; } int main() { char x = foo(); foo(); return 0; } 12

6.19 Function Templates If the program logic and operations are identical for each data type, overloading may be performed more compactly and conveniently by using function templates. You write a single function template definition. 6.19 Function Templates (cont.) All function template definitions begin with the template keyword followed by a template parameter list to the function template enclosed in angle brackets (< and >). Every parameter in the template parameter list (often referred to as a formal type parameter) is preceded by keyword typename or keyword class The formal type parameters are placeholders for fundamental types or user-defined types. These placeholders are used to specify the types of the function s parameters, to specify the function s return type and to declare variables within the body of the function definition. 13

14

Exercise Write a function overloading GetAverage Write a function template GetAverage int main() { int IntArray[5] = {100, 200, 400, 500, 1000}; float FloatArray[3] = { 1.55f, 5.44f, 12.36f}; cout << GetAverage(IntArray, 5); cout << GetAverage(FloatArray, 3); } 15

Function Template template<class T> double GetAverage(T tarray[], int nelements) { T tsum = T(); // tsum = 0 for (int nindex = 0; nindex < nelements; ++nindex) { tsum += tarray[nindex]; } } // Whatever type of T is, convert to double return double(tsum) / nelements; End 16