1.杂
- 计算机语言要处理概念——数据和算法。数据是程序使用和处理的信息,而算法是程序使用的方法。
- 过程性编程是试图使问题满足语言的过程性方法,强调的是算法方面,而OOP试图让语言来满足问题的需求,其理念是设计与问题的本质特性相对应的数据格式。
- 在C++中,类是一种规范,它描述了新型的数据格式,对象是根据这种规范构造的特定数据结构。
- 可移植相关:硬件和语言上的差异。
2.一个示例程序
1 //displays a mesage
2 #include<iostream>
3 int main()
4 {
5 using namespace std;
6 cout<<"Come up and C++ me some time.";
7 cout<<endl;
8 cout<<"You won't regret it!"<<endl;
9 return 0;
10 }
3.进入C++
(1)int main(void) 描述的是main()和操作系统之间的接口。关键字void指出函数不接受任何参数,在C++中让括号空着与使用void完全等效,在C中,让括号空着意味着对是否接收参数表示沉默。
(2)iostream中的io指的是输入(cin)和输出(cout).
(3)关于using namespace std;//命名空间。类、函数和变量是C++编译器的标准组件,他们都被放在名称空间std中。
(4)cout将一个字符串插入到输出流中。endl;//表示重起一行。
(5)C++风格:
- 每行一条语句。
- 每个函数都有一个开始花括号和一个结束花括号,这两个花括号各占一行。
- 函数中的语句都相对于花括号进行缩进。
- 与函数名称相关的圆括号周围没有空白。
4.C++语句
1 //提供两种语句,声明语句创建变量、赋值语句给变量赋值
2 //重新认识cout的智能
3 #include<iostream>
4 int main()
5 {
6 using namespace std;
7
8 int carrots;
9 carrots=25;
10
11 cout<<"I have";
12 cout<<carrots;//打印carrots的当前值25.
13 cout<<"carrots.";
14 cout<<endl;
15 carrots=carrots-1;
16 cout<<"Crunch,crunch,Now I have "<<carrots<<" corrots."<<endl;
17 return 0;
18 }
(1)在C++中,所有变量都必须声明,定义声明导致编译器为变量分配内存空间,相应的还有引用声明。
(2)cout和cin的智能化,请注意cout<<"n";和cout<<n;的区别。cin使用>>操作符从收入流中抽取字符。cout<<"n"<<"b";(cout可以使用<<进行拼接).
(3)类描述了一种数据类型的全部属性,对象是根据这些描述创建的实体。
1 //引入cin
2 #include<iostream>
3 int main()
4 {
5 using namespace std;
6 int carrots;
7 cout<<"How many carrots do you have ?";
8 cin>>carrots; //c++input
9 cout<<"Here are two more.";
10 carrots=carrots+2;
11 cout<<"Now you have"<<carrots<<"carrots."<<endl;//使用<<作为连接连续输出
12 return 0;
13 }
5.函数简介
1 //使用sqrt()函数
2 #include<iostream>
3 #include<cmath>//提供sqrt()函数原型
4 int main()
5 {
6 using namespace std;
7 double area;
8 cout<<"Enter the floor area,in square feet,of your home:";
9 cin>>area;
10 double side;//允许在任何地方声明新变量
11 side=sqrt(area);
12 cout<<"That's the equivalent of a square 39.1918"
13 <<"feet to side."<<endl;
14 cout<<"How fascinating!"<<endl;
15 cout<<side<<endl;//输出结果39.1918
16 //cout<<side<<endl;??
17 return 0;
18 }
(1)有返回值的函数(使用return关键字来提供返回值)和没有返回值的函数。不返回值的函数,不能将该函数的调用放在赋值语句或其他表达式中。
(2)对于有返回值的函数,可以认为函数执行完毕后,语句中的函数调用部分将被替换为返回值。
(3)对于C++库中的每个函数,都在一个或多个头文件中提供了其原型。原型只描述函数接口,库文件中包含了函数的编译代码,而头文件则包含函数原型。
(4)C+=允许在程序的任何地方声明新的变量。
(5)C++不允许将函数定义嵌套在另一个函数定义中。每个函数都是独立的,所有函数的创建都是平等的。
(6)main()函数的返回值并不是返回给程序的其它部分,而是被返回给操作系统。
(7)函数原型描述了函数接口(函数如何与程序的其它部分交互),参数列表指出了何种信息将被传递给参数,函数类型指出了返回值的类型。
1 //调用函数
2 #include<iostream>
3 void simon(int);//声明函数
4
5 int main()
6 {
7 using namespace std;
8 simon(3);//调用函数
9 cout<<"Pick an integer:";
10 int count;
11 cin>>count;
12 simon(count);
13 cout<<"Done!"<<endl;
14 return 0;
15 }
16
17 void simon(int n)//函数定义部分
18 {
19 using namespace std;
20 cout<<"Simon says touch your toes "<< n <<" times."<<endl;
21 }//函数不需要返回语句
(8)一般地,只让需要访问名称空间的函数访问它是更好的选择。
1 //用户定义的有返回值的函数
2 #include<iostream>
3 int stonetolb(int);//函数声明
4
5 int main()
6 {
7 using namespace std;
8 int stone;
9 cout<<"Enter the weight in stone:";
10 cin>>stone;
11 int pounds=stonetolb(stone);//把stone作为函数的参数
12 cout<<stone<<" stone =";
13 cout<<pounds<<" pounds."<<endl;
14 return 0;
15 }
16
17 int stonetolb(int n)
18 {
19 return 14*n;//函数的返回值
20 }