一. 开始学习C++
1.1 进入C++
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;
}
1.1.1 main()函数
1.1.2 C++预处理器和iostream文件
如果程序要使用C++输入或输出工具,需要提供这样两行代码:
#include <iostream>
using namespace std;
1.1.3 头文件名
1.1.4 名称空间
使用编译指令using可以省略名称空间前缀std::,以下述方式进行编码:
std::cout << "Come up and C++ me some time.";
std::cout << std::endl;
1.1.5 使用cout进行C++输出
cout << "Come up and C++ me some time.";
<<该符号指出了信息流动的路径。
控制符endl
cout << endl;
endl表示重起一行。
换行符
cout << "Pluto is a dwarf planet.\n";
cout << "Pluto is a dwarf planet." << endl;
\n被视为一个字符,名为换行符。
1.1.6 C++源代码的格式化
源代码中的标记和空白
C++源代码风格
· 每条语句占一行。
· 每个函数都有一个开始花括号和一个结束花括号,这两个花括号各占一行。
· 函数中的语句都相对于花括号进行缩进。
· 与函数名称相关的圆括号周围没有空白。
1.2 C++语句
carrot.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;
}
1.2.1 声明语句和变量
int carrots; //声明一个整型变量
这条语句提供了两项信息:需要的内存以及该内存单元的名称。
1.2.2 赋值语句
carrots = 25; //为变量赋值
赋值语句将值赋给存储单元,=叫做赋值运算符。
1.3 其他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;
}
该程序包含两项新特性:用cin来读取键盘输入以及将四条输出语句组合成一条。
类简介
类描述了一种数据类型的全部属性(包括可使用它执行的操作),对象是根据这些描述创建的实体。
1.4 函数
1.4.1 使用有返回值的函数
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 eqivalent of a square " << side
<< " feet to the side." << endl;
cout << "How fascinating!" << endl;
return 0;
}
1.4.2 函数变体
有些函数需要多项信息。
double pow(double, double);
answer = pow(5.0, 8.0);
另外一些函数不接受任何参数。
int rand(void);
myGuess = rand();
还有一些函数没有返回值。
void bucks(double);
bucks(1234.56);
1.4.3 用户定义的函数
ourfnuc.cpp
#include <iostream>
using namespace std;
void simon(int);
int main()
{
simon(3); //调用simon
cout << "Pick an integer: ";
int count;
cin >> count;
simon(count);
cout << "Done!" << endl;
return 0;
}
void simon(int n)
{
cout << "Simon says touch your toes " << n << " times." << endl;
}
函数格式
type functionname(argumentlist)
{
statements
}
C++不允许将函数定义嵌套在另一个函数定义中,每个函数定义都是独立的,所有函数的创建都是平等的。
函数头
C++程序应当为程序中使用的每个函数提供原型。
1.4.4 用户定义的有返回值的函数
#include <iostream>
using namespace std;
int stonetolb(int);
int main()
{
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;
}
全部的函数特性:
·有函数头和函数体;
·接受一个参数;
·返回一个值;
·需要一个原型。
1.4.5 在多函数程序中使用using编译指令
只让需要访问名称空间std的函数访问它是更好的选择。访问名称空间的方法有多种,下面是其中的四种。
·将using namespace std;放在函数定义之前,让文件中所有的函数都能够使用名称空间std中所有的元素;
·将using namespace std;放在特定的函数定义中,让该函数能够使用名称空间std中的所有元素;
·在特定的函数中使用类似using std::cout;这样的编译指令,而不是using namespace std;让该函数能够使用指定的元素,如cout。
·完全不使用编译指令using,而在需要使用名称空间std中的元素时,使用前缀std::,即std:: cout << "I'm using cout and endl from the std namespace" << std::endl;
1.5 编程练习
1. 显示姓名地址。
#include <iostream>
int main()
{
using namespace std;
cout << "My name is Li Hua. " << "And I lvie in Beijing." << endl;
return 0;
}
2. 输入以long为单位的距离,将它转换为码(1long等于220码)。、
#include <iostream>
int lengthconvert(int);
int main()
{
using namespace std;
int length;
cout << "请输入距离(long):" ;
cin >> length;
int ma = lengthconvert(length);
cout << length << " long = " << ma << " 码" << endl;
return 0;
}
int lengthconvert(int len)
{
return 200 * len;
}
3. 使用3个用户定义的函数(包括main()),并生成下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run
#include <iostream>
using namespace std;
void MiceNum();
void MiceAct();
int main()
{
MiceNum();
MiceNum();
MiceAct();
MiceAct();
return 0;
}
void MiceNum()
{
cout << "Three blind mice" << endl;
}
void MiceAct()
{
cout << "See how they run" << endl;
}
4. 输入年龄,并显示该年龄包含多少个月,示例:
Enter your age: 29
29 years = 348 months
#include <iostream>
int AgetoMonths(int);
int main()
{
using namespace std;
int yourage;
cout << "Enter your age:";
cin >> yourage;
int months = AgetoMonths(yourage);
cout << yourage << " years = " << months << " months" << endl;
return 0;
}
int AgetoMonths(int age)
{
return 12 * age;
}
5. 输入摄氏温度,显示华氏温度,示例:
Please enter a Celsius value: 20
20 degrees Celsius is 68 degrees Fahrenheit.
#include <iostream>
double CeltoFah(double);
int main()
{
using namespace std;
double celdeg;
cout << "Please enter a Celsius value:";
cin >> celdeg;
double fahdeg = CeltoFah(celdeg);
cout << celdeg << " degrees Celsius is ";
cout << fahdeg << " degrees Fahrenheit." << endl;
return 0;
}
double CeltoFah(double Cel)
{
return 32 + Cel * 1.8;
}
6. 编写函数,以光年值为参数,并返回对应天文单位的值,示例:
Enter the number of light years: 4.2
4.2 linght years = 265608 astronomical units.
#include <iostream>
double astronom(double);
int main()
{
using namespace std;
double lightyears;
cout << "Enter the number of light years:";
cin >> lightyears;
double astrounits = astronom(lightyears);
cout << lightyears << " light years = ";
cout << astrounits << " astronomical units." << endl;
return 0;
}
double astronom(double ligyear)
{
return 63240 * ligyear;
}
7. 输入分钟数和小时数,显示时间,示例:
Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28
#include <iostream>
void Timer(int min, int hour);
int main()
{
using namespace std;
int minutes,hours;
cout << "Enter the number of hours: ";
cin >> hours;
cout << "Enter the number of minutes: ";
cin >> minutes;
Timer(minutes, hours);
return 0;
}
void Timer(int min, int hour)
{
using std::cout;
cout << "Time: " << hour << " : " << min ;
}