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

📄 helloworld.html

📁 关于 C++ 的历史无须我来介绍了
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

  
  <link rel="stylesheet" href="css/stdlayout.css" type="text/css">

  
  <link rel="stylesheet" href="css/print.css" type="text/css">

  
  <meta content="text/html; charset=gb2312" http-equiv="content-type">

  
  <title>Hello! World!</title>
</head>


<body>

<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>

<h1><a href="CppGossip.html">C++ Gossip: Hello! World!</a></h1>

<br>

学习程式语言的第一件事,就是得先学会如何在控制台(Console)上显示文字,也就是纯文字模式的显示,初学者往往必须先从控制台开始学习程式的撰写
与执行程式,这很枯燥,不若一些视窗化的开发环境撰写起来有成就感,但主控台程式设计可以让程式设计人员专心于程式逻辑的开发,因而对初学者来说也是学习
语言的一个好方式。 <br>

<br>

来看看如何使用C++来撰写一个简单的程式,将您指定的文字输出于主控台上,C++的副档名为*.cpp,您可以使用任一种纯文字编辑程式来编辑C++程
式:<br>

<br>

<pre>#include &lt;iostream&gt; <br>using namespace std; <br><br>int main() { <br>    cout &lt;&lt; "Hello! World!\n"; <br>    cout &lt;&lt; "哈啰! C++!\n";<br><br>    return 0; <br>}<iostream></iostream></pre>

<br>

首先看到程式的这两行:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">#include
&lt;iostream&gt; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">using
namespace
std;</span><br>

</div>

<br>

#include是巨集(Macro)前置处理器指令(Preprocessor
directive)(详情之后会介绍),它告诉编译器(Compiler)这个程式必须使用到 iostream
这个表头档(Header
file)中的一些函式(Function)与类别(Class)定义,以正确的编译程式中所使用到的程式库函式。简单的说您所要使用到的主控台输入输出
功能就定义在这个档案中;这是新式的程式的表头档写法,using指令表示使用名称空间(Namespace)std下的名称,这之后还会详细说明,现在
请先记得,要在主控台进行输入输出,这两行一定要写。 <br>

<br>

C++过去的旧式表头档写法在现在也是可以使用的,您也可以使用下面这行来取代上面两行: <br>

<div style="margin-left: 40px; font-weight: bold; font-family: Courier New,Courier,monospace;">#include
&lt;iostream.h&gt;</div>

<br>

这种表头档的include写法,在许多旧程式上还很常见。C++标准表头档没有副档名,因为副档名的命名规则随各编译器而有所不同。 <br>

<br>

如果您学过C语言,在C语言中常用的一些函式库,在C++中也可以使用,只要将*.h去掉,并于前头上.c即可,例如math.h改为cmath,
string.h改为cstring,当然这种C/C++函式库混用的情况是不被建议的。 <br>

<br>

接下来看主函式(Main function)的部份,在C++程式中,程式的进入点(Entry point)是main()这个函式: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int
main() { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp; ....
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp;
&nbsp;
return 0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>

</div>

<br>

其中int表示这个程式执行完毕之后会传回一个整数(Integer),括号的位置可设置参数列,之后会学到在程式执行的时候,可同时将程式的相关引数传
递给程式,目前我们并不需要传递引数,所以括号中不设定任何参数,表示程式不接收任何引数。 <br>

<br>

程式的最后一行是return 0,也就是程式结束后传回 0,在不成文的规定中,如果程式结束我们会传回一个数值,通常 0
表示程式正常结束,您可以让其它程式或shell来取得程式的传回值,以进行进一步的处理;一个简单的例子是,在程式结束后键入echo $?
看看程式传回的值是否为0,在标准C++中,如果没有指定return,main()也会传回0。 <br>

<br>

再来看main()中的两行陈述:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout
&lt;&lt; "Hello!
World!\n"; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout
&lt;&lt;
"Hello! C++!\n";</span><br>

</div>

<br>

在C++程式中,每一个陈述(Statement)结束都必须使用分号(;),cout(发音see-out)是C++所开启的输出串流(Stream)
物件(iostream object),而&lt;
&lt;是
输出运算子(Operator),串流与运算子的观念在之后才会介绍,简单的说,如果您要将文字显示于荧幕上,可以使用这样的写法: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout
&lt;&lt; "文字";</span><br>

</div>

<br>

<span class="postbody">如果您在却
显示文字的后面加上'\n',这是换行字元,表示文字输出之后显示换行字元,否则的话,下一段
文字会直接显示于同一行文字之后。 <br>

<br>

注意您要输出的字串是使用双引号"
"来包括,如果您要显示双引号,就会使得编译器在语法上混淆,所以您必须先告诉编译这件事,使用跳脱字元\",也就是说,您告诉编译器,在\之后的"是要
显示的字元,例如: <br>

</span>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout
&lt;&lt;
"我是\"良葛格\"!";</span><br>

</div>

<br>

这段文字会文字模式下显示:我是"良葛格"。<br>

<br>

接下来您可以使用您的编译器来编译程式了,您可以使用商业软体Visual C++,或是免费软体Dev
C++等程式来编译程式并输出执行档,执行之后,就可以看到主控台上出现这两段文字: <br>

<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">

  <tbody>

    <tr>

      <td style="background-color: rgb(0, 0, 0);"><small style="color: rgb(255, 255, 255);">Hello! World!<br>

哈啰! C++!</small></td>

    </tr>

  
  </tbody>
</table>

<br>

</body>
</html>

⌨️ 快捷键说明

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