Separate Compilation and Namespaces

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.
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Libraries Making Functions Globally Reusable (§ 6.4) 1.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Separating Definition & Implementation Headers and Comments.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Makefile M.A Doman. Compiling multiple objects Card.cpp -> Card.o Deck.cpp -> Deck.o main.cpp -> main.o main.o Deck.o Card.o -> Dealer.exe.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
38 4/11/98 CSE 143 Modules [Chapter 2]. 39 4/11/98 What is a Module?  Collection of related items packaged together  Examples:  Stereo System Components.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
Chapter 11 Separate Compilation and Namespaces. Learning Objectives Separate Compilation –Encapsulation reviewed –Header and implementation files Namespaces.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
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.
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.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Introduction to Compiling M.A Doman. What is a compiler? A compiler is a program that reads a program written in one language and translates it into another.
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.
Review Why do we use protection levels? Why do we use constructors?
13 C Preprocessor.
Chapter 12 Classes and Abstraction
What Is? function predefined, programmer-defined
Advanced Program Design with C++
Andy Wang Object Oriented Programming in C++ COP 3330
Compilation and Debugging
Compilation and Debugging
Chapter 13 - The Preprocessor
Chapter Structured Types, Data Abstraction and Classes
More about OOP and ADTs Classes
Pre-processor Directives
Separate Compilation and Namespaces
Structures putting data together.
Separate Compilation.
More about OOP and ADTs Classes
C Preprocessor(CPP).
Separating Definition & Implementation
Introduction to Classes and Objects
Comments, Prototypes, Headers & Multiple Source Files
Code Organization CSCE 121 J. Michael Moore.
C++ Compilation Model C++ is a compiled language
Chapter 6: User-Defined Functions I
C Preprocessor Seema Chandak.
What Is? function predefined, programmer-defined
Class rational part2.
Lecture 8 Object Oriented Programming (OOP)
More C++ Classes Systems Programming.
Presentation transcript:

Separate Compilation and Namespaces Chapter 12 Separate Compilation and Namespaces

12.1 Separate Compilation

Separate Compilation C++ allows you to divide a program into parts Each part can be stored in a separate file Each part can be compiled separately A class definition can be stored separately from a program. This allows you to use the class in multiple programs

ADT Review An ADT is a class defined to separate the interface and the implementation All member variables are private The class definition along with the function and operator declarations are grouped together as the interface of the ADT Group the implementation of the operations together and make them unavailable to the programmer using the ADT

The ADT Interface The interface of the ADT includes The class definition The declarations of the basic operations which can be one of the following Public / private member data Public / private member function prototypes The function comments

The ADT Implementation The implementation of the ADT includes The function definitions The public member functions The private member functions Non-member functions Private helper functions Member variables Other items required by the definitions

Separate Files In C++ the ADT interface and implementation can be stored in separate files The interface file stores the ADT interface The implementation file stores the ADT implementation

A Minor Compromise The public part of the class definition is part of the ADT interface The private part of the class definition is part of the ADT implementation This would hide it from those using the ADT C++ does not allow splitting the public and private parts of the class definition across files The entire class definition is usually in the interface file

Case Study: DigitalTime The interface file of the DigitalTime ADT class contains the class definition The values of the class are: Time of day, such as 9:30, in 24 hour notation The public members are part of the interface The private members are part of the implementation The comments in the file should provide all the details needed to use the ADT

Naming The Interface File The DigitalTime ADT interface is stored in a file named dtime.h The .h suffix means this is a header file Interface files are always header files A program using dtime.h must include it using an include directive #include "dtime.h" Display 12.1

Circle.h Circle.cpp main.cpp Compiler Preprocessor Circle.o Library functions Linker main.o circleTest (a.out)

Preprocessing The first pass of the compiler Reads each source file Looks for macro directives which beging with ‘#’ Every time it hits an directive #include it goes and gets the header file named. It then reads and processes it. Passes the results to the next phase of the compiler.

#include " " or < > ? To include a predefined header file use < and > #include <iostream> < and > tells the compiler to look where the system stores predefined header files To include a header file you wrote, use " and " #include "dtime.h" " and " usually cause the compiler to look in the current directory for the header file

The Implementation File Contains the definitions of the ADT functions Usually has the same name as the header file but a different suffix Since our header file is named dtime.h, the implementation file is named dtime.cpp Suffix depends on your system (some use .cxx or .CPP)

#include “circle.h" The implementation file requires an include directive to include the interface file: #include “circle.h"

The Application File The Application file is the file that contains the program that uses the ADT It is also called a driver file Must use an include directive to include the interface file: #include “circle.h"

Running The Program Basic steps required to run a program: (Details vary from system to system!) Compile the implementation file Compile the application file Link the files to create an executable program using a utility called a linker Linking is often done automatically

Compile dtime.h ? The interface file is not compiled separately The preprocessor replaces any occurrence of #include "dtime.h" with the text of dtime.h before compiling Both the implementation file and the application file contain #include "dtime.h" The text of dtime.h is seen by the compiler in each of these files There is no need to compile dtime.h separately

Why Three Files? Using separate files permits The ADT to be used in other programs without rewriting the definition of the class for each Implementation file to be compiled once even if multiple programs use the ADT Changing the implementation file does not require changing the program using the ADT

Multiple Classes A program may use several classes Each could be stored in its own interface and implementation files Some files can "include" other files, that include still others It is possible that the same interface file could be included in multiple files C++ does not allow multiple declarations of a class The #ifndef directive can be used to prevent multiple declarations of a class

Preprocessing Macro preprocessing Allows the user to define macros that are shorthand for larger constructs Define variables/identifiers #define _card.h #define BUFFER_SIZE 256 Conditional expressions #if .. #else

Introduction to #ifndef To prevent multiple declarations of a class, we can use these directives: #define CIRCLE_H adds CIRCLE_H to a list indicating CIRCLE_H has been seen #ifndef CIRCLE_H checks to see if CIRCLE_H has been defined #endif If CIRCLE_H has been defined, skip to #endif

Using #ifndef true false Consider this code in the interface file #ifndef CIRCLE_H #define CIRCLE_H < The circle class definition goes here> #endif The first time a #include “circle.h" is found, CIRCLE_H and the class are defined The next time a #include “circle.h" is found, all lines between #ifndef and #endif are skipped true false

Chapter 12 -- End