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.,<iostream> <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., <iostream> ¨< > 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