C++ Primer Plus 第2章 开始学习C++

2.1 进入C++

程序清单2.1 myfirst.cpp

// my first.cpp -- display a message

#include<iostream>  							// a PREPROCESSOR directive
int main()  	 								// function header
{												//start of function body
	using namespace std;						//make definitions visible
	cout << "Come up and C++ me some time.";	//message
	cout << endl;								//start a new line
	cout << "You won't regret it!" << endl;		//more output
	return 0;									//terminate main()
 } 	

输出结果

Come up and C++ me some time.
You won't regret it!

2.1.1 main()函数
函数头(function heading)

int main()

该函数头表明main()函数可以给调用它的函数返回一个整数值,且不从调用它的函数那里获得任何信息

int main(void)    //very explicit style

在括号中使用关键字void明确地指出,函数不接受任何参数。
在C++中,让括号空着与在括号中使用void等效

void main()

这是等效的,因为void返回类型意味着函数不返回任何值。

2.1.2 C++注释

2.1.3 C++预处理器和iostream文件
如果程序要使用C++输入或输出工具,请提供这样两行代码:

#include<iostream>  	// a PREPROCESSOR directive
using namespace std;

#include编译指令导致预处理器将iostream文件的内容添加到程序中。

2.1.4 头文件名
像iostream这样的文件叫做包含文件(include file)——由于它们被包含在其他文件中;也叫头文件(header file)——由于它们被包含在文件起始处

2.1.5 名称空间
如果使用iostream,而不是iostream.h,则应该使用using空间编译指令来使iostream中的定义对程序可用:
可以省略编译指令using,以下述方式进行编码:

 std::cout << "Come up and C++ me some time.";
 std::cout << endl;	
using namespace std;

这个using编译指令使得std名称空间中的所有名称都可用。这是一种偷懒的做法,在大型项目中一个潜在的问题,更好的方法是,只使所需的名称可用,这可以通过使用using声明来实现:

using std::cout;	//make cout available
using std::endl;	//make endl available
using std::cin;		//make cin available

2.1.6 使用cout进行C++输出
1.控制符endl
2.换行符

cout << "What's next?\n";

2.1.7 C++源代码的格式化
在C++中,回车的作用就和空格或制表符相同。

// my first.cpp -- display a message

#include<iostream>  							// a PREPROCESSOR directive
using namespace std;
int
main
()  	 								// function header
{												//start of function body
	using
	namespace 
	std;						//make definitions visible
	cout << "Come up and C++ me some time.";	//message
	cout << endl;								//start a new line
	cout << "You won't regret it!" << endl;		//more output
	return 
	0;									//terminate main()
 } 												//end of function body

1.源代码中的标记和空白
一行代码中不可分割的元素叫做标记(token)。通常,必须用空格、制表符或回车将两个标记分开,空格、制表符和回车统称为空白(white space)。有些字符(如括号和逗号)是不需要用空白分开的标记。
2. C++源代码风格
每条语句占一行。
每个函数都有一个开始花括号和一个结束花括号,这两个花括号占一行。
函数中的语句都相对于花括号进行缩进。
与函数名称相关的圆括号周围没有空白。

2.2 C++语句

程序清单2.2 carrot.cpp

// carrots.cpp --food processing program
// uses and displays a variable

#include <iostream>

int main()
{
	using namespace std;
	
	int carrots;			// declare an interger varable
	carrots = 25;			// assign a value to the variable
	cout << "I have ";
	cout << carrots;
	cout << " carrots. ";
	cout << endl;
	carrots = carrots - 1;  // modify the variable
	cout << "Crunch, crunch. Now I have " << carrots << " carrots." << endl;
	return 0;
}

2.2.1 声明语句和变量

int carrots;	

这条语句提供了两项信息:需要的内存以及该内存单元的名称。

2.2.2 赋值语句

carrots = 25;

C++可以连续使用赋值运算符

int steinway;
int baldwin;
int yamaha;
yamaha = baldwin = steinway = 88;

赋值将从右向左进行

2.2.3 cout的新花样

cout << carrots;

cout可用于数字和字符串

2.3 其他C++语句

程序清单2.3 getinfo.cpp

//getinfo.cpp -- input and output
#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;
	return 0;
}

2.3.1 使用cin

cin >> carrots;	

2.3.2 使用cout进行拼接

cout << "Now you have " << carrots << " carrots. " << endl;
cout << "Now you have " ;
cout << carrots ;
cout << " carrots. ";
cout << endl;
cout << "Now you have " 
	 << carrots 
	 << " carrots. " 
	 << endl;

2.3.3 类简介

2.4 函数

2.4.1 使用有返回值的函数

double x;		// declare x as a type double variable
x = sqrt(6.25); // returns the value 2.5 and assigns it to x

程序清单 2.4 sqrt.cpp

// sqrt.cpp -- using the sqrt() function

#include <iostream>
#include <cmath>  			// or  math.h

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 fasinating!" << endl;
	return 0;
}

2.4.2 函数变体
有些函数需要多项信息

double pow(double, double);	// prototype of a funxtion with two arguments

另一些函数不接受任何参数

int rand(void);		// prototype of a function that takes no arguments

还有一些函数没有返回值

void bucks(double);		// prototype for function with no return value

2.4.3 用户定义的函数

// ourfunc.cpp -- defining your own function
#include<iostream>
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

main() 函数两次调用simon()函数,一次的参数为3,另一次的参数为变量count。

Simon says touch your toes 3 times.
Pick an integer: 2
Simon says touch your toes 2 times.
Done!

1.函数格式

type functionname(argumentlist)
{
	statement
}

C++ 不允许将函数定义嵌套在另一个函数定义中。每个函数的定义都是独立的。

2.函数头

void simon(int);

开头的void表明simon()没有返回值,因此调用simon()不会生成可在main()中将其赋给变量的数字。

simon(3);

由于simon()没有返回值,因此不能这样使用它:

sample = simon(3);

2.4.4 用户定义的有返回值的函数
程序清单2.6 convert.cpp

// convert.cpp -- converts stone to pounds
#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;
}
Enter the weight in stone: 15
15 stone = 210 pounds.
int stonetolb(int sts)
{
	int pounds = return 14 * sts;
	return pounds;
}

2.4.5 在多函数程序中使用using编译指令
将编译指令放在函数的外面,可以让两个函数都能访问名称空间

// ourfunc1.cpp -- defining your own function
#include<iostream>
using namespace std;
void simon(int);	// function prototype for simon()

int main()
{
	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
 {
 	cout << "Simon says touch your toes " << n << " times." << endl;
 }		
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值