C++ Primer Plus读书笔记第二章

自学了一段时间的C++打算还是要系统的整理一下一些知识点,让学习思路更清晰,不然老是学一点忘一点,这个读书笔记用来记录这段时间对C++ Primer Plus一书中知识点的记录,尽量会写的详细一点。直接从第二章开始,第一章不写。

//myfirst.cpp--displays a message
#include <iostream> //a preprocessor directive
using namespace std; //make definitions visible
//start of function body
int main(){

    cout<<"Comp up and C++ me some time!";//message

    cout<<endl;//start a new line

    cout<<"You won't regret it!"<<endl;//more output
    cout<<"请按下任意键结束进程"<<endl;
    cin.get();//按下任意键结束代码
    return 0;//terminate main()
}

一、创建一个完整的C++程序需要有:


1、注释,由前缀//标识。
2、预处理器编译指令#include。
3、函数头:int main()。
4、编译指令using namespace。
5、函数体,用{和}括起来。
6、使用C++的cout工具显示消息的语句。
7、结束main()函数的return语句。

二、语句和分号:
语句:语句是要执行的操作,在C++中每条完整的指令都称为语句,所有的语句都以分号结束。
分号:是语句的结束标记,是语句的组成部分,而不是语句之间的标记。

三、mian()函数:


函数头:对函数与程序其他部分之间的接口进行了总结;
函数体:是指出函数应做什么的计算机指令
1.做为接口的函数头: 

C++句法要求main()函数的定义以函数头int main()开始。

通常,C++函数可被其他函数激活或调用,函数头描述了函数与调用它的函数之间的接口。位于函数名前面的部分叫做函数返回类型,它描述的是从函数返回给调用它的函数的信息。函数名后括号中的部分叫做形参列表(argument list)或参数列表(parmeter list);它描述的是从调用函数传递给被调用的函数的信息。

 

从图中可以看出main()的接口描述,该接口从int开始,从关键字int可知,main()返回一个整数值。接下来是空括号,在这里空括号意味着mian()函数不接受任何信息,或者main()不接受任何参数。(main()函数不接受任何参数并不代表mian()是不讲道理的、发号施令的函数。相反,参数只是计算机人员用来表示从一个函数传递给另一个函数的信息)。

2.为什么main()不能使用其他名称:

通常,C++程序必须包含一个名为main()的函数。在运行C++程序时,通常从main函数开始执行。

四、注释:


C++注释以双斜杠(//)打头语。

五、C++预处理器和iostream文件:

如果程序要使用C++输入或输出工具,请提供这样两代码:

#include<iostream>
using namespace std;

像iostream这样的文件叫作包含文件(include file)——由于它们被包含在其他文件中;也叫头文件(header file)——由于它们被包含在文件起始处。C++编译器自带了很多头文件,每个头文件都支持一组特定的工具。

 

 六、名称空间

using namespace std;

为什么C++代码要添加上面那一句语句:它使得程序可以使用std名称空间中定义的名称,而不必使用std:前缀。

七、使用cout进行C++输出

在C++中,用双引号括起的一系列字符叫做字符串,因为它是由若干字符组合而成的。<<符号表示该语句将把这个字符串发送给cout。

八、控制符endl和换行符

 endl:表示重起一行。

\n:被视为一个字符,名为换行符。

书中显示用引号扩起的字符串时,通常使用换行符\n,在其他情况下则使用控制符endl。

 九、C++源代码的格式化

1.源代码中的标记和空白

 2.C++源代码风格

十、C++语句

1.声明语句和变量

对于声明变量,C++的做法是尽可能在首次使用变量前声明它

2. 赋值语句

3.cout的新花样

 

 十一、其他C++语句

1.使用cin

iostream文件将cin定义为一个表示这种流的对象。输出时,<<运算符将字符串插入到输出流中,输入时,cin使用>>运算符从输出流中抽取字符。

2.使用cout进行拼接

#include<iostream>
using namespace std;
int main ()
{
 
   int carrots=24;
//例子1:
   cout<<"Now you have"<<carrots<<"carrots."<<endl;
//例子2:
cout<<"Now you have";
    <<carrots;
    <<"carrots.";
    <<endl;
//例子3:
cout<<"Now you have"
    <<carrots
    <<"carrots."
    <<endl;
return 0;
}

3个例子中,最后输出结果是相同的。

十二、类简介

 类是用户定义的一种数据类型。要定义类,需要描述它能表示什么信息和可对数据执行那些操作。类定义描述的是数据格式及其用法,而对象则是根据数据形式规范创建的实体。类描述了一种数据类型的全部属性(包括可使用它执行的操作),对象是根据这些描述创建的实体。类描述指定了可对类对象执行的所有操作。

 

 十三、函数

C++函数分两种:有返回值的和没有返回值的。

有返回值的函数将生成一个值,而这个值可赋给变量或在其他表达式中使用。

简而言之,参数是发送给函数的信息,返回值是从函数中发送回去的值。

 

 十四、书中代码:

代码1:

#include <iostream>
//如果程序要使用C++输入或输出工具必须包含这两句#include<iostream>和using namespace std;
//include包含iostream文件
//iostream中:i表示istream输入,o表示ostream输出
using namespace std;
//using namespace std;这叫using指令
//{}里面是函数体
//int 返回的是一个整型
//main函数被启动代码调用,启动代码是由编译器添加到程序中的,是程序和操作系统之间的桥梁
//若int main()括号里面空的,说明main()函数不接受任何信息,或者main()不接受任何参数
//关键字void ,明确指出函数不接受任何参数
int main(void) //如果没有main函数程序将会不完整,C++每个程序必须有main函数
{
    cout<<"Come up and C++ me some time.\n";//cout输出流,把字符输出到屏幕上,<<插入运算符,\n换行字符
    cout<<endl;//控制符endl,endl也存在于iostream文件中
    cout<<"You won't regret it!"<<endl;
    return 0;//返回值0
}

代码2: 

#include <iostream>
//如果程序要使用C++输入或输出工具必须包含这两句#include<iostream>和using namespace std;
//include包含iostream文件
//iostream中:i表示istream输入,o表示ostream输出
using namespace std;
//using namespace std;这叫using指令
//{}里面是函数体
//int 返回的是一个整型
//main函数被启动代码调用,启动代码是由编译器添加到程序中的,是程序和操作系统之间的桥梁
//若int main()括号里面空的,说明main()函数不接受任何信息,或者main()不接受任何参数
//关键字void ,明确指出函数不接受任何参数
int main(void) //如果没有main函数程序将会不完整,C++每个程序必须有main函数
{
    int carrots;//定义一个整型变量carrots
    carrots = 25;//把整数25赋值给carrots变量,并存放在carrots存在的内存中
    cout<<"I have ";
    cout<<carrots;//这里carrots就是一个变量,打印的是存储在carrots这个变量里面存储的整数25
    // 25存储为二进制但是打印是字符2和5
    cout<<" carrtos.";
    cout<<endl;
    carrots =carrots-1;//carrots-1的结果赋值给carrots
    cout<<"Crunch,Crunch,Now I have "<<carrots<<"carrots."<<endl;

    return 0;//返回值0
}

代码3:

#include <iostream>
//如果程序要使用C++输入或输出工具必须包含这两句#include<iostream>和using namespace std;
//include包含iostream文件
//iostream中:i表示istream输入,o表示ostream输出
using namespace std;
//using namespace std;这叫using指令
//{}里面是函数体
//int 返回的是一个整型
//main函数被启动代码调用,启动代码是由编译器添加到程序中的,是程序和操作系统之间的桥梁
//若int main()括号里面空的,说明main()函数不接受任何信息,或者main()不接受任何参数
//关键字void ,明确指出函数不接受任何参数
int main(void) //如果没有main函数程序将会不完整,C++每个程序必须有main函数
{
    int carrots;//定义一个整型变量carrots
    cout<<"How many carrots do you have?"<<endl;
    cin>>carrots;//cin读取输入然后将输入的信息放入carots中,>>输入流
    cout<<"Here are two more.";
    carrots=carrots+2;
    cout<<"Now you have "<<carrots<<" carrtos."<<endl;

    return 0;//返回值0
}

代码4:

#include <iostream>
//如果程序要使用C++输入或输出工具必须包含这两句#include<iostream>和using namespace std;
//include包含iostream文件
//iostream中:i表示istream输入,o表示ostream输出
#include <cmath>调用math库
using namespace std;
//using namespace std;这叫using指令
//{}里面是函数体
//int 返回的是一个整型
//main函数被启动代码调用,启动代码是由编译器添加到程序中的,是程序和操作系统之间的桥梁
//若int main()括号里面空的,说明main()函数不接受任何信息,或者main()不接受任何参数
//关键字void ,明确指出函数不接受任何参数
int main(void) //如果没有main函数程序将会不完整,C++每个程序必须有main函数
{
    double area;
    cout<<"Enter the floor area,in square feet,of you home: ";
    cin>>area;
    double side;
    side= sqrt(area);
    cout<<"That's the equivalent of a square "<<side
        <<" feet to the side."<<endl;



    return 0;//返回值0
}

代码4:

#include <iostream>
//如果程序要使用C++输入或输出工具必须包含这两句#include<iostream>和using namespace std;
//include包含iostream文件
//iostream中:i表示istream输入,o表示ostream输出
using namespace std;
void simon(int n);
//using namespace std;这叫using指令
//{}里面是函数体
//int 返回的是一个整型
//main函数被启动代码调用,启动代码是由编译器添加到程序中的,是程序和操作系统之间的桥梁
//若int main()括号里面空的,说明main()函数不接受任何信息,或者main()不接受任何参数
//关键字void ,明确指出函数不接受任何参数
int main(void) //如果没有main函数程序将会不完整,C++每个程序必须有main函数
{
    simon(3);//3赋值给n
    cout<<"Pick an intger: ";
    int count;
    cin>>count;
    simon(count);
    cout<<"Done!"<<endl;
    return 0;//返回值0
}
void simon(int n)
{
    cout<<"Simon says tuoch you toes "<<n<<" time."<<endl;
}

代码5:

#include <iostream>
//如果程序要使用C++输入或输出工具必须包含这两句#include<iostream>和using namespace std;
//include包含iostream文件
//iostream中:i表示istream输入,o表示ostream输出
using namespace std;
int stonetolb(int sts);
//using namespace std;这叫using指令
//{}里面是函数体
//int 返回的是一个整型
//main函数被启动代码调用,启动代码是由编译器添加到程序中的,是程序和操作系统之间的桥梁
//若int main()括号里面空的,说明main()函数不接受任何信息,或者main()不接受任何参数
//关键字void ,明确指出函数不接受任何参数
int main(void) //如果没有main函数程序将会不完整,C++每个程序必须有main函数
{
    int stone;
    cout<<"Enter the weight stone: ";
    cin>>stone;
    int pounds = stonetolb(stone);
    cout<<stone<<" stone = ";
    cout<<pounds<<" pounds."<<endl;

}
int stonetolb(int sts)
{
    return 14 * sts;
}

十五、课后编程题

 

 

1.

#include <iostream>
using namespace std;
int main()
{
    cout<<"Rick"<<endl;
    cout<<"I live in China"<<endl;
    return 0;
}

2.

#include <iostream>
using namespace std;
int main()
{
    double distance;
    cout<<"Enter the distance(in long):";
    cin>>distance;
    cout<<"The distance "<<distance<<" long"<<" equals "<< 220*distance<<" yard"<<endl;
    return 0;
}

3.

#include <iostream>
using namespace std;
void print_mice();
void print_run();
int main()
{
    print_mice();
    print_mice();
    print_run();
    print_run();
    return 0;
}
void print_mice(void)
{
    cout<<"Three blid mice."<<endl;
}
void print_run(void)
{
    cout<<"See how they run."<<endl;
}

4.

#include <iostream>
using namespace std;

int main()
{
    int years;
    cout<<"Enter your age:";
    cin>>years;
    cout<<"you are "<<years<<" old,equals = "<<12*years<<"months old."<<endl;
    return 0;
}

5.

#include <iostream>
using namespace std;
double convert(double c);
int main()
{
    double c_degree,f_degree;
    cout<<"Please enter a Celsius value: ";
    cin>>c_degree;
   f_degree = convert(c_degree);
   cout<<c_degree<<" degrees Celsius is "<<f_degree<<"degress Fahrenheit"<<endl;
    return 0;
}
double convert(double c)
{
    return 1.8*c+32.0;
}

6.

#include <iostream>
using namespace std;
double converet(double);
int main()
{
    double light_years,astro_years;
    cout<<"Enter the number of light years: ";
    cin>>light_years;
    astro_years=converet(light_years);
    cout<<light_years<<" light years = "<<astro_years<<" astronomical units."<<endl;
    return 0;
}
double converet(double light)
{
    return light*63240;
}

7.

#include <iostream>
using namespace std;
void disply_time(int h,int m);
int main()
{
   int hours,minutes;
    cout<<"Enter the number of hours: ";
    cin>>hours;
    cout<<"Enter the number of minutes: ";
    cin>>minutes;
    disply_time(hours,minutes);
    return 0;
}
void disply_time(int h,int m)
{
   cout<<"Time: "<< h<<":"<<m<<endl;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lune_one

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值