Comp Sci 1570 Introduction to C++

Similar documents
Separate Compilation of Multi-File Programs

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

CE221 Programming in C++ Part 1 Introduction

Object Oriented Design

Appendix A. The Preprocessor

A A B U n i v e r s i t y

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

y

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Project structure - working with multiple les

Preprocessor Directives

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

5. Applicative Programming. 1. Juli 2011

CIS 190: C/C++ Programming. Classes in C++

Implementing an ADT with a Class

6.096 Introduction to C++

Laboratorio di Tecnologie dell'informazione

Practicum 5 Maps and Closures

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

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

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

Lecture 7. Log into Linux New documents posted to course webpage

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Lab 1: First Steps in C++ - Eclipse

Understanding main() function Input/Output Streams

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

A Fast Review of C Essentials Part II

CS16 Exam #1 7/17/ Minutes 100 Points total

A Fast Review of C Essentials Part I

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

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

Getting started with C++ (Part 2)

Program Organization and Comments

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

CSCI 123 Introduction to Programming Concepts in C++

CS 31 Discussion 1A, Week 1. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50

Laboratorio di Tecnologie dell'informazione

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

The Structure of a C++ Program

PIC 10A Objects/Classes

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

pointers + memory double x; string a; int x; main overhead int y; main overhead

Introduction to C ++

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

EL6483: Brief Overview of C Programming Language

Operator overloading

PIC 10A. Lecture 17: Classes III, overloading

Introducing C++ to Java Programmers

Introduction to C++ IT 1033: Fundamentals of Programming

Starting to Program in C++ (Basics & I/O)

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

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

CPSC 427: Object-Oriented Programming

Programming Language. Functions. Eng. Anis Nazer First Semester

CSI33 Data Structures

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

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

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

4. Structure of a C++ program

Tutorial 2: Compiling and Running C++ Source Code

The Compilation Process

assembler Machine Code Object Files linker Executable File

Creating a C++ Program

Technical Questions. Q 1) What are the key features in C programming language?

CS3215. Outline: 1. Introduction 2. C++ language features 3. C++ program organization

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

6.096 Introduction to C++ January (IAP) 2009

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Variables and numeric types

Chapter 1 INTRODUCTION

CS201 RECITATION 1. Introduction to C++

BLM2031 Structured Programming. Zeyneb KURT

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Introduction to Scientific Programming with C++

Professor Terje Haukaas University of British Columbia, Vancouver C++ Programming

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

These are notes for the third lecture; if statements and loops.

The C++ Language. Arizona State University 1

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

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

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

Programming Fundamentals and Methodology. COMP104: C++ Basics

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

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

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

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

What is Iteration? CMPT-101. Recursion. Understanding Recursion. The Function Header and Documentation. Recursively Adding Numbers

Java Program Structure and Eclipse. Overview. Eclipse Projects and Project Structure. COMP 210: Object-Oriented Programming Lecture Notes 1

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

Chapter 2: Introduction to C++

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

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

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

The University Of Michigan. EECS402 Lecture 09. Andrew M. Morgan. Savitch Ch Compiling With Multiple Files Make Utility.

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

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Transcription:

guards Comp Sci 1570 Introduction to C++

Outline guards 1 2 guards 3

Outline guards 1 2 guards 3

Modular guards One file before system includes prototypes main driver function function definitions now main driver file prototypes header file(s) functions definition implementation source file(s)

Outline guards 1 2 guards 3

: diagram and source files: greet*.* guards

What does including iostream do at compile time? guards #i n clude <i o s t r e a m > i n t main ( ) { s t d : : cout << H ello, world! << s t d : : e n d l ; return 0 ; }

including iostream guards enables calling, iostream is linked, not copied into main

: add guards See files: add.cpp, add.h, add main.cpp

guards allows separate compiling, and linking after, for speedy compile and re-compile. Whereas including the add.cpp directly in main would work, it would copy the entire add.cpp into main at compile time, increasing compile time, especially for big projects

guards

Outline guards 1 2 guards 3

main file guards // Author : C l a y t o n P r i c e // F i l e : main. cpp #i n clude <i o s t r e a m > #i n clude treefarm. h using namespace s t d ; i n t main ( ) { cout << t r e e p r i c e ( ) ; } return 0 ;

implementation file guards // Programmer : C l a y t o n P r i c e d a t e : 10 1 10 // F i l e : treefarmfuncs. cpp // Purpose : This f i l e c o n t a i n s t h e f u n c t i o n // d e f i n i t i o n s f o r t h e t r e e farm program. #i n c l u d e treefarm. h u s i n g namespace s t d ; f l o a t t r e e p r i c e ( c o n s t i n t numtrees, c o n s t s t r i n g t r e e t y p e ) { // body o f i m p l e m e n t a t i o n h e r e }

header file guards // treefarm. h i n u p p e r c a s e f o l l o w s c o n v e n t i o n #i f n d e f TREEFARM H // Can be a n y t h i n g unique, // but k e e p i n g t h i s c o n v e n t i o n i s n i c e #define TREEFARM H // Programmer : C l a y t o n P r i c e date : 10 1 10 // F i l e : treefarm. h // Purpose : t h i s f i l e c o n t a i n s the p r o t o t y p e s f // C o n s tants f o r the t r e e farm program // This c o n s t a n t s t a t e s the wt per board f o o t o const f l o a t OAK DENSITY = 4. 2 5 ; // pounds per s // more header h e r e... #e n d i f

Best practices for creating your own header files guards Always include header guards (a kind of preprocessor ) Do not define variables in header files unless they are constants. files should generally only be used for declarations. Do not define functions in header files. Each header file should have a specific job, and be as independent as possible. For example, you might put all your declarations related to functionality A in A.h and all your declarations related to functionality B in B.h. Give your header files the same name as the source files they re associated with (e.g. grades.h goes with grades.cpp). Try to minimize the number of other header files you #include in your header files. Only #include what is necessary. Do not #include.cpp files; it works, but why not?

Outline guards 1 2 guards 3

Directives for Files guards Put at the top #i f n d e f MYFILE H #define MYFILE H // header c o n t e n t goes h e r e #e n d i f at the bottom

Outline guards 1 2 guards 3

guards The preprocessor is perhaps best thought of as a separate program that runs just before the compiler when you compile your program. When the preprocessor runs, it simply scans through each code file from top to bottom, looking for. Directives are specific instructions that start with a # symbol and end with a newline (NOT a semicolon). There are several different types of, which we will cover below. The preprocessor is not smart; it does not understand C++ syntax; rather, it simply manipulates text before the compiler runs. The output of the preprocessor is then sent to the compiler. Note that the preprocessor does not modify the original code files in any way; rather, all text changes made by the preprocessor happen temporarily in-memory.

guards When you #include a file, the preprocessor copies the contents of the included file into the including file at the point of the #include directive. This is useful when you have information that needs to be included in multiple places (as forward declarations often are). The #include command has two forms: #include < filename > tells the preprocessor to look for the file in a special place defined by the operating system where header files for the C++ runtime library are held. You ll generally use this form when you re including headers that come with the compiler (e.g. that are part of the C++ standard library). # include filename tells the preprocessor to look for the file in the directory containing the source file doing the #include. If it doesn t find the header file there, it will check any other include paths that you ve specified as part of your compiler/ide settings. That failing, it will act identically to the angled brackets case. You ll generally use this form for including your own header files.

Duplicate variable definition guards #i n clude <i o s t r e a m > i n t main ( ) { // t h i s i s a d e f i n i t i o n f o r i d e n t i f i e r x i n t x ; // c o m p i l e e r r o r : d u p l i c a t e d e f i n i t i o n i n t x ; } return 0 ;

Duplicate function definition guards #i n clude <i o s t r e a m > i n t foo ( ) { return 5 ; } // c o m p i l e e r r o r : d u p l i c a t e d e f i n i t i o n i n t foo ( ) { return 5 ; } i n t main ( ) { s t d : : cout << foo ( ) ; }

Double include mistake guards i n t g e t S q u a r e S i d e s ( ) { return 4 ; } #i n clude math. h math.h: geometry.h: #i n clude math. h #i n clude geometry. h i n t main ( ) { return 0 ; } main.cpp:

Double include mistake guards After resolving all of the #include, main.cpp: i n t g e t S q u a r e S i d e s ( ) // from math. h { return 4 ; } i n t g e t S q u a r e S i d e s ( ) // from geometry. h { return 4 ; } i n t main ( ) { return 0 ; }

guards guard preprocessor will prevent the compiler from trying to create multiple definitions for the functions and constants and anything else in the header if this header is included in a translation unit multiple times. Even the system header files have them. Do this for every header file you create. The identifier convention is to use all uppercase versions of the file names (as demonstrated in the example above). Incidentally, ifndef stands for if not defined. So, you can read those as if not defined, define.

guard conditional compilation guards #i f n d e f SOME UNIQUE NAME HERE #define SOME UNIQUE NAME HERE // your d e c l a r a t i o n s and d e f i n i t i o n s h e r e #e n d i f See example: square*.* source files

Outline guards 1 2 guards 3

User and with multiple files guards When you compile a program split into multiple files, normally, the command would be g++ prog. cpp o my prog The source code is prog.cpp and the executable s name will be my prog. We wish to compile the previous tree farm program: g++ treefarm. cpp treefarmfuncs. cpp o t r e e s Provide the names of all compilable files as the source code (italicized in the example above). Alternatively, you may use the wildcard character, * g++. cpp o t r e e s This command will pick up all files in the current directory with a.cpp extension. So, if you use this method, it is vitally important to put your programming projects in their own directory. You don t want to have more than one main function in your directory.

Code::Blocks with multiple files guards To use multiple in this way with Code:Blocks, you must create a project from and empty project file, and add the files to it. Remember however, that you should compile and run on the campus Linux computers before submission, since environments can differ.