第2章 开始学习C++
2.1进入C++
首先搭建编程环境,采用微软的VS2017作为开发工具,界面如下
然后新建一个项目开始学习,首先点击右下角的创建新项目,选择Windows控制台应用程序,点击确定就可以开始C++的学习之旅了。
效果如下
如果需要新建一个文件的话,可以在右侧的解决方案管理器中新建项,需要注意的是在一个项目中只能存在一个main函数,所以,要想运行新建文件中的程序的话,需要把其他源文件中的main函数改为其他函数(自己定义名称就行了),否则运行时会报错。
运行时点击工具栏中的本地Windows调试器即可,但会发生闪退的情况,这时我们只需在return 0前加上cin.get()即可,这样程序会处于等待按键的状态,不会自动关闭。或者按ctrl+F5不调试直接执行。
程序清单2.1 打印
//myfirst.cpp--displays a message
#include "stdafx.h"
#include <iostream> //a preprocessor directive预处理器编译指令
int main() //function header函数头
{
using namespace std; //make definitions visible 编译指令,使std名称空间中的所有名称都可用
cout << "come up and C++ me some time.";//message 输出内容
cout << endl; //start a new line 重启一行的控制符
cout << "you won'tregret it!" << endl; //more output
cin.get(); //wait a press 等待按键
return 0; //terminate main()返回语句 结束改函数
} //end of function body
2.2 C++语句
程序清单2.2 变量
//carrots.cpp--food processing program
//uses and displays a variable
#include "stdafx.h"
#include <iostream>
int main() //一个项目中只能出现一个main函数,所以在练习时保证练习过的程序函数名不是main
{
using namespace std;
int carrots; //declare an integer variabled 定义变量
carrots = 25; //assign a value to the variable 变量赋值
cout << "I have ";
cout << carrots; //display the value of the variable 显示变量的值
cout << " carrots.";
cout << endl;
carrots = carrots - 1;//modify the variable 修改变量
cout << "Crunch,crunch.Now I have " << carrots << " carrots." << endl;
cin.get();
return 0;
}
2.3 其他C++语句
程序清单2.3 输入
//getinfo.cpp--input and output
#include "stdafx.h"
#include <iostream>
int main()
{
using namespace std;
int carrots;
cout << "How many carrots do you have?" << endl;
cin >> carrots; //C++ input 输入 >>运算符代表从输入流抽取字符
cout << "Here are two more."; //<<运算符代表将字符插入到输出流
carrots = carrots + 2; //the next line concatenates output 与下一行的输出有关
cout << "Now you have " << carrots << "carrots." << endl;
cin.get();
return 0;
}
2.4 函数
程序清单2.4 引用函数
//sqrt.cpp -- using sqrt() function
#include "stdafx.h"
#include <iostream>
#include <cmath>
int main()
{
using namespace std;
double area;
double side;
cout << "Enter the floor area,in square feet,of your home: ";
cin >> area;
side = sqrt(area);
cout << "That's the equivalent of a square " << side << " feet to the side." << endl;
cout << "How fascinating!" << endl;
return 0;
}
程序清单2.5 定义函数
//ourfunc.cpp -- defining your own function
#include "stdafx.h"
#include <iostream>
//using namespace std; 可以将编译指令放在两个函数前面,使两个函数都能够访问名称空间,后面函数中就不用在写编译指令了
void simon(int); //function prototype for simon() 声明函数原型
int main()
{
using namespace std;
simon(3); //call the simon() function 引用函数
cout << "Pick an integer: ";
int count;
cin >> count;
simon(count); //call it again 再次引用函数
cout << "Done!" << endl;
return 0;
}
void simon(int n)//define the simon() function 定义函数头
{
using namespace std;
cout << "Simon says touch your toes "
<< n << " times." << endl;
} //void functions don't need return statements void 后的函数没有返回值
/*函数特性
1.有函数头和函数体
2.接收一个参数
3.返回一个值
4.需要一个原型
*/
程序清单2.6 有返回值的函数
//convert.cpp -- converts stone to pounds
#include "stdafx.h"
#include <iostream>
int stonetolb(int); //function prototype函数原型
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;//函数返回值
}
2.5 总结
C++程序由一个或多个被称为函数的模块组成。程序从main()函数(全部小写)开始执行,因此该函数必不可少。函数由函数头和函数体组成。函数头指出函数的返回值(如果有的话)的类型和函数期望通过参数传递给它的信息的类型。函数体由一系列位于花括号({})中的C++语句组成。
有多种类型的C++语句,包括下述6种。
•声明讲句:定义函数中使用的变量的名称和类型。
•赋值语句:使用赋值运算符(=)给变量赋值。
•消息语句:将消息发送给对象,激发某种行动。
•函数调用:执行函数。被调用的函数执行完毕后,程序返回到函数调用语句后面的语句。
•函数原型:明函数的返回类型、函数接受的参数数量和类型。
•返回语句:将一个值从被调用的函数那里返回到调用函数中。
类是用户定义的数据类型规范,它详细描述了如何表示信息以及可对数据执行的操作。对象是根据类规范创建的实体,就像简单变量是根据数据类型描述创建的实体一样。
C++提供了两个用于处理输入和输出的预定义对象(cin和cout),它们是istrcam和ostream类的实例, 这两个类是在iostream文件中定义的。为ostream类定义的插入运算符(<<)使得将数据插入到输出流成为可能;为istrcam类定义的抽取运算符(>>)能够从输入流中抽取信息。cin和cout都是智能对象,能够根据程序上下文自动将信息从一种形式转换为另一种形式。
C++可以使用大量的C库函数。要使用库函数,应当包含提供该函数原型的头文件。
课后习题答案
//practic.cpp--practice program
#include "stdafx.h"
#include <iostream>
int three();
int see();
double fah(int);
int astro(double);
void times(int,int);
int main()
{
//problem 1
/*using namespace std;
cout << "youkino" << endl;
cout << "japan" << endl;
return 0;*/
//problem 2
/*using namespace std;
int longl;
int ma;
cout << "please input how long: " << endl;
cin >> longl;
ma = longl * 220;
cout << longl << " long = ";
cout << ma << " ma " << endl;
return 0*/
//problem 3
/*using namespace std;
three();
three();
see();
see();
return 0;*/
//problem 4
/*using namespace std;
int age;
int month;
cout << "Enter your age: ";
cin >> age;
month = age * 12;
cout << month << " month" << endl;
return 0;*/
//problem 5
/*using namespace std;
int cels;
cout << "Please enter a celsius value: ";
cin >> cels;
int fahr = fah(cels);
cout << cels << " degrees Celsius is ";
cout << fahr << " degrees Fahrenheit" << endl;
return 0;*/
//problem 6
/*using namespace std;
double lighty;
cout << "Enter the number of light years: ";
cin >> lighty;
int astr = astro(lighty);
cout << lighty << " light years = ";
cout << astr << " astronomical units" << endl;
return 0;*/
//problem 7
/*using namespace std;
int ho;
int mi;
cout << "Enter the number of hours : ";
cin >> ho;
cout << "Enter the number of minutes : ";
cin >> mi;
times(ho, mi);
return 0;*/
}
int three()
{
using namespace std;
cout << "Three blind mice" << endl;
return 0;
}
int see()
{
using namespace std;
cout << "See how they run" << endl;
return 0;
}
double fah(int cel)
{
return 1.8 * cel + 32.0;
}
int astro(double light)
{
return 63240 * light;
}
void times(int h, int m)
{
using namespace std;
cout << "Time: " << h << ":" << m << endl;
}