C++
Chapter 7 - Classes Part 2
const
const is a c++ key word which creates a "read" only object. The decision to use a const object is determined by the client side programmer, NOT the class designer. The class designer must therefore design the class to be able to create const instances.
Example of a using a client side const object:
int main ( void ) { const Vertex origin(0.0, 0.0, 0.0); origin.Show(); return(0); }The vertex class design must include const member functions so that the const object can utilize the const method to operate with the const object.
For Example:
Class design not taking into account a client side const object. The compiler will create an error saying that Show is not const.
class Vertex { public: Vertex ( double _x=0.0, double _y=0.0, double _z=0.0 ) { x=_x; y=_y; z=_z; } Set ( double _x, double _y, double _z ) { x=_x; y=_y; z=_z; } Show ( void ) { std::cout << "[" << x << "," << y << "," << z << "]\n"; } };We now redesign the class to make all method which do not modify the object into const functions
class Vertex { public: Vertex ( double _x=0.0, double _y=0.0, double _z=0.0 ) { x=_x; y=_y; z=_z; } Set ( double _x, double _y, double _z ) { x=_x; y=_y; z=_z; } Show ( void ) const { std::cout << "[" << x << "," << y << "," << z << "]\n"; } };The following table illustrates the const requirements for objects and functions
non-const method const method non-const object X X const object - X
composition
Object which contain other object are composed of those object. If Object A contains Object B contains Object C then when Object A is constructed, Object C, Object B then Object A are constructed.
// order.cpp #include <iostream> class A; class B; class C; class A { public: A(){ std::cout << "Constructed: A\n"; } ~A(){ std::cout << "Destructed : A\n"; } private: B b1; }; class B { public: B(){ std::cout << "Constructed: B\n"; } ~B(){ std::cout << "Destructed : B\n"; } private: C c1; }; class C { public: C(){ std::cout << "Constructed: C\n"; } ~C(){ std::cout << "Destructed : C\n"; } }; int main ( void ) { A a1; return(0); }
friend Functions and Classes
When a class friends a global function or a function from another class, the friended function will now have access to the private data members of the friending class. This feature should be used with great caution as it breaks the class protection mechanisms.
// friends.cpp #include <iostream> class Foo; void ShowIt ( Foo & f ) { std::cout << "Foo x = " << f.x << std::endl; } class Foo { public: friend void ShowIt ( foo & f ); private: int x; }; int main ( void ) { Foo f1; ShowIt( f1 ); return(0); }A class can also friend another class. This allows the friended class full access to the friending class. This condition creates a conjoined class and breaks encapsulation.
this pointer
Every class has a pointer to itself. This pointer is called the "this" pointer. // Fig. 7.7: fig07_07.cpp // Using the this pointer to refer to object members. #include <iostream> class Test { public: Test( int = 0 ); // default constructor void print() const; private: int x; }; Test::Test( int a ) { x = a; } // constructor void Test::print() const // ( ) around *this required { std::cout << " x = " << x << "\n this->x = " << this->x << "\n(*this).x = " << ( *this ).x << std::endl; } int main() { Test testObject( 12 ); testObject.print(); return 0; }
static data members
Static data members of classes are store in global scope and are shared by all instances of the class for which they are specified. Static members can server as instance counters
Static class members must be initialized in a global area of the program.
// example of static member in class #include <iostream> class foo { private: static int x; // x is declared but not defined public: static void set_x (int xin) { x = xin; } static int get_x ( void ) { return (x); } }; // x must be defined to the compiler outside the eyes // of the calss definition to allocate memory for the // x object int foo::x = 0; // now x is defined int main ( void ) { // notice no objects of type foo are created. foo::set_x(0); std::cout << foo::get_x() << std::endl; foo::set_x(100); std::cout << foo::get_x() << std::endl; return (0); }
Code Example:coin.h coin.cpp coinflip.cpp
04/12/00 03:51 PM