一.C++环境安装(虚拟机下)
下载好安装文件后,在待安装目录执行以下命令:
sudo dpkg -i *.deb
二.C++的语言特点及优势
1.C++实现了面向对象的程序设计
在高级语言当中,C++的处理运行速度是最快的。
2.C++语言非常灵活,功能非常强大
如果说C语言的优点是指针,那么C++的优点就是性能和类层次结构的设计。
3.C++非常严谨、精确和数理化,标准定义很细致
4.C++语言的语法思路层次分明、相呼应,语法结构是显式的、明确的
三.简介
1.文件后缀
cpp 即 c plus plus
2.编译命令(虚拟机下)
g++
3.C++的标准库头文件没有.h
标准输入输出流
#include <iostream>
4.C++可以兼容C中的头文件
C #include <stdio.h>
C++ #include <cstdio>
C #include <string.h>
C++ #include <cstring>
四.C++与C的区别
C语言面向过程,而C++面向对象
以五子棋为例
面向过程的设计思路
1.开始游戏
2.黑子先行
3.绘制画面
4.判断输赢
5.白子后行
6.绘制画面
7.判断输赢
8.回到步骤2
9.输出最后结果
面向对象的设计思路
1.玩家系统:黑白双方,行为是一模一样的
2.棋盘系统:负责绘制画面
3.规则系统:负责判定诸如犯规、输赢等
第一类对象(玩家对象)负责接受用户输入
并告知第二类对象(棋盘对象)棋子布局的变化,棋盘对象收到了棋子的变化后负责在屏幕上显示出这种变化
同时利用第三类对象(规则系统)来对棋局进行判定
总结:
(1)面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候一个一个调用即可。
(2)面向对象是把构成问题的事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描述某个事物在整个解决问题的步骤中的行为。
五.标准输入输出
1.输出Hello World
#include <iostream>
int main()
{
std::cout << "Hello World";
}
程序解释
std 命名空间
:: 作用域限定运算符
cout 标准输出 类对象
<< 输出运算符 重载左移运算
endl 代表换行
编译
g++ HelloWold.cpp
运行
./a.out
2.输出流对象可以自动识别基本类型的输出类型
示例
int a = 10;
char ch = 'a';
std ::cout << a;
std ::cout << ch;
也可以连锁调用
std ::cout << ch << a << std ::endl;
3.小练习
使用for循环输出10个整数
for( int i = 0 ; i < 10 ; i++ )
{
std ::cout << i <<std ::endl;
}
在C++中for循环的表达式1可以定义变量,变量的作用域是表达式1,2,3和循环体,生命周期是整个for循环的过程。
4.输入流
输入流对象可以自动识别基本类型的输出类型
scanf 遇到空格和回车代表输入结束。
示例
int a ;
std ::cin >> a;
std ::cout << a << std ::endl;
程序解释
cin 标准输入 功能等同于scanf
>> 代表输入运算符 运算符重载
练习
输入一个字符 实现大小写互相转换
#include <iostream>
int main()
{
char ch;
std ::cin >> ch;
if(ch>'=a'&&ch<='z')
{
ch -= 32;
}
else if(ch>='A'&&ch<='Z')
{
ch += 32;
}
else
{
std::cout << "error"<<endl;
}
std::cout << ch <<endl;
return 0;
}
5.cin同时获取多个值
示例
char ch,ch1;
std :: cin >> ch >>ch1;//输入a b
四.命名空间 std
1.使用命名空间的目的
避免命名空间污染
注意:C++只有一个命名空间std;
C++定义所有的类和对象都是std这个命名空间下命名的
C++的标准库都是在std命名空间中定义的
2.命名空间的表示使用方法
标准库的全部都放在命名空间std中
使用时 加上using namespace std;即可
示例
#include <iostream>
using namespace std;
int main()
{
char ch,ch1;
cin >> ch >>ch1;
cout << ch << ch1 << endl;
return 0;
}
3.自定义命名空间
示例:自定义命名空间student
#include <iostream>
using namespace std;
char name[20] = "xiaoming"; //全局变量name
namespace student{ //全局命名空间student
char name[20] = "xiaoli";
int age = 18;
void fun(){
cout << name << age << endl; //局部优先
}
};
using namespace student;
int main()
{
cout << ::name <<endl; //xiaoming 调用匿名命名空间 全局变量name
cout << student::name <<endl; //xiaoli
fun(); //xiaoli 18 student下的fun
return 0;
}
命名空间的作用只在起名上。
当全局变量和局部变量冲突时,局部优先。
在冲突情况下,想调用全局变量,可以将其存放在匿名空间。
五.字符串处理
1.C++字符串有两种处理方法
(1)c风格
用字符数组存储,用字符指针操作
char s[] = "helloworld";
char *p = s;
c语言字符串的两个要素
1.遇到“\0”结束
2.只能记录字符串的首地址
(2)C++风格
在标准库中使用string类
2.string类的操作
(1)需要加头文件
#include
(2)需要加std命名空间
using namespace std;
3.字符串的常用操作
赋值 连接 拷贝
示例:创建string对象
#include <iostream>
using namespace std;
#include <string>
#include <cstring> //strcmp strcat
#include <cstdlib>
int main(int argc, const char *argv[])
{
char buff[] = "hello"; //栈区
char *buff = (char*)malloc(sizeof(char)*6); //堆区
if(NULL == buff){
cout << "NULLPOINTER" ;
}
strcpy(buff,"hello"); //字符串拷贝
cout << buff << endl; //hello
free(buff);
cout << sizeof(buff) << endl; -->4
cout << buff << endl; //hello
cout << sizeof(buff) << endl; -->6
string buff1="world"; //初始化方法一 从无到有的过程
string buff2("world"); //初始化方法二 从无到有的过程
cout << buff1 << endl;
cout << sizeof(buff1) << endl;
//4 本质是个结构体 包含一个char* 的指针 按照结构体对齐原则
//所以 string的大小 = 4
return 0;
}
示例:转换
(1)c风格转为C++风格
string s = "abcd";
(2)将string转换成C风格字符串
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
void fun()
{
string buf ("hello");
char array[20] = {0};
sprintf(array,"%s",buf.c_str);
cout << array <<endl;
}
int main()
{
fun();
return 0;
}
**注意:**string也是一个类(也就是结构体),参照结构体使用。
补充:结构体相关知识
(1)C与C++中,定义结构体的不同方式
首先,在C中定义一个结构体最好用到typedef
typedef struct student
{
int a;
}stu;
这样在声明结构体变量s的时候就可以直接写:stu s;
如果不使用typedef则必须要用struct student s来声明。
struct student
{
int a;
};
这里的stu实际上就是struct student的别名
struct student = stu。
另外这里也可以不写student,但这样就无法再使用struct student s的方法来声明,只能使用stu s了。
typedef struct
{
int a;
}stu;
但在C++中,结构体的声明却很简单,只需
struct student
{
int a;
};
这样就定义了结构体类型student,声明变量时直接student s即可。
C++的定义及声明方法相当于综合了C中最简洁的定义方法和声明变量方法。
(2)C++中typedef的使用
struct student
{
int a;
}s1;
s1是一个变量。
typedef struct student
{
int a;
}s2;
s2是一个结构体类型,= struct student。
使用s1时可以直接访问s1.a=10。
使用s2则必须先s2 ss2,然后再ss2.a=10。
typedef解析
很多视频教程在涉及到typedef时,为了便于理解,会将typedef解释成起别名,这种说法其实容易造成一些误解
typedef的准确释义是定义(define)同义类型(type)
语法为:typedef -已存在类型-自定义类型
例如:typedef int integer就是为int型定义了一个同义类型integer。定义后就可以使用integer来声明int变量。
举一反三:typedef int* intpointer 后,int* p和intpointer p是等同的。
同理,在结构体声明中的typedef实际上是为整个结构体
struct student{
int a;
};
申请了一个同义变量s2,只是在定义结构体变量的过程中,为了方便使用,省略了结构体的主体,只使用struct student来进行定义,进而产生了struct student = s2的说法
示例:字符串赋值
string对象赋值可以通过赋值符号 =,直接将值赋予给目标对象
string buff1="world";//初始化
buff1 = "hello";//赋值 strcpy 说明变量buff1已经存在 因此叫赋值
示例:字符串拼接
string可以通过”+”来与string对象进行拼接
string buff1 = "hello";//初始化
string buff2("world");//初始化
buff1 = buff1 + buff2;//buff1+=buff2;
cout << buff1 << endl//--->helloworld
buff1+=1;//err 无法进行拼接 复杂的字符串格式化只能使用sprintf()
示例:字符串比较
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "hello world";
string s2 = "hello world";
cout<<(s1==s2)<<endl; // 1
string s3 = "hello farsight";
cout<<(s3!=s1)<<endl; // 1
cout<<(s1>s3)<<endl; // 1
// 比较第一个不相等字符的大小
}
示例:string类型的成员函数 empty() 和 size()
string buff2 = "hello";
string buff3 = "heaabb";
if(buff2 > buff3){
cout << "buff2>buff3"<< endl;
}
cout << buff2.empty() << endl; //0代表不是空串 1代表是空串
cout << buff2.size() << endl; //strlen 字符串的有效字符个数 5
string buff4= "";
cout << buff4.empty() << endl; //1
cout << buff4.size() << endl; //0
示例:获取字符对象
C++可以通过
1)字符串对象[下标]来直接获取该位置的字符。
2)字符串对象.at(下标)来直接获取该位置的字符。
将string中的每一个字符输出。
string str = "hello";
cout << str;
for(int i=0;i<str.size();i++){
cout << str[i] << endl; //[]运算不能提示数组是否越界
}
或者
for(int i=0;i<str.size();i++){
cout << str.at(i) << endl; //at可以提示数组越界
}
示例:cin输入字符串
cin无法输入空格 如果输入有空格 只拿到空格之前的
用法参照scanf (无法输入空格)
string buff;
cin >> buff; //hello world
cout << buff << endl; //hello
示例:getline输入字符串 遇到’\n’结束输入
读入一行的字符串,包括空格。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin,s);//hello world
cout<<s<<endl;//hello world
}
六.布尔类型
1.表示真或假
C++依然可以使用0和非0表示真假
C++还增加了bool类型专门表示真假true false
真假:非0为真(true) 0是假(false)
示例
#include<iostream>
using namespace std;
int main()
{
bool b1;
b1 = 10;
cout<<b1<<endl; //1
bool b2;
b2 = -23;
cout<<b2<<endl; //1
bool b5;
b5 = 0;//假
cout<<b5<<endl; //0
bool b3;
b3 = true;
cout<<b3<<endl; //1
bool b4;
b4 = false;
cout<<b4<<endl; //0
cout<<sizeof(b4)<<endl; //1
}
2.boolalpha和noboolalpha
boolalpha可以将从该行起直至结尾的所有bool值1或0转换成true或false,直至遇到noboolalpha,它会将true或false转换回bool值1或0
示例:
bool a = -5;
cout << a << endl; //1
cout << boolalpha << a << endl;//true
bool b = 0;
cout << b << endl;//false
bool c = 100;
cout << noboolalpha << c << endl;//1