21天学通C++

编写第一个C++程序

#include <iostream>

int main()
{
	std::cout << "Hello World" << std::endl;
	return 0;
}

这代码部分包含预处理编译指令#include
让预处理器获取想要得到的文件, iostream 是一个标准的头文件
程序的主题–,main()函数
cout 读作控制台输出see out 是将helloworld显示到屏幕上的语句
<< 流运算符插入运算符每次需要将插入运算符,std::endl 的作用相当于回车,每次需要将新的运算符插入到运算当中去
c++流的优点是可以将类似的语义用于另一种新类型的流的时候,将执行不同的操作。
返回值
在c++中,除非声明了不返回值,否则都具有一个返回值。
c++代码中的注释
//指出到哪都是注释 /* */ 表示的是文本之间的注释。

// Pre-processor directive
#include <iostream>

// Start of your program
int main()
{
   // Tell the compiler what namespace to look in
   using namespace std;

   /* Write to the screen using cout */
   cout << "Hello World" << endl;

   // Return a value to the OS
   return 0;
}

关键词using的另一种用法在程序清单2.3中使用第七八行替换了陈旭庆典中的第八航、

// Preprocessor directive
#include <iostream>

// Start of your program
int main()
{
   using std::cout;
   using std::endl;

   /* Write to the screen using cout */
   cout << "Hello World" << endl;

   // Return a value to the OS
   return 0;
}

前者能让使用这在不显示指定名称空间限定字符的情况下使用名称空间内的所有元素,而后者能够够吧让您在不显示指定名称空间限定字符的情况下使用std::cout 和std:;endl;

函数能够让程序划分成多个功能单元,并按照您的选择的顺序调用,函数被调用时通常将一个值返回给调用他的函数。

#include <iostream>
using namespace std;
// Function declaration
int DemoConsoleOutput();
int main()
{
   // Function call
	DemoConsoleOutput();
	return 0;
}
// Function definition
int DemoConsoleOutput()
{
	cout << "This is a simple string literal" << endl;
	cout << "Writing number five: " << 5 << endl;
	cout << "Performing division 10 / 5 = " << 10 / 5 << endl;
	cout << "Pi when approximated is 22 / 7 = " << 22 / 7 << endl;
	cout << "Pi actually is 22 / 7 = " << 22.0 / 7 << endl;
	return 0;
}

这里显示了cout函数的运算功能,它不仅能够显示文本,还可以简单显示算术运算的结果。
使用函数的返回值。
注意到函数必须有一个返回值,同样的main 函数也返回0, 一个更加明智的做法是将main的返回值定义为函数的返回值。

#include <iostream>
using namespace std;

// Function declaration and definition
int DemoConsoleOutput()
{
	cout << "This is a simple string literal" << endl;
	cout << "Writing number five: " << 5 << endl;
	cout << "Performing division 10 / 5 = " << 10 / 5 << endl;
	cout << "Pi when approximated is 22 / 7 = " << 22 / 7 << endl;
	cout << "Pi actually is 22 / 7 = " << 22.0 / 7 << endl;
	return 0;
}
int main()
{
   // Function call with return used to exit
	return DemoConsoleOutput();
}

使用std::cin 和sin::cout执行基本的输入输出操作。

将简单的文本写入到控制台,或者从控制台读取文本和数字可使用std::cin

因此 std::cin 后面是字符提取符号。>>从输入流中提取数据以及要将储存的数据存储到两个变量之中去。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值