Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Copyright © 2002 Pearson Education, Inc. Slide 1.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 6: Functions.
1 Friends and Namespace COSC 1567 C++ Programming Lecture 6.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Concordia University Department of Computer Science and Software Engineering COMP345 Advanced Program Design with C++ Lecture 1: C++ basics 1.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
1 CHAPTER 3 MODULAR PROGRAMMING. 2 Introduction  A library in C is a collection of general purpose and related functions.  2 types of libraries: Standard.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 1:
Chapter 11 Separate Compilation and Namespaces. Learning Objectives Separate Compilation –Encapsulation reviewed –Header and implementation files Namespaces.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
1 Class Features and Design Issues COSC 1567 C++ Programming Lecture 5.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
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.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Part II. 3.8 Placing a Class in a Separate File for Reusability.cpp file is known as a source-code file. Header files ◦ Separate files in which.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
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.
Chapter 12 Classes and Abstraction
Structures and Classes
Classes C++ representation of an object
What Is? function predefined, programmer-defined
Advanced Program Design with C++
Separate Compilation and Namespaces
Chapter Structured Types, Data Abstraction and Classes
More about OOP and ADTs Classes
Separate Compilation and Namespaces
More about OOP and ADTs Classes
Introduction to Classes and Objects
C++ Compilation Model C++ is a compiled language
Classes C++ representation of an object
What Is? function predefined, programmer-defined
Classes and Objects Systems Programming.
Presentation transcript:

Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Learning Objectives Separate Compilation – Encapsulation reviewed – Header and implementation files Namespaces – using directives – Qualifying names – Unnamed namespaces – Hiding helping functions – Nested namespaces 11-2 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Separate Compilation Program Parts – Kept in separate files – Compiled separately – Linked together before program runs Class definitions – Separate from "using" programs – Build library of classes Re-used by many different programs Just like predefined libraries 11-3 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Class Separation Class Independence – Separate class definition/specification Called "interface" – Separate class implementation – Place in two files If implementation changes  only that file need be changed Class specification need not change "User" programs need not change 11-4 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Encapsulation Reviewed Encapsulation principle: – Separate how class is used by programmer from details of class’s implementation "Complete" separation – Change to implementation  NO impact on any other programs Basic OOP principle 11-5 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Encapsulation Rules Rules to ensure separation: 1.All member variables should be private 2.Basic class operations should be: Public member functions Friend or ordinary functions Overloaded operators Group class definition and prototypes together Called "interface" for class 3.Make class implementation unavailable to users of class 11-6 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

More Class Separation Interface File – Contains class definition with function and operator declarations/prototypes – Users "see" this – Separate compilation unit Implementation File – Contains member function definitions – Separate compilation unit 11-7 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Class Header Files Class interface always in header file – Use.h naming convention Programs that use class will "include" it – #include "myclass.h" – Quotes indicate you wrote header Find it in "your" working directory – Recall library includes, e.g., indicate predefined library header file Find it in library directory 11-8 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Class Implementation Files Class implementation in.cpp file – Typically give interface file and implementation file same name myclass.h and myclass.cpp – All class’s member function defined here – Implementation file must #include class’s header file.cpp files in general, typically contain executable code – e.g., Function definitions, including main() 11-9 Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Class Files Class header file #included by: – Implementation file – Program file Often called "application file" or "driver file" Organization of files is system dependent – Typical IDE has "project" or "workspace" Implementation files "combined" here Header files still "#included" Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Multiple Compiles of Header Files Header files – Typically included multiple times e.g., class interface included by class implementation and program file – Must only be compiled once! – No guarantee "which #include" in which file, compiler might see first Use preprocessor – Tell compiler to include header only once Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Using #ifndef Header file structure: – #ifndef FNAME_H #define FNAME_H … //Contents of header file … #endif FNAME typically name of file for consistency, readability This syntax avoids multiple definitions of header file Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Other Library Files Libraries not just for classes Related functions – Prototypes  header file – Definitions  implementation file Other type definitions – structs, simple typedefs  header file – Constant declarations  header file Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Namespaces Namespace defined: A collection of name definitions – Class definitions – Variable declarations Programs use many classes, functions – Commonly have same names – Namespaces deal with this – Can be "on" or "off" If names might conflict  turn off Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

using Directive using namespace std; – Makes all definitions in std namespace available Why might you NOT want this? – Can make cout, cin have non-standard meaning Perhaps a need to redefine cout, cin – Can redefine any others Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Namespace std We’ve used namespace std Contains all names defined in many standard library files Example: #include – Places all name definitions (cin, cout, etc.) into std namespace – Program doesn’t know names – Must specify this namespace for program to access names Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Global Namespace All code goes in some namespace Unless specified  global namespace – No need for using directive – Global namespace always available – Implied "automatic" using directive Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Multiple Names Multiple namespaces – e.g., global, and std typically used What if name defined in both? – Error – Can still use both namespaces – Must specify which namespace used at what time Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Specifying Namespaces Given namespaces NS1, NS2 – Both have void function myFunction() defined differently { using namespace NS1; myFunction(); } { using namespace NS2; myFunction(); } – using directive has block-scope Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Creating a Namespace Use namespace grouping: namespace Name_Space_Name { Some_Code } Places all names defined in Some_Code into namespace Name_Space_Name Can then be made available: using namespace Name_Space_Name Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Creating a Namespace Example Function declaration: namespace Space1 { void greeting(); } Function definition: namespace Space1 { void greeting() { cout << "Hello from namespace Space1.\n"; } } Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

using Declarations Can specify individual names from namespace Consider: Namespaces NS1, NS2 exist Each have functions fun1(), fun(2) – Declaration syntax: using Name_Space::One_Name; – Specify which name from each: using NS1::fun1; using NS2::fun2; Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

using Definitions and Declarations Differences: – using declaration Makes ONE name in namespace available Introduces names so no other uses of name are allowed – using directive Makes ALL names in namespace available Only "potentially" introduces names Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Qualifying Names Can specify where name comes from – Use "qualifier" and scope-resolution operator – Used if only intend one use (or few) NS1::fun1(); – Specifies that fun() comes from namespace NS1 Especially useful for parameters: int getInput(std::istream inputStream); – Parameter found in istream’s std namespace – Eliminates need for using directive or declaration Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Naming Namespaces Include unique string – Like last name Reduces chance of other namespaces with same name Often multiple programmers write namespaces for same program – Must have distinct names – Without  multiple definitions of same name in same scope Results in error Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Class Namespace Example: Display 11.6 Placing a Class in a Namespace (Header File) 11-26Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Class Namespace Example: Display 11.7 Placing a Class in a Namespace (Implementation File) 11-27Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Unnamed Namespaces Compilation unit defined: – A file, along with all files #included in file Every compilation unit has unnamed namespace – Written same way, but with no name – All names are then local to compilation unit Use unnamed namespace to keep things "local" Scope of unnamed namespace is compilation unit Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Global vs. Unnamed Namespaces Not same Global namespace: – No namespace grouping at all – Global scope Unnamed namespace: – Has namespace grouping, just no name – Local scope Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Nested Namespaces Legal to nest namespaces namespace S1 { namespace S2 { void sample() { … } } Qualify names twice: – S1::S2::sample(); Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Hiding Helping Functions Recall helping function: – Low-level utility – Not for public use Two ways to hide: – Make private member function If function naturally takes calling object – Place in class implementation’s unnamed namespace! If function needs no calling object Makes cleaner code (no qualifiers) Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Summary 1 Can separate class definition and implementation  separate files – Separate compilation units Namespace is a collection of name definitions Three ways to use name from namespace: – Using directive – Using declaration – Qualifying Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Summary 2 Namespace definitions are placed inside namespace groupings Unnamed namespace – Used for local name definitions – Scope is compilation unit Global namespace – Items not in a namespace grouping at all – Global scope Copyright © 2008 Pearson Addison-Wesley. All rights reserved.