10.4 Access Specifiers


10.4 Access Specifiers

The properties of Frog are: Color, Height, and Width. The methods of Frog are: Jump(), Sleep(), and Eat(). Both properties and methods of a class have access specifiers, which determine how properties and methods can be accessed and used outside of the class.

Classes are designed to be self-contained, manageable units. Since each member variable of a class is local to the class, when the class is deleted so are its member variables and functions. Therefore, the member variable and function lifetimes are that of the class lifetime. Member functions and variables, then, can be called and accessed by other member functions of the class without problem. This kind of access is said to occur from inside the class. Like so:

      class Frog      {         int Color;         int Height;         int Width;         void Jump();         void Sleep();         void Eat();      };      void Frog::Jump()      {         Color = 0; // Assigns value to class member      } 

Our attempt to call the Eat function of the class was an example of access from outside the class. Like so:

      Frog Frg;      Frg.Eat();       //Make him eat 

However, access from outside the class is different from access within the class. Whether or not access outside the class is allowed depends on the type of access specifier a member function or variable is given. The next section details these.

10.4.1 Classes and Access Specifiers

When a property or a method of a class is declared, it can have one of several types of access specifiers. Two kinds shall be explored here: private and public. Consider the following Frog class, rewritten to account for access specifiers:

      class Frog      {         private:            int Color;            int Height;            int Width;         public:            void Jump();            void Sleep();            void Eat();      }; 
  • Private

    Properties and methods declared as private members can only be accessed by methods of the class, not from outside. By default, members are declared as private.

  • Public

    Properties and methods declared as public can be accessed both from within the class and from outside the class, by member functions and by non-member functions alike.

10.4.2 Private and Public

Setting different access specifiers-such as public or private-for different class properties and methods can be useful. One of the most important of these uses relates to code portability. Classes are designed to be self-contained, portable units of code that can be transferred from program to program. In this way, classes can be recycled between programs, saving programmers a great amount of work. However, when different programmers share and reuse large and complex classes, it can be difficult to tell which properties and methods are intended to be manipulated from outside the class, and which are only relevant to the internal workings of the class. The access specifiers are a good method for indicating which members are private and important internally for the class, and which members are public and intended to be changed or called from outside the class. Consider the following sample program:

      Class cCar      {           private:             //Private members can be accessed only in the class             int m_Speed;                //Speed of car             int m_DistanceToDest;       //Distance remaining until dest             int m_DistanceTravelled;    //Distance already traveled           public:             //Public members can be accessed both internally and externally             //Current position of car in 3D space             int m_XPos;             int m_YPos;             int m_ZPos;      }; 

10.4.3 Constructors and Destructors

A class is a blueprint for created objects, and may contain properties and methods that can have various accessors, namely private or public. Classes can also have two special kinds of methods: constructors and destructors. In C++, a constructor is a method of a class that is executed whenever an object of the class is created (such as Frog MyFrog;). A destructor is a method that is executed whenever an object is terminated. In other words, the constructor and destructor are functions called at crucial moments in the lifespan of a class. The constructor is typically used to initialize the properties of a class to starting values, and the destructor is used to perform any required processing when the class is destroyed.




Introduction to Game Programming with C++
Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn

Similar book on Amazon
Beginning C++ Through Game Programming
Beginning C++ Through Game Programming
Beginning Game Programming
Beginning Game Programming
The Ultimate Guide to Video Game Writing and Design
The Ultimate Guide to Video Game Writing and Design

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net