二、c++的sentence
其程序是由不同的函数组成的,而每个函数都有自己的语句结构。声明变量及赋值
#include <iostream>int main(){using namespace std;int carrots;//声明整形变量definitioncarrots = 25;//给他赋值cout <<"I have ";cout << carrots;//声明变量在字符串中使用cout <<"carrots.";cout <<endl;//另起一行carrots = carrots - 1;//重新给变量赋值cout << "Crunch, crunch. Now I have"<<carrots <<"carrots." <<endl;return 0;}I have 25 carrots.Crunch, crunch. Now I have 24 carrots.“本来25个红萝卜,嚼b嚼b就剩24个了。”
定义声明 defining declaration(definition);引用声明 reference declaration 其两者区别在于引用是借用已经声明过的变量,定义是创建一个新的变量。定义就是为了告知编译器给变量分配适用的内存空间。
这个int carrots;的int 就是告知编译器变量carrots是整形,非小数点部分并且正负都可以的数据。
赋值语句 assign a value ,修改 modify the value
carrots = 25;= 是赋值操作符与插入操作符 << 一个类型。c与c++ 的赋值操作符也可以这么用
int a;int b;int c;
a = b = c = value;这个过程是自右向左的,value 赋给c值,然后是传递给了b,最后是a。代码中的carrots = carrots -1;就是 modify the value。但是这个modify使用了减法表达式 carrots-1 然而被modify后的carrots会发生什么?就是自现在开始新值24会代替原来的值25,储存在与对应内存空间中。
再说这个cout,程序中出现了同时向cout插入<< 字符串与变量的情况。“插入”听起来很形象。但插不同东西,打印出来的就不同。插一个带套子的“25”再插一个不带套子的25,编译器会把他们分别放入不同的储存空间的。“25”char型的2与5的ASC编码,而25却是个int型的二进制数值,一大堆1和0组成的东西。
cout打印出来:“嚼b嚼b就剩24个了”
cout << "Crunch, crunch. Now I have"<<carrots <<"carrots." <<endl;插入符<< 会把插入的东西整合,这就是典型的操作符重载。c++的cout 很智能是iostream 成员之一,而不像c中的printf需要提供转换说明。再举一个sentence的例子
#include <iostream>int main(){using namespace std;int carrots;cout<<"How many carrots do you have?"<<endl;cin>>carrots;//input输入cout<<"Here are two more.";carrots = carrots+2;cout<<"Now you have"<<carrots<<"carrots"<<endl;//concatenates outputreturn 0;}
How many carrots do you have?使用cin对象读取键盘输入数字:12Here are two more. Now you have 14 carrots.
使用cin。它跟cout一样也是iostream中的一个对象,操作符<<和>>是指信息流的方向,cin使用>>操作符提取输入数据。通常它的右边是一个变量 variable 。cin也是智能的,可以自动分辨 int carrots 变量的类型。就是输入的字符会被转换成变量可以接受的类型。类与oop Object-oriented programming
类是被定义的,可以描述他能够做什么,格式及用法。就像被声明的 类型和变量的关系int carrots。类定义描述的是数据格式用法,而对象则是根据这些内容创建实体。那么cout 和 cin 就是一个叫 iostream类的对象了。概念:类描述了一种数据类型及属性,对象是根据这些描述创建的实体。那么类是如何让对象工作的呢?有两种方式,操作符和函数的调用。cout<<"God bless u." 就是采用了操作符 << 。
函数
c++的函数分为两种 有返回值与没有返回值的。有返回值的会返回一个值赋予变量,这个表达式 X = sqrt(6.25)那么X就会被赋值2.5,因为函数sqrt()是一个能够返回平方根的函数return the value and assign it to variable,它会计算平方根。而表达式sqrt(6.25)就是调用函数sqrt() called function ->return value
sqrt() 函数的原型是:double sqrt(double_1) 这里面的double是sqrt() 函数返回值、是个带小数部分的实数。double_1 是参数,也是一个同类型的实数。那么例子里的6.25是参数,2.5是返回值。c++应当为每个使用的函数提供原型prototype #include cmath
double x;//declare x as a type double variablex=sqrt(6.25); 这里有分号结尾,表明这是条语句而不是函数头,如果省略分号,编译器会错误理解为函数头。两种方法提供原型prototype 1、是直接代码中写出来 double sqrt(double); 2、提前加上包含的头文件#include <cmath>,如同提前包含流文件一样 #include<iostream> 。当然是方法2 更好更准确。例如:
#include <iostream>#include<cmath>//添加函数头文件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"<<"feet to the side."<<endl;cout<<"How fascinating!"<<endl;return 0;
}Enter the floor area, in square feet, of your home: 536That's the equivalent of a square 39.1918 feet to the side.How fascinating!
多参、无返回值或无参数的函数
1.多参函数,的参数间用逗号分开。例如 pow()有两个参数,第一个为底,第二个是幂。他的prototype是 double pow(double,double)2.不接受任何参数函数例如 rend() 他的prototype是 int rend(void)/ int rend()关键字void指出该函数不接受任何参数,但可以随机返回一个 整形 的数值。c++中函数即使无参数也必须有对括号。3.无返回值的函数例如 自定义了一个函数 rmb()可按照货币数值及符号显示数字。当传递进参数时,将会显示出类似¥30.36这样的信息。那么他的prototype是 void rmb(double); 因为不返回任何值所以不能调用在赋值语句,或者其他表达式里。必须使用纯粹的函数调用语句例如 rmb(30.36)在其他语言中这种函数会被称作过程procedure,或子程序subroutine
自定义无返回值函数 在使用之前必须提供原型prototype,通常会放在mian()函数顶部。还有必须提供函数的源代码放在main()函数后面
#include <iostream>void simon(int);//function prototype for simon 提供原型int main(){using namespace std;simon(3);//call the function第一次赋值调用cout<<"Pice an integer:";int count;//声明一个变量countcin>> 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;// 调用后会发生什么}
Simon says touch your toes 3 times 第一次调用Pick an integer:250 cin输入Simon says touch your toes 250 times 第二次调用Done!
自定义函数格式:与main()函数格式相同,并且不能在一个函数定义嵌套中去定义另外一个函数。每个函数都是独立的,创建都是平等的。
type functionname(argumentlist){
statements
}
自定义有返回值函数:
#include <iostream>int stonetolb(int);//prototype for stonetolbint main(){using namespace std;int stone;//declared a variablecout<<"Enter the weight in stone:";cin>>stone;int pounds = stonetolb(stone);//declare a variable and call that function to assign it.cout<<stone<<"stone=";cout<<pounds<<"pounds."<<endl;return 0;}int stonetolb(int sts)//define the function{return 14*sts;// the function's procedure}Enter the weight in stone: xxxstone = xxxx pounds.
没有评论:
发表评论