一、C++基础知识
1:最基本的函数
包括函数头+函数名+函数体
如果编译器到main()函数的末尾没有遇到返回语句,则认为main函数以如下语句结束:
return 0;
int main(void) //int 是返回值类型
{
return 0; //返回语句
}
注意:通常c++程序从main函数开始执行,如果没有main函数,程序将不完整,编译器会报错
但是在Windows编程中,可以编写动态链接库(Dll)模块,它不是一个独立的程序,因此不需要Main函数,又比如机器人中的控制器芯片,也可能不需要main函数,但是常规的独立程序都需要main()
2:注释
//双斜杠注释
/*
注释几段
*/
3:c++预处理器和iostream文件
如果程序要使用C++输入或输出工具,需提供以下代码
#include<iostream>
using namespace std;
可以使用其他代码替换第二行,这里使用这行代码旨在简化该程序
而#include<iostream>
该编译指令导致预处理器将iostream文件的内容添加到程序中,这是一种典型的预处理器操作,在源代码被编译之前,替换或添加文本
iostream中的io指的是输入和输出,C++的输入/输出方案涉及iostream文件中的多个定义。为了使用cout来显示消息,第一个程序需要这些定义。#include编译指令导致iostream文件中的内容随源代码文件的内容一起被发送给编译器
注意:使用cin和cout输入输出的程序必须要使用文件iostream
4:头文件命名约定
头文件类型 | 约定 | 示例 | 说明 |
C++旧式风格 | 以.h结尾 | iostream.h | C++程序可以使用 |
C旧式风格 | 以.h结尾 | math.h | C,C++程序可以使用 |
C++新式风格 | 没有扩展名 | iostream | C++程序可以使用,使用namespace std |
转换后的C | 加上前缀c,没有扩展名 | cmath | C++程序可以使用,可以使用不是c的特性 |
5:命名空间
using namespace std;
这叫做using编译指令
命名空间支持是一项C++特征,旨在让我们编写大型程序以及将多个厂商现有的代码组合起来的程序时更容易,还有助于组织程序
命名空间能让厂商将它的产品封装在一个名叫命名空间的单元内,这样就可以用命名空间的名称来指出想使用哪个厂商的产品。比如MIcroflop可以将其函数定义在命名空间为Microflop中,函数可以写成Microflop::wanda(),Piscine版本就写为Piscine::wanda(),这样程序就可以使用命名空间来区分
其实写成using namespace std;是一种较为粗暴的做法,意味着后面所用到的所有指令操作都是来自该命名空间,实际上更好的方法是,只使所需的名称即可,比如:
using std::cout;//make cout avaliable
using std::endl;
using std::cin;
6:使用cout进行C++输出
cout<<"Come up and C++ me some time.";
<<符号表示该语句将把这个字符串发送给cout,它指出了该信息流动的路径
从概念上看,输出是一个流,即从程序流出的一系列字符,cout对象表示这种流,其属性是在iostream文件中定义的。cout对象属性包括一个插入运算符(<<),它可以将其右侧的信息插入到流中。
1)控制符endl
cout<< endl;它是一个特殊的C++符号,表示重起一行,在输入流中插入endl将导致屏幕光标移动到下一行的开头。诸如endl等对于cout等来说有特殊含义的符号称为控制符(mainpulator)和cout一样,endl也是在文件iostream中定义的,且位于命名空间std中
换行操作同样我们也可以使用换行符\n
cout<<"Pluto is a dwarf planet.\n"
另外如果另起空行,则两种方法的输入量相同,对大多数人而言,endl更为方便
cout<<"\n";
cout<<endl;
2)C++源代码风格
·每条语句占一行
·每个函数都有一个开始花括号和结束花括号,这两个花括号各占一行
·函数中的语句都相对于花括号进行缩放
·与函数名称相关的圆括号周围没有空白
二、C++语句
1:声明语句和赋值语句
#include <iostream>
int main(void)
{
using namespace std;
int carrots;
carrots= 25;
cout << "i have";
cout<< carrots;
cout << endl;
return 0;
}
修改数值
#include <iostream>
int main(void)
{
using namespace std;
int carrots;
carrots= 25;
cout << "i have ";
cout<< carrots;
cout<< " carrots.";
carrots = carrots - 1;
cout<< " Crunch,Now I have "<<carrots<<" carrots.";
cout << endl;
return 0;
}
注意:空格和符号的使用
2:其他语句
#include <iostream>
int main(void)
{
using namespace std;
int carrots;
cout << "How many carrots do you have?" << endl;
cin >> carrots; //键盘输出
cout << "Here are two more.";
carrots = carrots + 2;
cout << "Now you have " << carrots << " carrots." << endl;
return 0;
}
*cin表示从键盘输入
3:使用cout进行拼接
比如刚才的语句
cout << "Now you have " << carrots << " carrots." << endl;
但是如果拼接的语句过长,也可以采纳这种格式
cout << "Now you have "
<< carrots
<< " carrots."
<< endl;
4:类的简介
类是C++中面向对象编程(OOP)的核心概念之一,它是一种数据类型。要定义类,需要描述它能够表示什么信息和可对数据执行哪些操作
比如对于cout来说,它是一个ostream类的对象,描述了ostream对象表示的数据及可对它执行的操作,而cin是一个istream类的对象,也是定义在iostream中的
*类描述了一种数据类型的全部属性,对象是根据这些描述创建的实体。
三、函数
函数有两种,有返回值和没有返回值的
1:有返回值的函数
有返回值的函数将生成一个值,而这个值可赋给变量或在其他表达式中使用
例如C++库中包含sqrt()函数,可以使用该语句返回平方根
x = sqrt(6.25);
上式调用了sqrt()函数,表达式被称为函数调用,被调用的函数叫做被调用函数,sqrt()得到的结果为2.5,并将这个值发送给调用函数,发送回去的值叫做函数的返回值(return value)。
即,函数执行完毕后,语句中的函数调用部分被替换为返回值
*C++程序应当为程序中每个函数提供原型
比如sqrt()函数的原型如下:
double sqrt(double);
第一个double意味着sqrt()将返回一个double值。括号中的double意味着sqrt()需要一个double参数
原型结尾的分号表示它是一条语句,这使得它是一个原型,而不是函数头,如果省略分号,编译器将这行代码解释为函数头,并接着要求提供函数体!
#include <iostream>
#include <cmath>
int main(void)
{
using namespace std;
double area;
cout << "Enter the floor area, in square feet, of ur home: ";
cin >> area;
double side;
side = sqrt(area);
cout << "That is the equivalent of a squart " << side << " feet to the side." << endl;
return 0;
}
2:函数变体
有些函数需要多项信息,这些函数使用多个参数,它们之间用逗号分开。例如,数学函数pow()接受两个参数,返回值为以第一个参数为底,第二个参数为指的幂,原型如下:
double pow(double, double );
比如计算5的8次方,可以这样写:
x= pow (5.0, 8.0);
又比如int rand(void);
关键词void指出,该函数不接受任何参数,如果省略void,让括号为空,则C++将其解释为一个不接受任何参数的隐式声明
函数也可以没有返回值,比如void bucks(double);
3:用户定义的函数
标准C库提供了140多个预定义的函数。如果其中的函数能满足要求,则应使用它们,但经常需要编写自己的函数
对于库函数,在使用前必须提供原型,通常放在main()之前
第一个学习自定义的函数,有参数,无返回值
#include <iostream>
void simon(int n);
int main(void)
{
using namespace std;
simon(3);
return 0;
}
void simon(int n) //simon无返回值,参数为整形
{
using namespace std;
cout << "Simon says touch ur toes " << n <<" times." <<endl;
}
4 :用户定义的有返回值的函数
编写一个可以将英石转换为磅的工具,则有参数和返回值,假定都是int类型,则程序如下:
#include <iostream>
int stonetolb(int sts); //函数原型声明
int main(void)
{
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) //参数和返回值都成int类型
{
int pounds = 14 * sts;
return pounds;
}
该函数虽然短小,简单,但是包含了全部的函数特性;
·有函数头和函数体;
·接受一个参数;
·返回一个值
·需要一个原型
5:在多函数程序中使用using编译指令
让程序能够访问命名空间std的方法有多种,下面是其中4种
1)将using namespace std;放在函数定义之前,让文件的所有函数都能够访问到std中的所有元素
2)将using namespace std;放在特定的函数定义中,让该函数能够使用std中的所有元素
3)在特定的函数中使用类似using std::cout这样的指令,意思让该函数使用指定的元素
4)完全不使用using指令,而在需要使用命名空间中的元素时,使用前缀std::,如下示例
std::cout << "i'm using cout endl from the std namespace" << std::endl;
四、复习题和编程练习
复习题:
1:C++程序的模块叫什么?
答:函数,它是为实现某种特定功能,可随时调用
2:下面的预处理器编译指令是做什么用的?
#include <iostream>
答:包含iostream头文件,将头文件的内容添加到源代码中,标准输入输出函数
3:下面的语句作用是什么
using namespace std;
答:当前源代码使用std的命名空间,using是预编译器指令
4:什么语句可以用来打印"Hello World",然后开始新的一行?
答:cout << "Hello World" << endl;
5:什么语句可以用来创建名为cheeses的整数变量?
答:int cheeses;
6:什么语句可以将值32赋值给cheeses?
答:cheeses = 32;
7:什么语句可以用来将从键盘输入的值读入变量cheeses中?
答:cin >> cheeses;
8:什么语句可以用来打印 ”We have X varieties of cheese.“X为变量cheeses的当前值
答:cout << "We have " << X << "varieties of cheese" <<endl;
9:下面的函数原型指出了关于函数的哪些信息?
int froop(double t); 带一个类型为double的参数,返回值为Int
void rattle(int n); 带一个int类型的参数,无返回值
int prune(void); 不带参数,int返回值
10:什么时候不用return?
答:函数没有返回值则不需要使用return
11:假设您编写的main()函数包含如下代码:
cout << " Please entetr ur PIN: "
而编译器指出cout是一个未知标识符,导致这种问题的原因是什么,指出三种修复方法
答:
using namespace std;
using std::cout;
std::cout;
编程题:
1:编写一个C++程序,它显示您的姓名和地址。
#include <iostream>
int main(void)
{
using namespace std;
cout << "Frank" << endl;
cout << "Wall street" << endl;
return 0;
}
2:编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(1 long=220码)
#include <iostream>
int main(void)
{
using namespace std;
double distance;
cout << "Please Enter the Distance(in long):";
cin >> distance;
cout << "The distance " << distance << "long" << "equals " << 220 * distance << " yard" << endl;
return 0;
}
3:编写一个C++程序,它使用三个用户定义的函数(包含main()),并生成下面的输出:
Three blinde mice
Three blind mice
See how they run
See how they run
其中一个函数要调用两次,该函数生成前两行,另一个函数也被调用两次,并生成其余的输出
#include <iostream>
void Print_mice( void);
void Print_run(void);
using namespace std;
int main(void)
{
Print_mice();
Print_mice();
Print_run();
Print_run();
return 0;
}
void Print_mice( void)
{
cout << "Three blind mice." << endl;
}
void Print_run(void)
{
cout << "See how they run." << endl;
}
4:编写一个程序,让用户输入年龄。然后显示该年龄包含多少个月
Enter ur age:29
Your age in mouths is 348.
#include <iostream>
int main(void)
{
using namespace std;
int age;
cout << "Please enter ur ages:";
cin >> age;
cout <<"Your age in mouths is " << age * 12 << endl;
return 0;
}
5:编写一个程序,其中的main()调用一个用户定义的函数(以摄氏温度为参数,并返回相应的华氏温度),按照下面的格式输出:
Please enter a Celsius value:20;
20 degree Celsius is 68 degree Fahrenheit.
转化公式如下:华氏温度=1.8×摄氏温度+32.0
#include <iostream>
double Trans(double Celsius);
int main()
{
using namespace std;
double Celsius;
cout << "Please enter a Celsius value: ";
cin >> Celsius;
double Fahren =Trans(Celsius);
cout << Celsius <<" Celsius" << " equals " << Fahren << " Fahrenheit";
return 0;
}
double Trans(double Celsius)
{
double Fahren = 1.8 * Celsius + 32.0;
return Fahren;
}
自定义函数的返回部分可以直接合成
return 1.8 * Celsius + 32.0;
6:编写一个程序,其main()调用一个用户定义的函数(以光年为参数,返回天文单位的值)
Enter the number of light years:4.2;
4.2 light years = 265608 astronomical units.
使用double类型,1光年=63240天文单位
#include <iostream>
using namespace std;
double Trans(double l);
int main(void)
{
double light,astro;
cout << "Enter the number of light years:";
cin >> light;
astro = Trans(light);
cout << light << "light years equals " << astro << " astromical units";
return 0;
}
double Trans(double l)
{
return l * 63240;
}
跟上面一道题几乎一样
7:编写一个程序,要求用户输入小时数和分钟数,在main()函数中,将这两个值传递给一个void函数,后者以这样的格式显示这两个值
Enter the number of hours :9
Enter the number of minutes:28
Time:9:28
#include <iostream>
using namespace std;
void Show(int h, int m);
int main(void)
{
int hours,minutes;
cout << "Enter the number of hours:";
cin >> hours;
cout << "Enter the number of minutes:";
cin >> minutes;
Show(hours,minutes);
return 0;
}
void Show(int h, int m)
{
cout << "Time:" << h << ":" << m << endl;
}
注意:
*在传递参数的时候,要保证你函数声明的参数类型和main函数中的输入参数类型保持一致
*引用函数的时候要和下面的保持一致
*传递几个参数就在自定义函数时写几个变量
二、