《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 11 Separate Compilation and Namespaces

Chapter 11 ABSOLUTE C++ Separate Compilation and Namespaces WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved
Chapter 11 Separate Compilation and Namespaces

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

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. 11-3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 11-3 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

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 Copyright006 Pearson Addison-Wesley.All rights reserved. 11-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 11-4 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

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 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 11-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 11-5 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

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. 11-6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 11-6 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

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 Copyright006 Pearson Addison-Wesley.All rights reserved. 11-7
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 11-7 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

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. 11-8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 11-8 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

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() Copyright 2006 Pearson Addison-Wesley.All rights reserved. 11-9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 11-9 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()

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. 11-10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 11-10 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
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 5 Arrays.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 8 Operator Overloading, Friends, and References.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 7 Constructors and Other Tools.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 6 Structures and Classes.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 2 Flow of Control.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 3 Function Basics.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 1 C++ Basics.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 4 Parameters and Overloading.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第三章.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第二章 信源与信息熵.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第一章 绪论(主讲:王军选).ppt
- 《信息论与编码》课程教学资源(作业习题)第三章 信道与信道容量(含解答).pdf
- 《信息论与编码》课程教学资源(作业习题)自测题无答案.pdf
- 《信息论与编码》课程教学实验指导书.pdf
- 《信息论与编码》课程教学大纲 Element of Information Theory and Coding B.pdf
- 《信息论与编码》课程教学大纲 Element of Information Theory and Coding A.pdf
- 《信息安全概论》课程教学资源(PPT课件)第9章 信息安全标准与法律法规.ppt
- 《信息安全概论》课程教学资源(PPT课件)第2章 信息保密技术.ppt
- 《信息安全概论》课程教学资源(PPT课件)第7章 网络安全技术.ppt
- 《信息安全概论》课程教学资源(PPT课件)第8章 信息安全管理.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 12 Streams and File IO.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 10 Pointers and Dynamic Arrays.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 9 Strings.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 13 Inheritance.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 14 Polymorphism and Virtual Functions.ppt
- 《微机技术及应用》课程教学大纲 Microcmputer Technology and aplications.doc
- 《微型计算机技术及应用》课程电子教案(PPT教学课件,共十五章,完整版).pptx
- 《计算机导论》课程教学大纲 Computer Concepts.pdf
- 《计算机导论》课程教学课件(英文讲稿)1-a-Computer History+ Di Devices.pdf
- 《计算机导论》课程教学课件(英文讲稿)1-b-Digital Data Representation.pdf
- 《计算机导论》课程教学课件(英文讲稿)2-a-Computer Hardware.pdf
- 《计算机导论》课程教学课件(英文讲稿)2-b-Computer Hardware.pdf
- 《计算机导论》课程教学课件(英文讲稿)3-a-b-Computer Software.pdf
- 《计算机导论》课程教学课件(英文讲稿)4- operating system.pdf
- 《计算机导论》课程教学课件(英文讲稿)4-a- File mangement.pdf
- 《计算机导论》课程教学课件(英文讲稿)5-a- LANS_WANS.pdf
- 《计算机导论》课程教学课件(英文讲稿)5-b- LANS_WANS.pdf
- 《计算机导论》课程教学课件(英文讲稿)6-a- The Internet.pdf
- 《计算机导论》课程教学课件(英文讲稿)6-b- The Internet.pdf
- 《计算机导论》课程教学课件(英文讲稿)7-a- Web.pdf