Miscellaneous C++ Topics

Slides:



Advertisements
Similar presentations
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Advertisements

Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
CS-2303 System Programming Concepts
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Introduction to C++ Systems Programming.
C++ for Engineers and Scientists Third Edition
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
Introduction to FunctionsCIS 1057 Fall Introduction to Functions CIS 1057 Computer Programming in C Fall 2013 (Acknowledgement: Many slides based.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 15 - C++ As A "Better C"
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Functions.
Classes C++ representation of an object
What Is? function predefined, programmer-defined
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
Introduction to Custom Templates
Functions Separate Compilation
CS212: Object Oriented Analysis and Design
6.11 Function Call Stack and Activation Records
6 Functions.
MACRO Processors CSCI/CMPE 3334 David Egle.
6 Functions.
“Under the Hood” of Polymorphism
Classes, Constructors, etc., in C++
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Structures, Unions, and Typedefs
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Linked Lists in C and C++
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Bit Fields & Bitwise Operations
Programming Assignment #1 12-Month Calendar—
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Symbolic Constants in C
Recursion and Implementation of Functions
Scope Rules and Storage Types
Programming Assignment #5
Differences between Java and C
Your first C and C++ programs
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Functions in C and C++ CS-2303 System Programming Concepts Hugh C. Lauer (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Digression on Loop Invariants
Chapter 15 - C++ As A "Better C"
Classes C++ representation of an object
A Deeper Look at Classes
What Is? function predefined, programmer-defined
More C++ Classes Systems Programming.
Presentation transcript:

Miscellaneous C++ Topics Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 Miscellaneous C++ Topics

Miscellaneous C++ Topics Outline A note on Preprocessor Wrappers Inline functions Default Arguments Function Templates CS-2303, A-Term 2012 Miscellaneous C++ Topics

Preprocessor Wrappers Exactly as in C Prevent code from being included more than once. #ifndef – “if not defined” Skip this code if it has been included already #define Define a name so this code will not be included again #endif If the header has been included previously Name is defined already and the header file is not included again. Prevents multiple-definition errors Example #ifndef TIME_H #define TIME_H … // code #endif §A12.5 in K&R (Not in Absolute C++) CS-2303, A-Term 2012 Miscellaneous C++ Topics

Miscellaneous C++ Topics Inline Functions Advice to compiler that the body of the function may be substituted for a call to that function Insert the qualifier inline before return type of function Example:– inline int max(int a, int b) { return (a>b) ? a : b; } §7.2 of Absolute C++ CS-2303, A-Term 2012 Miscellaneous C++ Topics

Inline Functions (continued) Reasons:– Reduce function call overhead—especially for small functions in inner loops Takes advantage of compiler’s local optimizations Trade-off of inline functions Multiple copies of the function code are inserted in the program (possibly making the program larger) The compiler may ignore the inline qualifier Typically does so for all but the smallest functions CS-2303, A-Term 2012 Miscellaneous C++ Topics

Inline Function Example inline qualifier Complete function definition so compiler knows how to expand a cube function call into inline code CS-2303, A-Term 2012 Miscellaneous C++ Topics

Inline Functions (continued) Typical usage:– Member function of a class Very simple, short, fast operations Especially in inner loops Especially set and get functions Saves the (non-trivial) overhead of invoking a method, creating activation record, etc. Encourages cleaner, more readable code inline qualifier is strictly advisory Widely used whenever you ask yourself about efficiency of a calling a function vs. accessing a class member directly CS-2303, A-Term 2012 Miscellaneous C++ Topics

Miscellaneous C++ Topics Questions? CS-2303, A-Term 2012 Miscellaneous C++ Topics

Miscellaneous C++ Topics Default Argument Definition:– A default value to be passed to a parameter When the function/method call does not specify an argument for that parameter Example void prnt(int value, int base = 10); … prnt(100); //prints in base 10 prnt(100,16); //prints in base 16 prnt(100,8); //prints in base 8 prnt(100,3); //prints in base 3 An “assignment” as part of parameter list CS-2303, A-Term 2012 Miscellaneous C++ Topics

Default Arguments (continued) Must be rightmost parameter(s) in parameter list of function Default value specified in function prototype Usually in class header file So compiler knows how to compile calls If one argument is defaulted, all arguments to right of it must be defaulted also CS-2303, A-Term 2012 Miscellaneous C++ Topics

Default Argument Example void f(int a, double b, int c = 10, bool d = false); f(100, 3.14) // okay f(100, 3.14, 15) // okay f(100, 3.14, 15, true) // okay f(100, 3.14, , true) // no good f(100, 3.14, 10, true) // okay CS-2303, A-Term 2012 Miscellaneous C++ Topics

Summary – Default Arguments Simplest form of function overloading Default values specified in header and function prototype Defaults applied from right to left §4.2, Absolute C++ (Near end of section) CS-2303, A-Term 2012 Miscellaneous C++ Topics

Miscellaneous C++ Topics Questions? CS-2303, A-Term 2012 Miscellaneous C++ Topics

Miscellaneous C++ Topics Problem What about the case when all of the overloaded functions have essentially the same code, but for different types Not good style to write a bunch of nearly identical code Inefficient of programmer time Various instances can get out of sync with each other Some cases might be missed CS-2303, A-Term 2012 Miscellaneous C++ Topics

Solution – Function Templates A more compact and convenient form of overloading. Identical program logic and operations for each data type §16.1 of Absolute C++ CS-2303, A-Term 2012 Miscellaneous C++ Topics

Miscellaneous C++ Topics Function Template Written by programmer once Essentially defines a whole family of overloaded functions Begins with the template keyword Contains a template parameter list of formal type and the parameters for the function template are enclosed in angle brackets (<>) Formal type parameters Preceded by keyword typename or keyword class Placeholders for fundamental types or user-defined types CS-2303, A-Term 2012 Miscellaneous C++ Topics

Function Template Example Using formal type parameter T in place of data type CS-2303, A-Term 2012 Miscellaneous C++ Topics

Miscellaneous C++ Topics Result Whole Family of Overloaded Functions All with the “same” source code All doing the same thing, but with different data types Defined in one place Common Programming Errors Not placing keyword class or keyword typename before every formal type parameter of a function template Writing < class S, T > instead of < class S, class T > ) is a syntax error CS-2303, A-Term 2012 Miscellaneous C++ Topics

Miscellaneous C++ Topics Definition:– Specialization The automatic generation of a new “overloaded function” from the template as needed I.e., whenever a function template is called with a particular type Example for function template max with type parameter T called with int arguments Compiler detects a max invocation in the program code. int is substituted for T throughout the template definition. This produces function-template specialization max<int> CS-2303, A-Term 2012 Miscellaneous C++ Topics

Specialization Example Compiler:– Suspends what it was doing Generates a new overloaded function from template maximum for type parameter int Compiles this new function Returns to this program and compiles a call to new function Invoking maximum with int arguments CS-2303, A-Term 2012 Miscellaneous C++ Topics

Specialization Example (continued) Invoking maximum with double arguments Compiler does same again, but for parameter type double Invoking maximum with char arguments And again, but for parameter type char CS-2303, A-Term 2012 Miscellaneous C++ Topics

Specialization (continued) Each time that the compiler encounters a use of maximum with a type that it has not seen before … it creates a new instance of the function maximum with new parameter types With new mangled name! CS-2303, A-Term 2012 Miscellaneous C++ Topics

Function Templates Summary Very important for Code readability Code reuse Good coding style Programmer efficiency Foundation of other template features in C++ CS-2303, A-Term 2012 Miscellaneous C++ Topics

Miscellaneous C++ Topics Questions? CS-2303, A-Term 2012 Miscellaneous C++ Topics