Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Program Files and Preprocessing Directives Program files and.

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. About the book Ivor Horton, Beginning ANSI C++: The Complete.
Computer Science 1620 Function Scope & Global Variables.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
CS 202 Computer Science II Lab Fall 2009 October 1.
Programming C/C++ on Eclipe Trình bày: Ths HungNM C/C++ Training.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Basic Data Type and calculations Data types in C++ Program.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Ivor Horton, Beginning ANSI C++: The Complete Language, 3 rd.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
CS 213 Fall 1998 Namespaces Inline functions Preprocessing Compiling Name mangling Linking.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Loops: Repeating One or More Statements for loop while loop.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
SNU OOPSLA Lab. 9. Source Files and Programs © copyright 2001 SNU OOPSLA Lab.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. More on Handling Basic Data Types Mixed types of data Data.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Pointers Declare a pointer Address of a variable Pointers and.
C++ Programming Michael Griffiths Corporate Information and Computing Services The University of Sheffield
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Preprocessing Lecture 9 November 2, 2004.
Chapter 15 - C++ As A "Better C"
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
13 C Preprocessor.
C++ Lesson 1.
What Is? function predefined, programmer-defined
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
Programmer-Defined Functions, Call-by-Value, Multiple Files Lab 5
Global & Local Identifiers
Lecture 4-7 Classes and Objects
Introduction to Programming
Screen output // Definition and use of variables
C Preprocessor(CPP).
Classes and Objects.
Programming in C Miscellaneous Topics.
Programming in C Miscellaneous Topics.
Namespaces How Shall I Name Thee?.
C++ Compilation Model C++ is a compiled language
C Preprocessor Seema Chandak.
What Is? function predefined, programmer-defined
Conditional Compilation
Presentation transcript:

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Program Files and Preprocessing Directives Program files and header files A translation unit Linkage Namespaces Preprocessing directives Debugging

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC.

Translation unit Each.cpp file in your program, along with the contents of all its header files included, is called a translation unit. The compiler processes each translation unit independently to generate an object file

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. The Scope of a Name Block scope (local scope) –Name enclosed between braces Name hiding

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Accessing hidden name

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. The scope of a Name Global scope (file scope) ::limit

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Program Files and Linkage The ways in a translation unit are handled in the compiler/link process is determined by a property that a name can have called linkage Every name in a translation unit has internal linkage, external linkage, or no linkage

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Linkages Internal linkage –Access a name within the same translation unit –Global variables have been declared as const External linkage –A name can be accessed from another translation unit –Global variables have been declared as non- const No linkage –All names that are defined within a block (local names)

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. External Names

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Variable Scope

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. External Linkage File1.cpp File2.cpp

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Forcing Constant Variables to Have External Linkage Constant variables have internal linkage File1.cpp File2.cpp

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Program File Two types of file extension.cpp,.h.cpp –Definitions –Main program.h –Declarations External variables (data10_1.cpp, ex10_1.cpp)

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Namespace Unique name for external linkage –With large programs, choosing unique names for all the entities that have external linkage can become difficult Global namespace Declare namespace

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Global Namespace namespace calc { …} namespace sort {… } namespace calc {// extend the namespace calc } Data10_2.cpp, ex10_2.cpp Data10_2a, ex10_2a.cpp

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Functions in Namespace Using namespace and using declarations Using declarations –Compare.h, Compare.cpp, ex10_3.cpp –#include “compare.h”; + using compare::max; + using compare::min; is the same work as using namespace compare;

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Template Functions and Namespaces Tempcomp.h, ex10_4.cpp Extension namespace –Extent the same namespace – compare –Tempcomp.h, Normal.h, ex10_5.cpp

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Namespace Aliases –Replace the long namespace name –Such as: namespace alias_name = original_namespace_name; namespace SG5P3S2 = SystemGroup5_Process3_Subsection2; int maxValue = SG5P3S2::max(data, size);

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Nested Namespace namespace outer { double max(double* data, const int& size) { … } double min(double*data const int& size) { … } namespace inner { double* normalize(double* data, const int & size) { double minValue = min(data, size); // outer::min() } } } int result = outer::min(data,size); double* newData = outer::inner::normalize(data, size); // calling outer::min() –ex10_5a.cpp

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Preprocessing Directives

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. #include #include “self_defined_file_name” #include “myheader.h” #include “.\header\myheader1.h” #include “..\user\header\myheader2.h”

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Substitutions Disadvantages –Without data type detection –Not belong to namespace, not in any scope #define identifier sequence_of_characters #define PI // string “ ” replace all the PI tokens in code while compiler #define BLACK WHITE // replace all the BLACK to WHITE tokens while compiler #define BLACK //remove the identifier BLACK token

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. #undef #define PI … #undef PI … –Code between #define and #undef, PI will be replaced to However, the rest of the code remain PI without replacing

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Macro Substitutions #define identifier(list_of_identifiers) substitution_string –E.g., #define Print(myVar) cout << (mayVar) << endl Print(ival);  cout << (ival) << endl; –E.g. #define Print(myVar, digits) cout << setw(digits) << (myVar) << endl Print(ival, 15);  cout << setw(15) << (ival) << endl; Using template function or inline function template inline void Print(const T& myVar, const int& digits) { cout << setw(digits) << myVar << endl; } Print(ival, 15);

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Errors Cause by Macro #define max(x,y) x>y ? x:y result = max(myval, 99); result = myval>99 ? myval:99; result = max(myval++, 99); result = myval++>99 ? myval++:99; // if myval>99 then myval will increase twice result = max((myval++), 99); result = (myval++)>99 ? (myval++):99;

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Errors Cause by Macro #define product(m,n) m*n result = product(x, y+1); // Which means result = x*y+1 #define product(m,n) ((m)*(n)) Use inline function instead of macro to prevent such errors Inline int product(int m, int n) { return m*n; }

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. #if and #endif #if defined identifier double average = 0.0; #if defined CALCAVERAGE int count = sizeof data/sizeof data[0]; for (int i=0; i<count; i++) average += data[i]; average /= count; #endif

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. #ifndef Preventing codes duplication #if !defined identifier #ifndef identifier

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. #if, #else, #endif #if constant_expression #if CPU == Pentium4 cout << “Pentium4 code version.” << endl; // Pentium4 oriented code #else cout << “Older Pentium code version.” << endl; // Code for older Pentium processors #endif

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. #elif #elif constant_expression #if LANGUAGE == ENGLISH #define Greeting “Good Morning.” #elif LANGUAGE == GERMAN #define Greeting “Guten Tag.” #elif LANGUAGE == FRENCH #define Greeting “Bonjour.” #else define Greeting “Hi.” #endif cout << Greeting << endl;

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Standard Preprocessing Macros

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Output compilation time

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. #error Diagnostic messages in preprocessing phase #ifndef _cplusplus #error “Error – Should be C++” #endif

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Integrated Debuggers Tracing program flow –Stepping through Setting breakpoints Setting watches Inspecting program elements

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Preprocessing Directives in Debugging Functions.h, Functions.cpp, ex10_6.cpp

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Assert Macro Diagnostic logical expressions in program ex10_7.cpp