Code Organization CSCE 121 J. Michael Moore.

Slides:



Advertisements
Similar presentations
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
CSE 303 Lecture 16 Multi-file (larger) programs
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
David Notkin Autumn 2009 CSE303 Lecture 20 Multi-file (larger) programs Friday: social implications.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Separating Definition & Implementation Headers and Comments.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
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.
15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Problem Session 4 Header Files and Unix. Where are they? You may need to examine the header file to determine what the data structures and functions provided.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
TK1924 Program Design & Problem Solving Session 2011/2012
Classes C++ representation of an object
What Is? function predefined, programmer-defined
Chapter 7: Introduction to Classes and Objects
Chapter 9 More on Objects and Classes
Separate Compilation and Namespaces
Chapter 13 - The Preprocessor
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Pre-processor Directives
Anatomy of a class Part II
Separate Compilation and Namespaces
Const in Classes CSCE 121 J. Michael Moore.
Structures putting data together.
Separate Compilation.
Computer Organization & Compilation Process
Chapter 9 Classes: A Deeper Look, Part 1
Chapter 9 Objects and Classes
C Preprocessor(CPP).
Separating Definition & Implementation
Code Organization Classes
Anatomy of a Function Part 1
Anatomy of a Function Part 3
Comments, Prototypes, Headers & Multiple Source Files
Structures putting data together.
Miscellaneous C++ Topics
CSc 352 An Introduction to the C Preprocessor
Default Parameters February 24, 2016
Function Overloading CSCE 121 J. Michael Moore
Classes.
C++ Compilation Model C++ is a compiled language
C Preprocessor Seema Chandak.
Classes C++ representation of an object
What Is? function predefined, programmer-defined
Computer Organization & Compilation Process
Code Organization Classes
Lecture 8 Object Oriented Programming (OOP)
Conditional Compilation
More C++ Classes Systems Programming.
Presentation transcript:

Code Organization CSCE 121 J. Michael Moore

Multiple Files As programs grow, we want to separate our code into multiple files. Think how hard it would be to find something in a file with a million lines of code. We are already doing that, each time we #include something, utilizing other files for creating the program.

Ways of organizing Files for “libraries” of functions. Makes files very large and slow to compile. Only need declarations for compiling. Put function declarations and definitions in separate files. Only #include the declarations. By convention Function declarations go in header files .h What a function is… Function definitions go in source files .cpp How the function works…

Recall Multiple definitions!!! EGADS!!! A definition can happen only one time. When we #include, it may or may not contain any definitions. What if different files in our program #include the same file, and that file contains definitions? Multiple definitions!!! EGADS!!!

Avoiding re-definitions Do not put definitions in the files we include. Not practical, plus we’ll need to once we get to user-defined classes. Only #include from one file. Not possible, files that #include do so since the compiler needs the declarations in the file. Only #include header files set up to prevent re-definitions.

Header Guards If a file has already been included, do not include it again. Use preprocessor, i.e. things that start with #

Using Header Guards #ifndef HEADERFILENAME_H #define HEADERFILENAME_H // code to be included #endif If identifier not defined Define identifier End the if If the file is included again, the identifier will be defined and the code will not be included again. By convention, we use the header filename in all caps with underscore in place of ‘.’

Using Header Guards (Alternative) #pragma once // code to be included If already encountered, file not processed again. Not to be used in this class! Simpler Not part of C++ standard! Not supported by all compilers!

Note on default parameters and separate files… The compiler uses the declaration to create the function signature. The default parameter value should be indicated only in the declaration. In other words, put the default parameter value in the header file!

Using our “Library” What we’ve seen so far #include <iostream> < > indicate the library is located in the default location for c++ libraries. When we create our own “library” #include "myHeader.h" " " indicate the library is located in the same location as other files for this program