📄 apa.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</head>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//--> </SCRIPT><link rel="stylesheet" href="/includes/stylesheets/ebooks.css"> <META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1.1"> <TITLE>Teach Yourself Visual C++ 6 in 21 Days -- Appendix A -- C++ Review</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"BORDER="0"><BR>Teach Yourself Visual C++ 6 in 21 Days</H1><CENTER><P><A HREF="../ch21/ch21.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../apb/apb.htm"><IMGSRC="../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><H1 ALIGN="CENTER">- A -<BR>C++ Review</H1><H1></H1><UL> <LI><A HREF="#Heading1">Creating Your First Application</A> <UL> <LI><A HREF="#Heading2">Helloworld.cpp</A> <LI><A HREF="#Heading3">The if Statement, Operators, and Polymorphism</A> <LI><A HREF="#Heading4">Global and Local Variables</A> </UL> <LI><A HREF="#Heading5">Pointers</A> <UL> <LI><A HREF="#Heading6">References</A> </UL> <LI><A HREF="#Heading7">Classes</A> <UL> <LI><A HREF="#Heading8">Constructors and Destructors</A> </UL> <LI><A HREF="#Heading9">Inheritance</A> <LI><A HREF="#Heading10">Summary</A></UL><P><HR SIZE="4"><BR>The appendix is designed to provide you with a quick review of the fundamentals ofthe C++ programming language. After reading this appendix, you will have a thoroughunderstanding of the various aspects of C++ and its syntax.</P><P><H2><A NAME="Heading1"></A>Creating Your First Application</H2><P>Your first example is a simple program that displays "Hello World" onthe screen. For this, you create a workspace and the C++ file required for the program.The procedure for writing a C++ program using Visual C++ is simple and easy. Followthese steps:</P><DL> <DT></DT> <DD><B>1. </B>From the main menu, select Visual C++. <P> <DT></DT> <DD><B>2. </B>Select File | New from the Visual C++ toolbar. <P> <DT></DT> <DD>Make sure the Projects tab is selected (see Figure A.1). <P> <DT></DT> <DD><B>3. </B>Select Win32 Console Application from the options on the left. <P></DL><P><A HREF="javascript:popUp('0afig01.gif')"><B>FIGURE A.1.</B></A><B> </B><I>Settingup the Hello workspace.</I></P><DL> <DT><I></I></DT> <DD><B>4. </B>Type <B>Hello</B> on the right side under Project Name. <P> <DT></DT> <DD><B>5. </B>Select OK. <P></DL><P>Visual C++ creates the workspace of your application. Visual C++ actually createsa directory Hello, which enables you to store all files related to a particular projectin one area. You will begin adding the files you require for this project:</P><DL> <DT></DT> <DD><B>1. </B>Once again, select File | New from the toolbar. <P> <DT></DT> <DD><B>2. </B>Select the Files tab if it is not already selected. <P> <DT></DT> <DD><B>3. </B>Highlight C++ Source File. <P> <DT></DT> <DD><B>4. </B>Check the Add to Project box on the right side. <P> <DT></DT> <DD><B>5. </B>In the File Name edit box, type <B>Helloworld</B> (see Figure A.2). <P> <DT></DT> <DD><B>6. </B>Click OK. <P></DL><P><A HREF="javascript:popUp('0afig02.gif')"><B>FIGURE A.2.</B></A><B> </B><I>Settingup the Helloworld project.</I></P><P>The Helloworld.cpp file is where you add the C++ source code. All C++ source codefiles have a .cpp extension. Later, I will cover other file types.</P><P>You create all the tutorial examples in this section in a similar way. The onlydifference is that the names of the workspaces and the files are different.</P><P><H3><A NAME="Heading2"></A>Helloworld.cpp</H3><P>The Helloworld program displays HELLO WORLD on the screen. Listing A.1 containsthe code. Type the code exactly as shown in the Helloworld.cpp window. Do not typethe line numbers; they are for reference only. C++ is case sensitive, so main isnot the same as MAIN, which is not the same as Main.</P><P><H4>LISTING A.1. Helloworld.cpp.</H4><PRE> 1: // Workspace Name: Hello 2: // Program Name: Helloworld.cpp 3: 4: # include <iostream.h> 5: 6: int main() 7: 8: { 9: cout<< "HELLO WORLD \n";10: return 0;11: }</PRE><P>To run the program, follow these steps:</P><DL> <DT></DT> <DD><B>1. </B>Select File | Save to save your work. <P> <DT></DT> <DD><B>2. </B>Select Build | Set Active Configuration<I> </I>(see Figure A.3). <P> <DT></DT> <DD><B>3. </B>Highlight Hello - Win32 Debug and click OK (see Figure A.4). <P> <DT></DT> <DD><B>4. </B>Select Build | Build Hello.exe. <P></DL><P>Visual C++ compiles and links the program to create an executable file. The configurationwindow indicates the success or failure of the compilation. A successful compilationreturns</P><P><PRE>Hello.exe - 0 error(s), 0 warning(s)</PRE><P>If you encounter any errors, verify that all the lines of the program were typedexactly as shown.</P><P>To execute the Helloworld program, select Build | Execute Hello.exe.</P><P><A HREF="javascript:popUp('0afig03.gif')"><B>FIGURE A.3.</B></A><B> </B><I>Settingthe active configuration.</I></P><P><A HREF="javascript:popUp('0afig04.gif')"><B>FIGURE A.4.</B></A><B> </B><I>SelectingWin32 Debug.</I></P><P>The program executes by opening an MS-DOS shell and displaying the text HELLOWORLD (see Figure A.5).</P><P><A HREF="javascript:popUp('0afig05.gif')"><B>FIGURE A.5.</B></A><B> </B><I>HELLOWORLD display.</I></P><P><H4>Components of Helloworld.cpp</H4><P>The first two lines of the program are comment lines:</P><P><PRE>// Workspace Name: Hello// Program Name: Helloworld.cpp</PRE><P>The double slash command (//) tells the compiler to ignore everything after theslash. It is good programming practice to comment your work because it makes theprogram easier to read, especially for someone who did not write it. Comments becomeimportant when you are working on a complex program for months. When you want tomake changes, comments assist you in recollecting your thoughts from more than amonth ago.</P><P>The third line begins with the pound symbol (#):</P><P><PRE># include <iostream.h></PRE><P>This is a directive to the preprocessor to search for the filename that follows(iostream.h) and include it. The angled brackets (< >) cause the preprocessorto search for the file in the default directories. The iostream.h file contains definitionsfor the insertion (<<) and extraction (>>) operators. This directiveis required to process the cout statement defined on line 9 in the program. The iostream.hfile is a precompiled header provided with your compiler. You may experiment withthe Helloworld program by commenting out the include line. To do this, insert thebackslash (//) before the pound sign (#). When you compile and execute this program,you get an error:</P><P><PRE>Compiling...Helloworld.cppC:\cplusplus\Hello\Helloworld.cpp(9) : error C2065: Â `cout' : undeclared identifierC:\cplusplus\Hello\Helloworld.cpp(9) : error C2297: `<<' : bad right ÂoperandError executing cl.exe.Hello.exe - 2 error(s), 0 warning(s)</PRE><P>Without the iostream.h file, the program does not recognize the cout command orthe insertion operator (<<).</P><P>The next line of code, line 6, is actually where program execution begins. Thisis the entry point of your code:</P><P><PRE>int main()</PRE><P>This line tells the compiler to process a function named main. Every C++ programis a collection of functions. You will cover functions in greater detail later inthis appendix. For now, you define a function as the entry point for a block of codewith a given name. The empty parentheses indicate that the function does not passany parameters. Passing parameters by functions is described in the section "Functionsand Variables," later in this chapter.</P><P>Every C++ program must have the function main()<I>.</I> It is the entry pointto begin program execution. If a function returns a value, its name must be precededby the type of value it will return; in this case, main() returns a value of typeint.</P><P>The block of code defined by any function should be enclosed in curly brackets({ }):</P><P><PRE>{cout<< "HELLO WORLD \n";return 0;}</PRE><P>All code within these brackets belongs to the named function--in this case, main().</P><P>The next line executes the cout object. It is followed by the redirection operator<<, which passes the information to be displayed. The text to be displayedis enclosed in quotes. This is followed by the newline operator (\n). The redirectionor insertion operator (<<) tells the code that whatever follows is to be insertedto cout.</P><BLOCKQUOTE> <P><HR><STRONG>NOTE:</STRONG> Line 9 ends with a semicolon. All statements in C++ must end with a semicolon. <HR></BLOCKQUOTE><P>Line 10 of the code has a return statement. Programmers often use return statementseither to return certain values or to return errors. Also remember that on line 7when you defined the main() function, you defined its return type to be an integer(int). You may rerun this code by deleting the return statement on line 10, in whichcase line 7 would have to be modified as follows:</P><P><PRE>void main()</PRE><P>It is good programming practice to include return codes for complex programs.They will help you identify and track down bugs in your program.</P><P><B>Functions and Variables</B></P><P>The Helloworld program consists of only one function, main(). A functional C++program typically consists of more than a single function. To use a function, youmust first declare it. A function declaration is also called its prototype. A prototypeis a concise representation of the entire function. When you prototype a function,you are actually writing a statement, and as mentioned before, all statements inC++ should end with semicolons. A function prototype consists of a return type, name,and parameter list. The return type in the main() function is int, the name is main,and the parameter list is (), null.</P><P>A function must have a prototype and a definition. The prototype and the definitionof a function must agree in terms of return type, name, and parameter list. The onlydifference is that the prototype is a statement and must end with a semicolon. ListingA.2 illustrates this point with a simple program to calculate the area of a triangle.</P><P><H4>LISTING A.2. Area.cpp.</H4><PRE> 1: // Workspace: Triangle</PRE><PRE> 2: // Program name: Area.cpp</PRE><PRE> 3: // The area of a triangle is half its base times height 4: // Area of triangle = (Base length of triangle * Height of triangle)/2 5: 6: #include <iostream.h> // Precompiled header 7: 8: double base,height,area; // Declaring the variables 9: double Area(double,double); // Function Prototype/declaration10: 11: int main()12: {13: cout << "Enter Height of Triangle: "; // Enter a number14: cin >> height; // Store the input in variable15: cout << "Enter Base of Triangle: "; // Enter a number16: cin >> base; // Store the input in variable17: 18: area = Area(base,height); // Store the result from the Area Âfunction19: // in the variable area20: cout << "The Area of the Triangle is: "<< area << endl ; // Output the Âarea21: 22: return 0;23: }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -