- 每个程序必需有一个main函数,此为执行入口;返回值必需为int型,“0”表示执行成功。
- 函数组成部分:
1)返回类型
2)函数名
3)参数
4)函数体
例:#include int main() { return 0 } - C++源文件后缀与所运行的纺译器有关
1).cc
2).cxx
3).cpp
4).cp
5).c -
IO库:iostream
1)cin(读作 see-in),标准输入
2)cout(读作 see-out),标准输出
3)cerr(读作 see-err),标准错误
4)clog(读作 see-log),产生执行一般信息
例:#include >iostream< int main() { std::cout << "Enter two numbers:" << std::endl; int v1 , v2; std::cin >> v1 >> v2; std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl; return 0; }5)
#include <iostream>
第一行是一个预处理指示
尖括号中的名字叫头文件
std:命名空间
endl:操纵符,具有输出换行的效果,关刷新设备缓冲区
<<:输出操作符
>>:输入操作符 -
//:单行注释
/**/:多行注释
,注意不要嵌套。 -
if
while
for -
未知数目的输入
#include <iostream> int main() { int sum = 0 , value; while (std::cin >> value) sum += value; std::cout << "Sum is: " << sum << std::endl; return 0; } -
使用自定义类
#include <iostream> #include "Sales_item.h" int main() { Sales_item book; std::cin >> book; std::cout << book << std::endl; return 0; } -
include标准库用”<>”
include自定义类型用引号#include <iostream> #include "Sales_item.h"
-
调用成员函数
item1.same_isbn(item2)
-
一个简单完整的程序
#include <iostream> #include "Sales_item.h" int main() { // declare variables to hold running sum and data for the next record Sales_item total, trans; // is there data to process? if (std::cin >> total) { // if so, read the transaction records while (std::cin >> trans) if (total.same_isbn(trans)) // match: update the running total total = total + trans; else { // no match: print & assign to total std::cout << total << std::endl; total = trans; } // remember to print last record std::cout << total << std::endl; } else { // no input!, warn the user std::cout << "No data?!" << std::endl; return -1; // indicate failure } return 0; }
【C++ Primer】读书笔记 一
< 没有标签 >
回复 (0)