Built-in Functions


Just as ANSI standard C++ has a number of functions in header files that you can use in your programs, so does Visual C++. An exhaustive look at all of these is well beyond the scope of this book. However, we can take a look at some very commonly used functions. To begin with, Visual C++ supports all the header files, and all their constituent functions that you found in ANSI standard C++. It just adds a few more.

One of the most obvious things that Visual C++ offers is increased support for strings. In Visual C++, you need not deal directly with character arrays. There is a string variable that is actually a class, with many methods in it. The following example will illustrate this to you.

Example 15.2

Step 1: Create a standard dialog application as you did in Example 15.1. You will use the MFC Application Wizard (exe) to create a basic dialog applications. The only difference is that you should name it 15_02.

Step 2: Place four static boxes and four edit boxes on the dialog box as you see in Figure 15.18.

click to expand
Figure 15.18: Example 15.2 layout.

Step 3: Using the right mouse button to view the properties of each of the components, you will change the captions on the static boxes as you see in Figure 15.19. Also add another button with the caption String Operations.

click to expand
Figure 15.19: Labeling the static boxes.

Step 4: With each of the Edit boxes, you will right-click on them and launch the class wizard. Using the second tab, member variables, you will assign each of the Edit boxes a string variable as you see in Figure 15.20.

click to expand
Figure 15.20: Assigning variables to the Edit boxes.

Step 5: Now we just need to add a little bit of code into the string operations button. Simply double-click on the button. (This will add the OnButton1 function to that button.) Then add the code you see here.

void CExample15_02Dlg::OnButton1()  {      // get data from controls to variables.      UpdateData(true);      // copy data to the other string variables.      m_uppercase =  m_rawstring;      m_lowercase = m_rawstring;      m_reverse = m_rawstring;      // perform string operations.      m_uppercase.MakeUpper();      m_lowercase.MakeLower();      m_reverse.MakeReverse();      // get data from variables to controls      UpdateData(false);          } 

Step 6: When you execute the program, you should enter some text into the first Edit box then click the strong operations button. You will see something much like what is depicted in Figure 15.21.

click to expand
Figure 15.21: String operations.

This example should have accomplished several things. First of all, it should have given you another chance to work with the Application Wizard, Class Wizard, and with basic Visual C++ components. Secondly, this example introduced you to some of the functions associated with strings in Visual C++. Remember that, in Visual C++, the string is not simply an array of characters; rather, it is a class. In fact, it is called the CString class. As with any class, it has certain methods. You have just seen the MakeUpper, MakeLower, and MakeReverse methods. Table 15.5 summarizes the main methods of the string.

Table 15.5: Methods of the String Class

Method

Purpose

GetLength

This method returns an integer that tells the number of characters in the string.

IsEmpty

This property returns true if the string contains no characters.

Compare

This method compares two strings to see if they are equal.

Mid

This allows you to extract the middle characters from a string.

Left

This method allows you to extract the left side of a string.

Right

This method is used to extract the right side of a string.

MakeUpper

This method returns a string that is the upper case of the string passed to it.

MakeLower

This method returns a string that is the lower case of the string passed to it.

MakeReverse

This method returns a string that is the reverse of the string passed to it.

Find

This method searches to find a series of characters within a string.

Hint!

You should notice that we did not use the class wizard to associate a function with the button. We simply double-clicked on the button. Each component has a default function associated with it. If you double-click on the component, it will automatically associate the default component with it.

There are other methods in CString but these are the most commonly used. You will find that Visual C++ offers a plethora of useful functions that can help you. Covering all such functions, or even a significant portion thereof, is well beyond the scope of this book. However, you will find a few commonly used functions demonstrated in this section of the book, and you can find even more by simply searching through the Microsoft Help files that come with Visual Studio.

Hint!

Although these Visual C++ strings are very similar to the standard C++ string and the C style strings, they are not exactly the same.




C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

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