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

Slides:



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

2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Chapter 15 Polymorphism and Virtual Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Virtual Function.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
Copyright © 2002 Pearson Education, Inc. Slide 1.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
0 - 0.
Addition Facts
Chapter 16 Templates. Learning Objectives Function Templates – Syntax, defining – Compiler complications Class Templates – Syntax – Example: Pair class.
Addition 1’s to 20.
Week 1.
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
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.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Friends and Namespace COSC 1567 C++ Programming Lecture 6.
Chapter 3 Getting Started with C++
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
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.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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 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.
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.
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
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.
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
Presentation transcript:

Chapter 11 Separate Compilation and Namespaces

Copyright © 2006 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

Copyright © 2006 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

Copyright © 2006 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

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

Copyright © 2006 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

Copyright © 2006 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

Copyright © 2006 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

Copyright © 2006 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 classs member function defined here Implementation file must #include classs header file.cpp files in general, typically contain executable code e.g., Function definitions, including main()

Copyright © 2006 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 © 2006 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 © 2006 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 © 2006 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 © 2006 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 © 2006 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 © 2006 Pearson Addison-Wesley. All rights reserved Namespace std Weve 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 doesnt know names Must specify this namespace for program to access names

Copyright © 2006 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 © 2006 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 © 2006 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 © 2006 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 © 2006 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 © 2006 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 © 2006 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 © 2006 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 istreams std namespace Eliminates need for using directive or declaration

Copyright © 2006 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 © 2006 Pearson Addison-Wesley. All rights reserved Class Namespace Example: Display 11.6 Placing a Class in a Namespace (Header File)

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

Copyright © 2006 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 © 2006 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 © 2006 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 © 2006 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 implementations unnamed namespace! If function needs no calling object Makes cleaner code (no qualifiers)

Copyright © 2006 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 © 2006 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