10.3 Further Data Representation


10.3 Further Data Representation

Data structures are useful and simple structures for grouping together related data, which together forms information. However, as useful as they are, our discussion must now shift away from data structures, and consider a new type of structure that is an extension of data structures. This structure is particularly important to C++, and is called a class. Classes engendered a new era in programming, and programs that extensively use classes are said to be object oriented.

10.3.1 Classes and Object Orientation

Classes can do everything data structures can do, and more. Like data structures, classes are akin to defining data types; that is, they are blueprints for objects that are declared as belonging to this type. A basic class can be declared in the same way as a data structure by substituting the class keyword. Consider the following code:

      class StudentRecord      {         int Student_ID;         int Exam_Results[7];      }; 

10.3.2 Classes as Objects

In addition to inheriting all the properties of data structures, classes offer a more natural representation of real-world data beyond simply grouping together a set of member variables, such as StudentID and Exam_Results. To demonstrate this, let's examine a frog and attempt to represent him in a program.

A frog is a type of animal, and there can be many frogs in this world, but all of these frogs are the same kind of animal-a frog. A frog then, like a class or data structure, is a blueprint for a species. Like all animals and objects in the real world, frogs have properties. Any one frog can be x centimeters high and y centimeters wide. A frog has a color, a certain number of eyes, and so on. So far, these real-world properties can be seen to correspond to the member variables of a data structure quite succinctly.

A frog also has various states and behavior characteristics. For example, a frog can jump, sleep, eat, and so on. This kind of data isn't a property of a frog insofar as it can be represented as being a single value like color or height. Instead, these properties are functional; they are functions of the frog. To summarize, a frog can contain properties and functions, which is our first distinction of a class. Classes can contain member variables and member functions. Member variables are called properties of a class, and member functions are called methods of a class. Take a look at the following sample program:

      #include <iostream>      class Frog      {         int Color;         int Height;         int Width;         void Jump();         void Sleep();         void Eat();      };      void Frog::Jump()      {         //do something here      }      void Frog::Sleep()      {         //do something here      }      void Frog::Eat()      {         //do something here      }      //------------------------------------------------------      int main()      {         return 0;      } 

Note 

Notice how the functions are declared within the scope of the class, but the functions are defined after the class is defined, such as void Frog::Sleep(). Notice also how in those function definitions, the scope operator (::) is used to prefix the class name to the function name (Frog::Sleep) to indicate the class ownership of that function.

10.3.3 Classes and Objects

The typical way to call methods of a class is the same way you access properties of a class, by using the member operator (.). This was demonstrated with data structures. So, to call a function of a class, the following code could be used:

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

Although this code is correct, it will not compile because there is a problem with the class declaration. Something about the Frog class is causing a problem and the compiler indicates the function Eat is private. The next section examines this issue.




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