An inline function is one in which the function code replaces the function call directly. Inline class member functions

Similar documents
More C++ Classes. Systems Programming

Fundamentals of Programming Session 24

Pointer Assignments. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 18 October 29, The new Operator

57:017, Computers in Engineering C++ Classes

Chapter 16: Classes and Data Abstraction

Chapter 6: Classes and Data Abstraction

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools

9.1 Introduction. Integrated Time class case study Preprocessor wrapper Three types of handles on an object. Class functions

IS 0020 Program Design and Software Tools

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

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

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

IS 0020 Program Design and Software Tools

Arrays. Week 4. Assylbek Jumagaliyev

Chapter 2 - Control Structures

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

A Deeper Look at Classes

Chapter 2 - Control Structures

Chapter 18 - C++ Operator Overloading

Functions and Recursion

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Introduction to C++ Systems Programming

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Functions and Recursion

Working with Batches of Data

C++ Polymorphism. Systems Programming

Chapter 1 Introduction to Computers and C++ Programming

Fundamentals of Programming Session 25

Classes and Data Abstraction. Topic 5

Classes: A Deeper Look

Introduction to Programming

CS242 COMPUTER PROGRAMMING

Computer Programming with C++ (21)

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

Operator Overloading

Chapter 2 - Control Structures

Classes and Data Abstraction. Topic 5

Arrays. Outline. Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays Prentice Hall, Inc. All rights reserved.

Operator Overloading in C++ Systems Programming

Exception Handling Pearson Education, Inc. All rights reserved.

C++ Programming Lecture 11 Functions Part I

Introduction. W8.2 Operator Overloading

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Chapter 2 - Control Structures

CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

Introduction to C++ Introduction to C++ 1

Introduction to Programming

W8.2 Operator Overloading

EE 152 Advanced Programming LAB 7

Introduction to Programming session 24

Pointers and Strings. Adhi Harmoko S, M.Komp

Classes: A Deeper Look, Part 2

Class string and String Stream Processing Pearson Education, Inc. All rights reserved.

Control Statements: Part Pearson Education, Inc. All rights reserved.

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Functions and Recursion

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

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

How to engineer a class to separate its interface from its implementation and encourage reuse.

Computer Programming C++ Classes and Objects 6 th Lecture

Deitel Dive-Into Series: Dive-Into Cygwin and GNU C++

C Functions. Object created and destroyed within its block auto: default for local variables

Chapter 20 - C++ Virtual Functions and Polymorphism

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

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

Operator Overloading

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Chapter 3 - Functions

1 // Fig. 6.13: time2.cpp 2 // Member-function definitions for class Time. 3 #include <iostream> Outline. 4 5 using std::cout; 6 7 #include <iomanip>

UEE1303(1070) S12: Object-Oriented Programming Constructors and Destructors

Chapter 3 - Functions

Chapter 19 - C++ Inheritance

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

Chapter 12 - Templates

AN OVERVIEW OF C++ 1

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools

COMP322 - Introduction to C++

Chapter 9 Classes : A Deeper Look, Part 1

CSC 330 Object-Oriented Programming. Exception Handling CSC 330

UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

Defining Class Functions.

W8.1 Continuing Classes friend Functions and friend Classes Using the this Pointer Cascading Function Calls

Programmazione. Prof. Marco Bertini

Intermediate Programming, Spring 2017*

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Object Oriented Programming with C++ (24)

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Due Date: See Blackboard

System Design and Programming II

Transcription:

Inline Functions An inline function is one in which the function code replaces the function call directly. Inline class member functions if they are defined as part of the class definition, implicit if they are defined outside of the class definition, explicit, I.e.using the keyword, inline. Inline functions should be short (preferable one-liners).

Example of Inline Functions class CStr { char *pdata; int nlength; public: Inline functions within class declarations char *get_data(void) //implicit inline function {return pdata; } int getlength(void); };

Example of Inline Functions inline void CStr::getlength(void) //explicit inline function { return nlength; } Inline functions outside of class declarations int main(void) { char *s; int n; CStr a( Joe ); s = a.get_data(); n = b.getlength(); } In both cases, the compiler will insert the code of the functions get_data() and getlength() instead of generating calls to these functions

Utility Functions Utility functions private functions that support the operation of public functions Not intended to be used directly by clients

1 // Fig. 6.9: salesp.h 2 // SalesPerson class definition. 3 // Member functions defined in salesp.cpp. 4 #ifndef SALESP_H 5 #define SALESP_H 6 7 class SalesPerson { 8 9 public: Set access function performs validity checks. 10 SalesPerson(); // constructor 11 void getsalesfromuser(); // input sales from keyboard 12 void setsales( int, double ); // set sales for a month private utility 13 void printannualsales(); // summarize and print sales 14 function. 15 private: 16 double totalannualsales(); // utility function 17 double sales[ 12 ]; // 12 monthly sales figures 18 19 }; // end class SalesPerson 20 21 #endif salesp.h (1 of 1)

1 // Fig. 6.10: salesp.cpp 2 // Member functions for class SalesPerson. 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 using std::fixed; 9 10 #include <iomanip> 11 12 using std::setprecision; 13 14 // include SalesPerson class definition from salesp.h 15 #include "salesp.h" 16 17 // initialize elements of array sales to 0.0 18 SalesPerson::SalesPerson() 19 { 20 for ( int i = 0; i < 12; i++ ) 21 sales[ i ] = 0.0; 22 23 } // end SalesPerson constructor 24 salesp.cpp (1 of 3) 6

25 // get 12 sales figures from the user at the keyboard 26 void SalesPerson::getSalesFromUser() 27 { 28 double salesfigure; 29 30 for ( int i = 1; i <= 12; i++ ) { 31 cout << "Enter sales amount for month " << i << ": "; 32 cin >> salesfigure; 33 setsales( i, salesfigure ); 34 35 } // end for 36 37 } // end function getsalesfromuser 38 39 // set one of the 12 monthly sales figures; function subtracts 40 // one from month value for proper subscript in sales array 41 void SalesPerson::setSales( int month, double amount ) validity checks. 42 { 43 // test for valid month and amount values 44 if ( month >= 1 && month <= 12 && amount > 0 ) 45 sales[ month - 1 ] = amount; // adjust for subscripts 0-11 46 47 else // invalid month or amount value 48 cout << "Invalid month or sales figure" << endl; Set access function performs salesp.cpp (2 of 3) 7

49 50 } // end function setsales 51 52 // print total annual sales (with help of utility function) 53 void SalesPerson::printAnnualSales() 54 { 55 cout << setprecision( 2 ) << fixed 56 << "\nthe total annual sales are: $" 57 << totalannualsales() << endl; // call utility function 58 59 } // end function printannualsales 60 61 // private utility function to total annual sales 62 double SalesPerson::totalAnnualSales() 63 { 64 double total = 0.0; // initialize total 65 66 for ( int i = 0; i < 12; i++ ) // summarize sales results 67 total += sales[ i ]; 68 69 return total; 70 71 } // end function totalannualsales salesp.cpp (3 of 3) private utility function to help function printannualsales; encapsulates logic of manipulating sales array. 8

1 // Fig. 6.11: fig06_11.cpp 2 // Demonstrating a utility function. 3 // Compile this program with salesp.cpp 4 5 // include SalesPerson class definition from salesp.h 6 #include "salesp.h" 7 8 int main() 9 { 10 SalesPerson s; // create SalesPerson object s 11 12 s.getsalesfromuser(); // note simple sequential code; no 13 s.printannualsales(); // control structures in main 14 15 return 0; 16 17 } // end main fig06_11.cpp (1 of 1) Simple sequence of member function calls; logic encapsulated in member functions. 9

Enter sales amount for month 1: 5314.76 Enter sales amount for month 2: 4292.38 Enter sales amount for month 3: 4589.83 Enter sales amount for month 4: 5534.03 Enter sales amount for month 5: 4376.34 Enter sales amount for month 6: 5698.45 Enter sales amount for month 7: 4439.22 Enter sales amount for month 8: 5893.57 Enter sales amount for month 9: 4909.67 Enter sales amount for month 10: 5123.45 Enter sales amount for month 11: 4024.97 Enter sales amount for month 12: 5923.92 fig06_11.cpp output (1 of 1) 10 The total annual sales are: $60120.59