// myfirst.cpp #include <iostream> int main() { using namespace std; cout << "Come up and C++ me some time."; cout << endl; cout << "You won't regret it!" << endl; return 0; }
C++是一门大小写敏感的语言。
C++程序必须包含一个名为main()的函数,作为程序的入口。
在main()函数没有return
语句时,默认在末尾加上return 0;
。
C++注释:
//
:行注释。/**/
:块注释。块注释不能嵌套块注释。#include <iostream>
编译指令会让预处理器将iostream文件的内容添加到程序中。
using namespace std;
使用名称空间std,可以简化代码。
myfirst.cpp
遵循的C++源代码风格:
// carrots.cpp #include <iostream> int main() { using namespace std; int carrots; carrots = 25; cout << "I have "; cout << carrots; cout << " carrots."; cout << endl; carrots = carrots - 1; cout << "Crunch, crunch. Now I have " << carrots << " carrots." << endl; return 0; }
声明语句int carrots;
包含两个信息:需要的内存以及该内存单元的名称。
C++数据类型的大小范围取决于实现。
对于声明变量,C++的做法是尽可能在首次使用变量前声明它。
// getinfo.cpp #include <iostream> int main() { using namespace std; int carrots; cout << "How many carrots do you have?" << endl; cin >> carrots; cout << "Here are two more. "; carrots = carrots + 2; cout << "Now you have " << carrots << " carrots" << endl; return 0; }
使用标准输入流获取控制台输入。
// sqrt.cpp #include <iostream> #include <cmath> int main() { using namespace std; double area; cout << "Enter the floor area, in square feet, of your home: "; cin >> area; double side; side = sqrt(area); cout << "That's the equivalent of a square " << side << " feet to the side." << endl; cout << "How fascinating!" << endl; return 0; }
C++程序应当为程序中使用的每个函数提供原型。
// ourfunc.cpp // 自定义函数 #include <iostream> void simon(int); int main() { using namespace std; simon(3); cout << "Pick an integer: "; int count; cin >> count; simon(count); cout << "Done!" << endl; return 0; } void simon(int n) { using namespace std; cout << "Simon says touch your toes " << n << " times." << endl; }
C++不允许将函数定义嵌套在另一个函数定义中。每个函数定义都是独立的,所有函数的创建都是平等的。
// convert.cpp // 有返回值的函数 #include <iostream> int stonetolb(int); int main() { using namespace std; int stone; cout << "Enter the weight in stone: "; cin >> stone; int pounds = stonetolb(stone); cout << stone << " stone = "; cout << pounds << " pounds." << endl; return 0; } int stonetolb(int sts) { return 14 * sts; }
// 编写一个C++程序,显示姓名和地址 #include <iostream> int main() { using namespace std; cout << "Name: hllog" << endl; cout << "Address: China" << endl; return 0; }
// 编写一个C++程序,它要求用户输入一个以long为单位的距离, // 然后将它转换为码。 1 long = 220码 #include <iostream> int longtoyard(int); int main() { using namespace std; cout << "Enter a distance(long): "; int distance; cin >> distance; int yard = longtoyard(distance); cout << distance << " long = " << yard << " yard." << endl; return 0; } int longtoyard(int num) { return 220 * num; }
// 编写一个C++程序,它使用3个用户定义的函数(包括main()), // 并生成下面的输出 // Three blind mice // Three blind mice // See how they run // See how they run #include <iostream> void show1(); void show2(); int main() { show1(); show1(); show2(); show2(); return 0; } void show1() { using namespace std; cout << "Three blind mice" << endl; } void show2() { using namespace std; cout << "See how they run" << endl; }
// 编写一个程序,让用户输入年龄,然后显示该年龄包含多少个月 #include <iostream> int monthOfAge(int); int main() { using namespace std; cout << "Enter your age(year): "; int age; cin >> age; int month = monthOfAge(age); cout << "Your age: " << month << " month" << endl; return 0; } int monthOfAge(int age) { return 12 * age; }
// 摄氏温度转华氏温度 // 华氏温度 = 1.8 * 摄氏温度 - 32.0 #include <iostream> double C2F(double); int main() { using namespace std; cout << "Enter a Celsius: "; double celsius; cin >> celsius; double fahrenheit = C2F(celsius); cout << celsius << " celsius = " << fahrenheit << " fahrenheit." << endl; return 0; } double C2F(double C) { return 1.8 * C - 32.0; }
// 光年转天文单位 // 1光年 = 63240天文单位 #include <iostream> double lightYear2AstronomicalUnit(double); int main() { using namespace std; cout << "Enter the number of light years: "; double lightYear; cin >> lightYear; double astronomicalUnit = lightYear2AstronomicalUnit(lightYear); cout << lightYear << " light years = " << astronomicalUnit << " astronomical units." << endl; return 0; } double lightYear2AstronomicalUnit(double lightYear) { return 63240 * lightYear; }
// 输入小时和分钟数,并格式化输出 #include <iostream> void fmtTime(int, int); int main() { using namespace std; int hours, minutes; cout << "Enter the number of hours: "; cin >> hours; cout << "Enter the number of minutes: "; cin >> minutes; fmtTime(hours, minutes); return 0; } void fmtTime(int hours, int minutes) { using namespace std; cout << "Time: " << hours << ":" << minutes << endl; }