知识点补充
学习目的:
C++异常时的处理,及其过程;
知道流的概念,会使用对文件进行操作;
C++11新标准;
内容:
异常处理
文件流(***)
C++11标准(**)
异常处理机制
异常处理关键字:
throw:抛出异常提示;
try: 尝试执行可能会有异常的代码;
catch: catch接收抛出的异常并响应;
语法:
try
{
// ....
直接或间接有throw
// ....
}
catch(接收异常)
{
处理
}
示例:
#include <iostream>
using namespace std;
const double& test(const double& a, const double& b)
{
if (b == 0.0)
{
// 有异常情况==>抛出
throw "分母不能为0!"; // throw抛出的异常信息:支持多种数据类型
// 如果抛出异常,后面的代码就不再执行。
}
return (a / b);
}
int main()
{
double num = 9;
double val = 0;
double& n = num;
double& v = val;
try
{
cout << test(n, v) << endl;
}
catch (const char* str)
{
cout << str << endl;
}
catch (...) // 抛出其他类型的信息,类似 switch 里的 default
{
cout << "出问题了!" << endl;
}
}
使用自定义异常:
C++中一些处理异常的类:
代码:
#include <iostream>
using namespace std;
#include <exception>
// 从
class MyException : public exception
{
public:
const char* what() const;
};
const char* MyException::what() const
{
return "自定义的异常!"; // 一般写为英文
}
int main()
{
try
{
// 抛出一个匿名类
throw MyException();
}
catch (MyException& obj) // 引用类型的接收
{
cout << obj.what() << endl;
}
return 0;
}
文件流
流:建立在面向对象基础上的一种抽象的处理数据的工具,用于实现数据的无结构化传递;
fstream : C++中最常用的文件操作类;
示例1:
#include <iostream>
using namespace std;
#include <fstream>
int main()
{
fstream obj;
obj.open("Hello.txt",ios::out);
obj.put('S');
char ch = 'A';
obj.put(ch);
obj.close();
obj.open("Hello.txt", ios::in);
obj.get(ch);
cout << ch << endl;
ch = obj.get();
cout << ch << endl;
obj.close();
return 0;
}
示例2:二进制读写
#include <iostream>
using namespace std;
#include <fstream>
int main()
{
fstream obj;
obj.open("Hello.txt", ios::out);
int num = 0XAB12CD34;
// 二进制形式读写
obj.write((const char*)&num, 1);
obj.close();
obj.open("Hello.txt", ios::in);
int val = 0;
obj.read((char*)&val, sizeof(int));
obj.close();
cout << hex << "val = " << val << endl;
return 0;
}
使用重载的 << 和 >>
#include <iostream>
using namespace std;
#include <fstream>
int main()
{
fstream obj;
obj.open("Hello.txt", ios::out);
obj << "Hello,world!" << endl;
obj << "123" << endl;
obj.close();
char str[64];
int num = 0;
obj.open("Hello.txt", ios::in);
obj >> str;
obj >> num;
cout << str << endl;
cout << "num = " << num << endl;
obj.close();
return 0;
}
C++11标准
- 初始化的方式
int a = 0;
int b(2.1);
int m{4};
int n = {(int)3.14} //新的写法不允许有精度的缺失
- 指针置空
int* p = NULL; // 宏定义
int* p = nullptr; // 关键字
- 自动类型
int num(0);
int* p(&num);
auto p(&num); // 根据值匹配类型
- decltype():赋值类型
int a = 0;
decltype(a)b; // 根据a的类型定义一个和a类型一致的b变量
decltype((a))m = a; // 给a取别名m
- 新的for规则
#include <iostream>
using namespace std;
#include <string>
int main()
{
string str("abcdefg");
for (int i = 0; i < str.size(); i++)
{
cout << str[i] << " ";
}
// C++11:
// 使用 for 循环遍历 str,每次使用 ch 去接收 str 的当前元素
for (auto ch : str)
{
cout << ch << " ";
}
// 局限性:只能用来遍历数组等容器 i 所代表的是里面存的元素
return 0;
}
- 给类型取别名
typedef int INT;
typedef void(*pFun)();
using Int = int;
using PFUN = void(*)();
- default在类中的用法
class CA
{
public:
CA();
};
CA::CA() = default; // 默认调用默认构造
- final:阻止类的继承 || 虚函数的重写
// 阻止类的继承 || 虚函数的重写
class CA
{
public:
virtual void fun() final{}
}
<本节完>