📄 ch03.htm
字号:
For example, you know from the console mode programs you created that the following
statement will cause the characters <TT>Hello World!</TT> to be displayed on your
screen:</P>
<PRE><FONT COLOR="#0066FF"><TT>cout << "Hello World!" << endl;</TT>
</FONT></PRE>
<H3><FONT COLOR="#000077"><B>Declarations</B></FONT></H3>
<P>A declaration is another type of statement. As discussed earlier, declarations
introduce a variable to the compiler. The following line is an example of a simple
declaration:</P>
<PRE><FONT COLOR="#0066FF"><TT>int myAge;</TT>
</FONT></PRE>
<P>This tells the compiler that <TT>myAge</TT> is an integer.
<H3><FONT COLOR="#000077"><B>Assignment</B></FONT></H3>
<P>An assignment expression is used to assign a value to a variable, using the assignment
operator, <TT>=</TT>, as follows:</P>
<PRE><FONT COLOR="#0066FF"><TT>int myAge;</TT>
<TT>myAge = 135;</TT>
</FONT></PRE>
<P>Every expression has a value. The value of an assignment expression is the value
of the assignment. This means that the following statement assigns the value <TT>42</TT>
to the variables <TT>yourAge</TT> and <TT>myAge</TT>:</P>
<PRE><FONT COLOR="#0066FF"><TT>myAge = yourAge = 42;</TT>
</FONT></PRE>
<P>The program in Listing 3.4 demonstrates how to assign a value to a variable.
<H4><FONT COLOR="#000077">TYPE: Listing 3.4. A C++ program that assigns a value to
a variable.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>#include <iostream></TT>
<TT>using namespace std;</TT>
<TT>int main()</TT>
<TT>{</TT>
<TT> int myAge;</TT>
<TT> myAge = 42;</TT>
<TT> cout << "Hello" << endl;</TT>
<TT> cout << "My age is " << myAge << endl;</TT>
<TT> return 0;</TT>
<TT>}</TT>
</FONT></PRE>
<P>The assignment operator is just one example of the operators available in C++.
More operators are discussed in the next section.
<H3><FONT COLOR="#000077"><B>Other Common Expressions and Operators</B></FONT></H3>
<P>The C++ language contains operators that you can use to write addition, subtraction,
multiplication, and other expressions. Some common math operators are shown in Table
3.1.
<H4><FONT COLOR="#000077">Table 3.1. Some common math operators used in C++.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><B>Operator</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Description</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>+</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Addition</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Subtraction</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>/</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Division</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>*</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Multiplication</TD>
</TR>
</TABLE>
</P>
<P>All math operators group from left to right. The multiplication and division operators
have a higher precedence than the addition and subtraction operators. This means
that the following expressions are equivalent: a + 5 * 3 a + 15 You can use parentheses
to force an expression to be evaluated in a preferred order. Note the grouping of
the following expression: (a + 5) * 3 This expression adds 5 to the value stored
in a and then multiplies that value by 3. The math operators can also be combined
with an assignment operator, as follows:</P>
<PRE><FONT COLOR="#0066FF"><TT>int myAge;</TT>
<TT> myAge = 40 + 2;</TT>
</FONT></PRE>
<P>The expression <TT>40 + 2</TT> has a value of <TT>42</TT>. After that value is
calculated, the value of the expression is stored in the <TT>myAge</TT> variable.
<H2><FONT COLOR="#000077"><B>Rectangles and Regions</B></FONT></H2>
<P>The rectangle is a fundamental component of most Windows programs. Because most
windows and controls are rectangular, it isn't surprising that one of the most commonly
used data structures in Windows programming is used to represent a rectangle.</P>
<P>Rectangles are often used to represent the position or size of all types of windows:
main windows as well as controls, toolbars and dialog boxes. There are two basic
types of rectangle coordinates:
<UL>
<LI>Screen rectangle coordinates, which place a rectangle in relationship to the
entire screen
<LI>Client rectangle coordinates, which always have their top and left values set
to zero and provide the size of a rectangle that represents a window's client area
</UL>
<P>Screen rectangle coordinates are often used when moving a window in relation to
the entire screen. Client rectangles are most commonly used when positioning controls
or drawing inside a control or other window.</P>
<P>When requesting the dimensions of a rectangle, you must pass a <TT>CRect</TT>
variable to one of the Windows rectangle functions. The following two lines of code
declare an instance of <TT>CRect</TT> as a variable and pass it to the <TT>GetClientRect</TT>
function:</P>
<PRE><FONT COLOR="#0066FF"><TT>CRect rcClient;</TT>
<TT>GetClientRect(rcClient);</TT>
</FONT></PRE>
<P>The next example uses a client area rectangle to display a message to the user,
just like the HelloMFC program in the first hour. The new example will draw the message
in the center of the client area; if the window is resized, the message will be redrawn
in the center of the new rectangle.</P>
<P>Create an MFC AppWizard application named HelloRect, following the steps presented
in the first hour. Modify the <TT>OnDraw</TT> function found in the <TT>CHelloRectView</TT>
class so that it looks like the function shown in Listing 3.5.
<H4><FONT COLOR="#000077">TYPE: Listing 3.5. Using a rectangle to center a message
in a window.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CHelloRectView::OnDraw(CDC* pDC)</TT>
<TT>{</TT>
<TT> CRect rcClient;</TT>
<TT> GetClientRect(rcClient);</TT>
<TT> pDC->DrawText("Hello Client Rectangle!", -1, rcClient,</TT>
<TT> DT_SINGLELINE | DT_CENTER | DT_VCENTER );</TT>
<TT>}</TT>
</FONT></PRE>
<P>Build the HelloRect application, and run it from Developer Studio. Note that if
you resize the window, the message is redrawn so that it remains in the center of
the client area.
<H2><FONT COLOR="#000077"><B>Summary</B></FONT></H2>
<P>In this hour, you have learned about some of the more advanced building blocks
that make up C++ programs: functions, structures, and classes. You also looked at
some basic information about the MFC class library and built an MFC application without
using ClassWizard.
<H2><FONT COLOR="#000077"><B>Q&A</B></FONT></H2>
<DL>
<DD><B>Q What is the difference between a rectangle that uses screen coordinates
and a rectangle that uses client coordinates?</B><BR>
<BR>
<B>A</B> Every window in a Windows application can be represented by a rectangle;
this rectangle will typically use either screen or client coordinates. The rectangle
that results from these coordinates is always the same, the difference is only in
the point of reference that is used to measure the rectangle.<BR>
<BR>
<B>Q Can a structure have member functions?</B><BR>
<BR>
<B>A</B> Absolutely. A class and a structure are exactly the same, except that all
members of a structure are accessible by default, while class members are private
(not accessible) by default. You will learn more about access restrictions in the
next hour.<BR>
<BR>
<B>Q Why is no function prototype required for <TT>main()</TT>?</B><BR>
<BR>
<B>A</B> The short answer: because the C++ standard says you don't need one. The
purpose of function prototypes is to introduce new functions to the compiler; because
every C++ program is required to have a main function, no function is necessary.
</DL>
<H2><FONT COLOR="#000077"><B>Workshop</B></FONT></H2>
<P>The Workshop is designed to help you anticipate possible questions, review what
you've learned, and begin thinking ahead to putting your knowledge into practice.
The answers to the quiz are in Appendix B, "Quiz Answers."
<H3><FONT COLOR="#000077"><B>Quiz</B></FONT></H3>
<DL>
<DD>1. What are some examples of the different types of windows found in a Windows
application?<BR>
<BR>
2. What is a function?<BR>
<BR>
3. What are the four parts of a function definition?<BR>
<BR>
4. How are classes different from structures?<BR>
<BR>
5. What function is called when an instance of a class is created?<BR>
<BR>
6. What function is called when an instance of a class is destroyed?<BR>
<BR>
7. What is the difference between the client and non-client areas?<BR>
<BR>
8. What is the value of the expression <TT>a = 42</TT>?<BR>
<BR>
9. What symbol is used for multiplication?<BR>
<BR>
10. What symbol is used for division?
</DL>
<H3><FONT COLOR="#000077"><B>Exercises</B></FONT></H3>
<DL>
<DD>1. Write a console-mode program that asks for a distance in miles and converts
the distance into feet. There are 5,280 feet in a mile.<BR>
<BR>
2. Modify the HelloWin program to display different messages in different parts of
the main window.
</DL>
<CENTER>
<P>
<HR>
<A HREF="../ch02/ch02.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch04/ch04.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR>
<BR>
<BR>
<IMG SRC="../button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"
BORDER="0"></P>
<P>© <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -