STORING AND RETRIEVING DATA


Data manipulation is a fundamental part of the programs you write in Visual C++. Every program requires the ability to store data and then later to retrieve and process it. The projects from earlier chapters are excellent examples of this. Visual C++ provides many options for manipulating data so that your applications can perform the tasks your users need.

Hint 

Data is information that your application collects, stores, recalls, and modifies during its execution.

Alternative Ways of Storing Data

Visual C++ is an extremely flexible programming language, and as such it offers a wide variety of ways to solve a particular problem or perform a certain task. This is especially true when it comes to working with data. Visual C++ provides a number of different options, each of which is best suited to different situations. The options that you decide to use will vary based on the challenge you are faced with. Often, the best method for storing data varies based on the manner in which the data is used.

  • Constants. These data types are best when you are dealing with immutable quantities. Because they cannot be changed, you can rely on them to be the same throughout your program. A good example of a constant data type is pi.

  • Variables. Whereas constants are perfect for storing unchanging and unchangeable data, variables, as their name implies, are best suited for storing data that is expected to change frequently.

  • Arrays. Sometimes you need to store and work with large collections of related information, such as a list of names or highest scores. These situations are perfect for an array because this data type allows you to work with both the list and the individual elements of it when you need to.

  • Structures. A structure is a user-defined data type that you design to suit your needs. It allows you to group related variables into a single variable. You could, for instance, create a customer variable that stored a person's name and customer number. Your application could then refer back to either one later, but use a single variable to do so.

    Trick 

    One of the most powerful options for manipulating data is the class. This option is covered later in Chapter 9, "Getting Comfortable with Object-Oriented Programming."

Data Types

Visual C++ is capable of storing and working with many different data types. Table 5.1 lists each of the supported data types. Each data type, except the Object data type, is capable of storing a specific type of data. The limits on the range of possible values that a given data type can store vary based on the type.

Table 5.1: Visual C++ Supported Data Types

Data Type

Value Range

Boolean

true or false

Byte

0 to 255

Char

0 to 65,535

DateTime

January 1, 0001 to December 31, 9999

Decimal

1.0×10-28 to approximately 7.9×1028 with 28 to 29 significant digits

Double

5.0×10-324 to 1.7×10308 with a precision of 15–16 digits

Int16

-32,768 to 32,767

Int32

-2,147,483,648 to 2,147,483,647

Int64

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Object

A generic type that is the basis of all other types

Sbyte

-128 to 127

Single

1.5×10-45 to 3.4×1038 with a precision of 7 digits

String

Up to two billion characters

UInt16

0 to 65,535

UInt32

0 to 4,294,967,295

UInt64

0 to 18,446,744,073,709,551,615

Trick 

Although you can always use the largest data type possible to store information, your programs in fact benefit from using the smallest possible. When you specify the correct data type to meet the task that your application is performing, you enable your Visual C++ programs to run faster. This bonus comes from reducing the amount of memory used by the application and by eliminating the need to later convert data from one data type to another.

By specifying a data type, you tell Visual C++ the range of allowable values for your variable. You also indicate to Visual C++ what actions you want to perform on the variable. For example, by specifying one of the numeric data types, you tell Visual C++ that it can perform mathematic calculations. On the other hand, if you specify string data, you tell Visual C++ that you want to work with text.




Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
ISBN: 735615381
EAN: N/A
Year: 2005
Pages: 131

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