每日学点c++:“Hello,World!“篇
1.0 基本语句
主要内容:变量赋值、cout使用,endl使用
#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.1 其他语句
主要内容:cin使用
#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;
cout << "Now you have " << carrots << "carrots." << endl;// 下一行输出结果
return 0;
}
输出结果:
1.2 类
- 类,是c++中面向对象编程(OOP)的核心概念之一。
- 类,是用户定义的一种数据类型。要定义类,需要描述它能够表示什么信息和可对数据执行哪些操作。类之于对象,就像类型之于变量。
- 类,包含对象;对象,不包含类。
- 类描述指定了可对类对象执行的所有操作。如:特定对象的输入、输出。
1.3 函数
-
分类:有返回值、无返回值
-
有返回值的函数:生成一个值,可赋给变量或在其他表达式中使用。
-
参数,发送给函数的信息;返回值,从函数中发送回去的值。
-
函数原型之于函数就像变量声明之于变量——指出涉及的类型。
例如:sqrt()的函数原型为double sqrt(double)。第一个double,定义了返回值的类型;第二个double,意味着sqrt()需要一个double参数。 -
原型函数的实现:
- 在源代码文件中输入函数原型;
- 包含头文件cmath,其中定义了原型。
例程:
# include <iostream>
# include <cmath> //include sqrt()
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 fanscinating!" << endl;
return 0;
}
输出结果:
1.4 自定义无返回值函数
- 对于库函数(库自带函数),在使用之前必须提供其原型,通常把原型放到main()之前。
- 对于自定义函数,最简便的方法,将代码放在main()的后面。
- c++,不允许将函数定义嵌套在另一个函数定义中。每个函数的定义都是独立的,所有函数的创建都是平等的。
# include <iostream>
void simon(int); // simon函数原型
int
main()
{
using namespace std;
simon(3); // 调用simon函数
cout << "Pick an integer: ";
int count;
cin >> count;
simon(count); // 再一次调用
cout << "DONE!" << endl;
return 0;
}
void simon(int n) // 定义simon函数
{
using namespace std;
cout << "Simon says touch your toes " << n << " times." << endl;
// void 函数不需要返回值,所以不能使用:simple = simon(3);
}
结果输出:
Simon says touch your toes 3 times.
Pick an integer: 521
Simon says touch your toes 521 times.
DONE!
请按任意键继续. . .
1.5 自定义有返回值函数
#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 ats)
{
return 14 * ats;
}
输出结果:
Enter the weight in stone: 55
55stone = 770 pounds.
请按任意键继续. . .
1.6 在多函数程序中使用using编译指令
为什么使用如下命令?
using namespace std;
因为函数中使用了cout,因此需要访问位于名称空间std中的cout定义。
如何让多个函数都可以访问名称空间std中的cout定义?
答:将编译指令放在函数的外面,且位于两个函数的前面。
访问名称空间std的几种方式:
- 将
using namespace std;
放在函数定义之前,让文件所有的函数都能使用名称空间std中的所有元素; - 将
using namespace std;
放在特定函数内部,让该函数都使用名称空间std中的所有元素; - 在特定函数中,使用类似
using std::std;
,而不是using namespace std;
,让该函数能够使用指定的元素,如cout
; - 完全不使用编译指令
using
,而在使用名称空间std中的元素时,需使用前缀std::,如下所示:
std::cout<<"I'm using cout and endl from the std namespace." << std::endl;
#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 <<" time."<<endl;
}
输出结果:
Simon says touch your toes 3 time.
Pick an integer: 55
Simon says touch your toes 55 time.
Done!
请按任意键继续. . .
1.7 编程练习
1.编写c++程序,显示姓名和地址;
#include <iostream>
using namespace std;
int main()
{
cout<<"My NAME IS : 李明"<<endl;
cout<<"My DIR IS : 成都 "<<endl;
return 0;
}
2.编写c++编程,他要求用户输入一个以long为单位的距离,然后将它转换成码;
#include <iostream>
using namespace std;
int main()
{
cout<<"long is ";
int lmz_long;
cin >>lmz_long;
int ma;
ma = 200*lmz_long;
cout<<lmz_long<<" long is "<<ma<<" 码。";
return 0;
}
3.编写一个C++程序,它使用3个用户定义函数(包括main()),并生成下面的输出:
Three blind mice.
Three blind mice.
See how the run.
See how the run.
其中一个函数要调用两次,该函数生成前两行,另一个函数也被调用两次,并生成其余的输出。
#include <iostream>
using namespace std;
void fun1();
void fun2();
int main()
{
fun1();
fun1();
fun2();
fun2();
return 0;
}
void fun1()
{
cout<<"Three blind mice."<<endl;
}
void fun2()
{
cout<<"See how the run."<<endl;
}
4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月。
#include <iostream>
using namespace std;
int fun1(int n);
int main()
{
cout<<"请输入您的年龄:";
int age;
cin>>age;
int month;
month = fun1(age-1);
cout<<"您已经走过 "<<month<<"月。";
return 0;
}
int fun1(int n)
{
return 12*n;
}
5.编写一个程序,main()调用一个用户定义的函数,以摄氏温度值为参数,返回相对应的华氏温度值。
转换公式:华氏温度 = 1.8x摄氏温度 + 32.0
#include <iostream>
using namespace std;
double fun1(int n);
int main()
{
cout<<"Plese enter a Celsius value:";
int value;
cin>>value;
double Fah;
Fah = fun1(value);
cout<<value<<" degrees Celsius is "<<Fah<<" degrees Fahrenheit.";
return 0;
}
double fun1(int n)
{
return 1.8*n + 32;
}
6.编写一个程序,main()调用一个用户定义的函数,(以光年值为参数,并返回对应天文单位的值)。
公式转换:1 光年 = 63240 天文单位
#include <iostream>
using namespace std;
double fun1(double n);
int main()
{
cout<<"Plese enter the number of light year:";
double value;
cin>>value;
double Fah;
Fah = fun1(value);
cout<<value<<"光年是"<<Fah<<"天文单位。";
return 0;
}
double fun1(double n)
{
return 63240*n;
}
7.编写一个程序,main()调用一个用户定义的void函数,将用户输入小时数和分钟数,传递给void函数。
#include <iostream>
using namespace std;
void fun1(int h,int m);
int main()
{
cout<<"Plese enter the number of hours:";
int h;
cin>>h;
cout<<"Plese enter the number of minutes:";
int m;
cin>>m;
fun1(h,m);
return 0;
}
void fun1(int h,int m)
{
cout<<"Time "<<h<<":"<<m;
}