⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch03.htm

📁 Visual C++ 的学习资料 Visual C++ 的学习资料
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
	
	<TITLE>Teach Yourself Visual C++&#174; 5 in 24 Hours -- Hour 3 -- Structures, Classes, and the MFC Class Library</TITLE>
</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF">

<CENTER>
<H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
<FONT COLOR="#000077">Teach Yourself Visual C++&#174; 5 in 24 Hours</FONT></H1>
</CENTER>
<CENTER>
<P><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> 
<HR>

</CENTER>
<CENTER>
<H1><FONT COLOR="#000077">- Hour 3 -<BR>
Structures, Classes, and the MFC Class Library</FONT></H1>
</CENTER>
<P>In the first two hours, you have learned some of the basic concepts behind C++,
and you have written some simple programs. In this hour, you will be introduced to
some more advanced Visual C++ programming topics. In particular, you will learn

<UL>
	<LI>How functions are used to provide small reusable chunks of code<BR>
	<BR>
	
	<LI>How structures and classes are used to create source code and data components<BR>
	<BR>
	
	<LI>How expressions and statements are used in C++ programs<BR>
	<BR>
	
	<LI>How to use the MFC class library to write Windows programs without using ClassWizard
</UL>

<P>You will also build sample programs that illustrate the topics you learn about
in this hour.
<H2><FONT COLOR="#000077"><B>Using Functions</B></FONT></H2>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>function</I> is a group
of computer instructions that performs a well-defined task inside a computer program.</P>
<P>Functions are one of the primary building blocks of C and C++ applications. Functions
provide a way to break up a large program into more manageable parts. At the same
time, functions make it possible to perform the same task at various points within
the program without repeating the code.</P>
<P>For example, If you buy a wagon, you'll find that it comes with a full set of
assembly instructions and has four identical wheels. Why should the instructions
repeat the steps to assemble a wheel four times? It is much easier to describe the
wheel assembly process once and indicate that you perform the process for each wheel.
The wheel assembly instructions are a module (function), within the full set of assembly
instructions (program), that is executed four times.</P>
<P>Every C++ program has at least one function; this function is called <TT>main</TT>.
The <TT>main</TT> function is called by the operating system when your application
starts; when <TT>main</TT> has finished executing, your program has finished.
<H3><FONT COLOR="#000077"><B>Declaring Function Prototypes</B></FONT></H3>
<P>Before you can use a function, you must declare it by supplying a function prototype
to the compiler. To declare a function, you specify the function's name, its return
value, and a list of any parameters that are passed to it, as shown here:</P>
<PRE><FONT COLOR="#0066FF"><TT>int CalculateAge(int nYearBorn);</TT>
</FONT></PRE>
<P>This line is a function prototype for the <TT>CalculateAge</TT> function, which
takes a single integer as a parameter and returns an integer as its result. A function
that returns no value is declared as returning the <TT>void</TT> type.</P>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>The traditional way to provide
function prototypes is to place them in <I>header</I> files, which are usually named
with an <TT>.h</TT> extension.</P>
<P>Header files that are part of the C++ standard library do not use the <TT>.h</TT>
extension; two examples of standard header files are <TT>iostream</TT> and <TT>math</TT>.
These header files contain all the prototypes and other declarations needed for IO
streams and math functions to be compiled correctly.
<H3><FONT COLOR="#000077"><B>Defining Functions</B></FONT></H3>
<P>A function is defined the same way the <TT>main</TT> function is defined. All
function definitions follow the same pattern; it's basically the function prototype
with the function's body added to it. The function definition always consists of
the following:

<UL>
	<LI>The function's return value<BR>
	<BR>
	
	<LI>The function's name<BR>
	<BR>
	
	<LI>The function's parameter list<BR>
	<BR>
	
	<LI>The actual function body, enclosed in curly braces
</UL>

<P>Listing 3.1 shows how to use a function to display the Hello World! message. To
run this project, create a new console-mode project named HelloFunc, using the steps
described for the Hello and Hello2 projects in the first two hours.
<H4><FONT COLOR="#000077">TYPE: Listing 3.1. The Hello World! program rewritten to
use a function.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>#include &lt;iostream&gt;</TT>
<TT>using namespace std;</TT>
<TT>// Function prototype</TT>
<TT>void DisplayAge(int nAge);</TT>

<TT>int main()</TT>
<TT>{</TT>
<TT>    DisplayAge(42);</TT>
<TT>    return 0;</TT>
<TT>}</TT>

<TT>void DisplayAge(int nAge)</TT>
<TT>{</TT>
<TT>    cout &lt;&lt; &quot;Hello World! I'm &quot; &lt;&lt; nAge &lt;&lt; &quot; years old.&quot;  endl;</TT>
<TT>}</TT>
</FONT></PRE>
<P>Because the function doesn't return a value to the calling function, the return
type is defined as <TT>void</TT>.
<H3><FONT COLOR="#000077"><B>Calling Functions</B></FONT></H3>
<P>In the C++ language, the act of transferring control to a function is known as
<I>calling</I> the function. When a function is called, you supply a function name
and a list of parameters, if any. The following steps take place when a function
is called:

<DL>
	<DD>1. The compiler makes a note of the location from which the function was called
	and makes a copy of the parameter list, if any.<BR>
	<BR>
	2. Any storage required for the function to execute is temporarily created.<BR>
	<BR>
	3. The called function starts executing, using copies of the data that was supplied
	in the parameter list.<BR>
	<BR>
	4. After the function has finished executing, control is returned to the calling
	function, and memory used by the function is released.
</DL>

<P>These steps are shown in Figure 3.1, which uses the function from Listing 3.1
as an example.</P>
<P><A NAME="01"></A><A HREF="01.htm"><B>Figure 3.1.</B></A><BR>
<I>Steps involved in calling a function.</I>


<BLOCKQUOTE>
	<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>The requirement
	that you declare functions before using them is an extension of the C++ type system.
	Because function prototypes are required, the compiler can detect errors such as
	incorrect parameters used in a function call. 
<HR>


</BLOCKQUOTE>

<H2><FONT COLOR="#000077"><B>What Are Structures?</B></FONT></H2>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>structure</I> is a data
type that is an aggregate; that is, it contains other data types, which are grouped
together into a single user-defined type.


<BLOCKQUOTE>
	<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Structures are
	commonly used when it makes sense to associate two or more data variables. 
<HR>


</BLOCKQUOTE>

<P>An example of a structure is a payroll record, where the number of hours worked
and the pay rate are combined in a structure, as shown in Figure 3.2.</P>
<P><A NAME="02"></A><A HREF="02.htm"><B>Figure 3.2.</B> </A><I><BR>
Structures are made up of member variables.</I></P>
<P>Declaring a structure introduces a new type of variable into your program. Variables
of this new type can be defined just like <TT>int</TT>, <TT>char,</TT> or <TT>float</TT>
variables are defined. Listing 3.2 is an example of how a structure is typically
used.
<H4><FONT COLOR="#000077">TYPE: Listing 3.2. Using a structure to calculate a weekly
salary.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>#include &lt;iostream.h&gt;</TT>

<TT>struct  TIME_REC</TT>
<TT>{</TT>
<TT>    double   dHours;</TT>
<TT>    double   dRate;</TT>
<TT>};</TT>

<TT>int main()</TT>
<TT>{</TT>
<TT>    TIME_REC    payrollRecord;</TT>

<TT>    payrollRecord.dHours = 40.0;</TT>
<TT>    payrollRecord.dRate = 3.75;</TT>

<TT>    cout &lt;&lt; &quot;This week's payroll information:&quot; &lt;&lt; endl;</TT>
<TT>    cout &lt;&lt; &quot;Hours worked : &quot; &lt;&lt; payrollRecord.dHours &lt;&lt; endl;</TT>
<TT>    cout &lt;&lt; &quot;Rate         :$&quot; &lt;&lt; payrollRecord.dRate  &lt;&lt; endl;</TT>

<TT>    double dSalary = payrollRecord.dRate * payrollRecord.dHours;</TT>
<TT>    cout &lt;&lt; &quot;Salary       :$&quot; &lt;&lt; dSalary  &lt;&lt; endl;</TT>

<TT>    return 0;</TT>
<TT>}</TT>
</FONT></PRE>
<H2><FONT COLOR="#000077"><B>What Are Classes?</B></FONT></H2>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>class</I> allows data
and functions to be bundled together and used as if they are a single element. Classes
typically model real-world concepts that have both data and some sort of behavior,
although this is not a hard and fast rule.</P>
<P>Classes are similar to structures; in fact, classes really are just structures
with a different name. Classes have one feature that makes them very useful for object-oriented
programming: Unless a member of a class is specifically declared as <TT>public</TT>,
that member is generally not accessible from outside the class. This means that you
can hide the implementation of methods behind the external interface.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -