C++primer plus---第二章

C是面向过程  C++是面向对象 

myfirst.cpp(win32控制台程序-->新建C++ source file--->写入代码)
#include<iostream>
using namespace std;
int main()
{
	cout<<"Come up and C++ me some time."<<endl;
	cout<<"You won't regret it!"<<endl;
	return 0;
}

如果想让窗口一直打开,在return 0之前添加cin.get();

C++primer plus---第二章                C++primer plus---第二章

预处理器编译指令 #include

(iostream中的io指的是输入和输出,使用cin和cout进行输入和输出的程序必须包含文件iostream)

函数头 int main()

编译指令 using namespace

(如果使用iostream,而不是iostream.h,则应使用using namespace std;指令来使iostream中的定义对程序可用)

函数体 用 {}括起来

注释以双斜杠(//  /*  */)两种形式
 

carrot.cpp

carrot.cpp(win32控制台程序-->新建C++ source file--->写入代码)

#include<iostream>
int main()
{
	using namespace std;
	int carrots;
	carrots = 25;
	cout<<"I have"<< carrots <<"carrots."<<endl;
	carrots = carrots-1;
	cout<<"Crunch,crunch.Now I have "<<carrots <<" carrots."<<endl;
	return 0;
}

 

C++primer plus---第二章

 

 

函数

函数名后括号中的部分叫做形参列表或参数列表:它描述的是从调用函数传递给被调用函数的信息。

C++primer plus---第二章

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++primer plus---第二章

ourfunc1.cpp

#include<iostream>
using namespace std;
void simon(int);
int main()
{
    simon(3);
    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;
}