Professional Documents
Culture Documents
Friend and Namespace
Friend and Namespace
Objectives
What a friend is Declare a friend function Use a friend function with data from two classes Use a forward reference Use a friend function with two or more instances of the same class bestow friendship on functions that are members of other classes Bestow friendship on an entire class
Objectives
Separate Compilation
Encapsulation reviewed Header and implementation files
Namespaces
using directives Qualifying names Unnamed namespaces Hiding helping functions Nested namespaces
Friend Functions
Nonmember functions
Recall: operator overloads as nonmembers
They access data through accessor and mutator functions Very inefficient (overhead of calls)
Friend Functions
Friend function of a class
Not a member function Has direct access to private members
Just as member functions do
8-6
10
The function is not found to be a member or a friend, and access is denied; you receive an error message
11
Ex6-1.cpp
12
13
14
15
16
Advantageous?
For operators: very! Allows automatic type conversion Still encapsulates: friend is in class definition Improves efficiency
18
19
20
21
Ex6-2.cpp
22
When two classes refer to each other, you can choose to forward declare either one, and then define the other class first
The same principle holds true if three, four, or more classes share a friend function that makes reference to all the classes
23
24
25
You simply create a member function for the CustTransaction class The prototype is: CustTransaction addTransactions (const CustTransaction aPayment);
26
Ex6-4.cpp
27
A copy of the entire, newly constructed and defined billingSummary is returned to the calling function, where totalTrans receives it
One way to avoid a subsidiary transaction is to create a friend function to the CustTransaction class
28
Ex6-5.cpp
29
30
The other class, named CourseRec, holds information about one section of a course, including a section number, enrollment limit, and current enrollment
31
33
34
35
Ex6-8.cpp
36
37
Friend Classes
Entire classes can be friends
Similar to function being friend to class Example: class F is friend of class C
All class F member functions are friends of C NOT reciprocated Friendship granted, not taken
38
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
39
Class Separation
Class Independence
Separate class definition/specification
Called "interface"
43
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
41
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 Called "interface" for class
42
Namespaces
Namespace defined: A collection of name definitions
Class definitions Variable declarations
43
using Directive
using namespace std;
Makes all definitions in std namespace available
44
Namespace std
Weve used namespace std Contains all names defined in many standard library files Example: #include <iostream>
Places all name definitions (cin, cout, etc.) into std namespace Program doesnt know names Must specify this namespace for program to access names
45
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
46
Multiple Names
Multiple namespaces
e.g., global, and std typically used
47
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
48
Creating a Namespace
Use namespace grouping:
namespace Name_Space_Name { Some_Code }
49
Function definition:
namespace Space1 { void greeting() { cout << "Hello from namespace Space1.\n"; } }
50
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;
51
using directive
Makes ALL names in namespace available Only "potentially" introduces names
52
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
53
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
54
Unnamed Namespaces
Compilation unit defined:
A file, along with all files #included in file
Use unnamed namespace to keep things "local" Scope of unnamed namespace is compilation unit
55
Unnamed namespace:
Has namespace grouping, just no name Local scope
56
Nested Namespaces
Legal to nest namespaces
namespace S1 { namespace S2 { void sample() { } }
Ex6-10.cpp
Summary
A friend function is a function that can access private data members of a class, even though the function itself is not a member of the class A friend function does not use the class name and the scope resolution operator in the function header You can create a function that is a friend to multiple classes
58
Summary
If you want a function to have access to two or more instances of the same class, you can use either a class member function or a friend function Classes can bestow friendship on functions that are members of other classes Bestow friendship on an entire class
Every function that is a member of the class becomes a friend of the granting class
59
Summary
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
60
Summary
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
61