[ fromfile: classes.xml id: memberaccess ]
Client code is code that is outside the scope of the class, but which uses objects or members of that class.
client code #includes
the header file that contains the class definition.
In Example 2.5, you can see that fraction.h
contains class definition code of Fraction
, and is itself a client of QString.
Example 2.5. src/classes/fraction/fraction.h
The access specifiers, public, protected
, and private
, are used in a class definition to specify where in a program the affected members can be accessed.
The following list provides an informal first approximation of the definitions of these three terms.
Refinements are contained in footnotes.
A public
member can be accessed (using an object of the class)[13] anywhere in a program that #includes
the class
definition file.
A protected
member can be accessed inside the definition of a member function of its own class, or a member function of a derived class.[14]
A private
member is only accessible by member functions of its own class.[15]
Class members are private
by default.
Example 2.6. src/classes/fraction/fraction-client.cpp
#include <QTextStream> #include "fraction.h" int main() { const int DASHES = 30; QTextStream cout(stdout); { int i; for (i = 0; i < DASHES; ++i) cout << "="; cout << endl; } cout << "i = " << i << endl; Fraction f1, f2; f1.set(3, 4); f2.set(11,12); f2.m_Numerator = 12; cout << "The first fraction is: " << f1.toString() << endl; cout << "\nThe second fraction, expressed as a double is: " << f2.toDouble() << endl; return 0; }
[13] public static
members can be accessed without an object. We discuss this in Section 2.9.
[15] Private members are also accessible by friends
of the class, which we discuss in Section 2.6.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |