OBJECT ORIENTED PROGRAMMING USING C++

Similar documents
Programming for Engineers C Preprocessor

MODULE 10 PREPROCESSOR DIRECTIVES

C: How to Program. Week /June/18

Chapter 7: Preprocessing Directives

Appendix A. The Preprocessor

Lecture 10. Command line arguments Character handling library void* String manipulation (copying, searching, etc.)

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Have examined process Creating program Have developed program Written in C Source code

COMP322 - Introduction to C++

Unit 4 Preprocessor Directives

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

fpp: Fortran preprocessor March 9, 2009

3 PREPROCESSOR. Overview. Listing 3-0. Table 3-0.

Errors During Compilation and Execution Background Information

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford.

C for Engineers and Scientists: An Interpretive Approach. Chapter 7: Preprocessing Directives

Conditional Compilation

A Fast Review of C Essentials Part II

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

C and C++ 2. Functions Preprocessor. Alan Mycroft

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

COMsW Introduction to Computer Programming in C

The Compilation Process

CSI33 Data Structures

Intermediate Programming, Spring 2017*


C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros.

Intro to the Preprocessor

UNIT- 3 Introduction to C++

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Summer May 11, 2010

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

CODE TIME TECHNOLOGIES. Abassi RTOS MISRA-C:2004. Compliance Report

Programming with C++ as a Second Language


INDEX. Figure I-0. Listing I-0. Table I-0. Symbols.DIRECTIVE (see Assembler directives)? preprocessor operator 3-34

C Syntax Out: 15 September, 1995

Defensive Programming

Problem Solving and 'C' Programming

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Chapter 2. Procedural Programming

Chapter 2: Introduction to C++

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

Macros in C/C++ Computer Science and Engineering College of Engineering The Ohio State University. Lecture 33

CSI33 Data Structures

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

Contents Lecture 3. C Preprocessor, Chapter 11 Declarations, Chapter 8. Jonas Skeppstedt Lecture / 44

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Chapter 7: The Preprocessor

Inno Setup Preprocessor: Introduction

COMP322 - Introduction to C++ Lecture 01 - Introduction

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Columbus Schema for C/C++ Preprocessing

Chapter 2: Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

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

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

#include. Practical C Issues: #define. #define Macros. Example. #if

Compiler, Assembler, and Linker

Lecture 2 Tao Wang 1

C++ Programming: From Problem Analysis to Program Design, Third Edition

C Preprocessor. Prabhat Kumar Padhy

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

CSCI 171 Chapter Outlines

The C Pre Processor ECE2893. Lecture 18. ECE2893 The C Pre Processor Spring / 10

Separate Compilation of Multi-File Programs

III. Classes (Chap. 3)

Visual Analyzer V2.1 User s Guide

CMPE110 - EXPERIMENT 1 * MICROSOFT VISUAL STUDIO AND C++ PROGRAMMING

Objectives. In this chapter, you will:

Probably the best way to start learning a programming language is with a program. So here is our first program:

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Come and join us at WebLyceum

Lab # 02. Basic Elements of C++ _ Part1

2 nd Week Lecture Notes

A Fast Review of C Essentials Part I

CSCI 1061U Programming Workshop 2. C++ Basics

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

QUIZ. What are 3 differences between C and C++ const variables?

ME 461 C review Session Fall 2009 S. Keres

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

LECTURE 02 INTRODUCTION TO C++

The Structure of a C++ Program

Lecture Outline. COMP-421 Compiler Design. What is Lex? Lex Specification. ! Lexical Analyzer Lex. ! Lex Examples. Presented by Dr Ioanna Dionysiou

The C Preprocessor (and more)!

PROGRAMMAZIONE I A.A. 2017/2018

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Axivion Bauhaus Suite Technical Factsheet MISRA

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Your First C++ Program. September 1, 2010

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

CS2141 Software Development using C/C++ C++ Basics

Engineering Problem Solving with C++, Etter

Transcription:

OBJECT ORIENTED PROGRAMMING USING C++

Chapter 17 - The Preprocessor Outline 17.1 Introduction 17.2 The #include Preprocessor Directive 17.3 The #define Preprocessor Directive: Symbolic Constants 17.4 The #define Preprocessor Directive: Macros 17.5 Conditional Compilation 17.6 The #error and #pragma Preprocessor Directives 17.7 The # and ## Operators 17.8 Line Numbers 17.9 Predefined Symbolic Constants 17.10 Assertions

17.1 Introduction preprocessing occurs before a program is compiled. inclusion of other files definition of symbolic constants and macros, conditional compilation of program code conditional execution of preprocessor directives Format of preprocessor directives: lines begin with # only whitespace characters before directives on a line they are not C++ statements - no semicolon (;)

17.2 The #include Preprocessor Directive #include copy of a specified file included in place of the directive #include <filename> - searches standard library for file (use for standard library files) #include "filename" - searches current directory, then standard library (use for user-defined files) Used for loading header files (#include <iostream>) programs with multiple source files to be compiled together header file - has common declarations and definitions (classes, structures, function prototypes) #include statement in each file

17.3 The #define Preprocessor Directive: Symbolic Constants #define preprocessor directive used to create symbolic constants and macros. Symbolic constants when program compiled, all occurrences of symbolic constant replaced with replacement text Format: #define identifier replacement-text Example: #define PI 3.14159 everything to right of identifier replaces text #define PI = 3.14159 replaces "PI" with " = 3.14159", probably results in an error cannot redefine symbolic constants with more #define statements

17.4 The #define Preprocessor Directive: Macros Macro - operation defined in #define intended for C programs macro without arguments: treated like a symbolic constant macro with arguments: arguments substituted for replacement text, macro expanded performs a text substitution - no data type checking Example: #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) ) area = CIRCLE_AREA( 4 ); becomes area = ( 3.14159 * ( 4 ) * ( 4 ) );

17.4 The #define Preprocessor Directive: Macros (II) use parenthesis: without them, #define CIRCLE_AREA( x ) PI * ( x ) * ( x ) area = CIRCLE_AREA( c + 2 ); becomes area = 3.14159 * c + 2 * c + 2; which evaluates incorrectly multiple arguments: #define RECTANGLE_AREA( x, y ) ( ( x ) * ( y ) ) rectarea = RECTANGLE_AREA( a + 4, b + 7 ); becomes rectarea = ( ( a + 4 ) * ( b + 7 ) ); #undef undefines a symbolic constant or macro, which can later be redefined

17.5 Conditional Compilation conditional compilation control preprocessor directives and compilation cast expressions, sizeof, enumeration constants cannot be evaluated structure similar to if #if!defined( NULL ) #define NULL 0 #endif determines if symbolic constant NULL defined if NULL is defined, defined(null) evaluates to 1 if NULL not defined, defines NULL as 0 every #if ends with #endif #ifdef short for #if defined(name) #ifndef short for #if!defined(name)

17.5 Conditional Compilation (II) Other statements: #elif - equivalent of else if in an if structure #else - equivalent of else in an if structure "Comment out" code cannot use /*... */ use #if 0 #endif code commented out to enable code, change 0 to 1

17.5 Conditional Compilation (III) Debugging #define DEBUG 1 #ifdef DEBUG cerr << "Variable x = " << x << endl; #endif Defining DEBUG enables code. After code corrected, remove #define statement and debugging statements are ignored.

17.6 The #error and #pragma Preprocessor Directives #error tokens tokens - sequences of characters separated by spaces "I like C++" has 3 tokens prints message and tokens (depends on implementation) for example: when #error encountered, tokens displayed and preprocessing stops (program does not compile) #pragma tokens implementation defined action (consult compiler documentation) pragmas not recognized by compiler are ignored

17.7 The # and ## Operators # - replacement text token converted to string with quotes #define HELLO( x ) cout << "Hello, " #x << endl; HELLO(John) becomes cout << "Hello, " "John" << endl; Notice # strings separated by whitespace are concatenated when using cout ## - concatenates two tokens #define TOKENCONCAT( x, y ) x ## y TOKENCONCAT( O, K ) becomes OK

17.8 Line Numbers #line renumbers subsequent code lines, starting with integer value file name can be included #line 100 "myfile.c" lines are numbered from 100 beginning with next source code file for purposes of errors, file name is "myfile.c" makes errors more meaningful line numbers do not appear in source file

17.9 Predefined Symbolic Constants Five predefined symbolic constants cannot be used in #define or #undef Symbolic c onsta nt LINE FILE DATE TIME Desc ription The line number of the current source code line (an integer constant). The presumed name of the source file (a string). The date the source file is compiled (a string of the form "Mmm dd yyyy" such as "Jan 19 2001"). The time the source file is compiled (a string literal of the form "hh:mm:ss").

17.10 Assertions assert macro header <cassert> tests value of an expression if 0 (false) prints error message and calls abort assert( x <= 10 ); if NDEBUG defined, all subsequent assert statements ignored #define NDEBUG