面向对象程序设计-C++ Stream & Template & Exception【第十五次上课笔记】
这是本门《面向对象程序设计》课最后一次上课,刚好上完了这本《Thinking in C++》 :)
这节课首先讲了流 Stream 的概念
平时我们主要用的是(1)在屏幕上输入输出的 cin cout 流
(2)在文件中输入输出的 ifstream ofstream 流
(3)在字符串中输入输出的 istringstream ostringstream istrstream ostrstream 流
具体实例可以看以下代码:
/*************************************************************************
> File Name: Code09.cpp
> Author: Jeremy Wu
> Created Time: Mon 08 Jun 2015 09:55:51 AM CST
************************************************************************/ //跟字符串相关的流:1)支持C风格的字符串流
// 2)支持C++风格的字符串流
// 1 ) strstream (istrstrstream, ostrstream)
// 2 ) stringstream (istringstream, ostringstream) #include <iostream>
#include <sstream>
#include <string> using namespace std; float str_to_float (const char * s) {
istrstream tmp (s, strlen (s));
float ans;
tmp >> ans;
return ans;
} int main () { //想把整数输入到字符串里 ostringstream oss;
int i = ;
double d = 1.414; oss << i << '\t' <<d;
string str = oss.str (); cout << str << endl; //C++ 字符串 istringstream iss ("223 2.414");
iss >> i >> d;
cout << i << '\t'<< d << endl; //C 字符串 char *s = "v -3.1 // 3.1415"; return ;
}
在C语言中对输出的控制是非常方便的,同样,在C++中也可以对输出作格式控制,并且功能更加强大
在控制格式上有两种方法: (1)使用 setfs 等控制符
(2)cout 的时候使用操作值
/*************************************************************************
> File Name: Code07.cpp
> Author: Jeremy Wu
> Created Time: Mon 08 Jun 2015 09:46:45 AM CST
************************************************************************/ //流的输入输出的格式化
// 控制格式两种方式 1)使用 setfs 等控制符
// 2)cout 的时候使用操作值 #include <iostream> using namespace std; //ios 是所有流的基类 ios::fmtflags oldFlag = cout.flags (); int main () { int i = ;
cout.setf (ios::showpos); //显示正负号
cout << i << endl;
cout.setf (ios::showbase); //显示进制号
//16进制输出
cout << hex << i << endl;
//8进制输出
cout << oct << i << endl << endl; cout.flags (oldFlag); //默认保留6位数字
double f = 1234.56789;
cout << "default: \t" << f << endl; //科学计数法,默认保留小数后6位数字
cout.setf (ios::scientific, ios::floatfield);
cout << "scientific: \t" << f << endl; //固定点模式,默认保留小数后6位数字
cout.setf (ios::fixed, ios::floatfield);
cout << "fixed: \t " << f << endl; //恢复默认值
cout.setf (ios::fmtflags (), ios::floatfield);
cout << "default: \t" << f << endl << endl; //输出浮点数默认精度
cout << "default precision is " << cout.precision () << endl; //修改默认精度
cout.precision (); cout << "after precision (8)" << endl;
cout << "default: \t" << f << endl; cout.setf (ios::scientific, ios::floatfield);
cout << "scientific: \t" << f << endl; cout.setf (ios::fixed, ios::floatfield);
cout << "fixed: \t" << f << endl << endl; cout.flags (oldFlag); //输出预留宽度
cout << "Current width: \t" << cout.width ()
<< "\t Currend fill character : \t" << cout.fill () << endl; cout.precision (); //设置预留宽度
//设置填充为 #
//默认按右边对齐
//
// ·修改 width 只针对下一次输出有效
//
cout.width ();
cout.fill ('#'); cout << f << endl; cout << "Current width: \t" << cout.width ()
<< "\t Currend fill character : \t" << cout.fill () << endl; cout << f << endl << endl; cout.width ();
cout << -f << endl;
cout.width (); //左边对齐
cout.setf (ios::left, ios::adjustfield);
cout << -f << endl;
cout.width (); //中间对齐
cout.setf (ios::internal, ios::adjustfield);
cout << -f << endl << endl; cout.flags (oldFlag); return ;
}
在C++ 中有个非常有用的功能叫做模板 Template
以下是一个 Template 简短 的例子
在自己写模板的时候得注意许多小细节,比较在类的外部定义函数的时候需要在前面加上模板头还要在类后面加上<T>等
/*************************************************************************
> File Name: Code08.cpp
> Author: Jeremy Wu
> Created Time: Mon 08 Jun 2015 09:51:33 AM CST
************************************************************************/ //函数模板
//实例化的真正含义: 函数模板 -> 模板函数 #include <iostream>
#include <cstring> using namespace std; /*
int sum (int a, int b) {
return a + b;
} double sum (double a, double b) {
return a + b;
}
*/ //模板的定义
template <typename T> //template head
T sum (T _a, T _b) {
return _a + _b;
} //模板变量和模板常量(可以提供默认值
template <typename T = double, int Size = >
class Array {
private:
//const static int Size = 100;
T a[Size];
public:
Array () { memset (a, , sizeof (a)); }
T & operator [] (int index);
Array & operator + (T rhs) {
for (int i = ; i < Size; ++i) a[i] += rhs;
return *this;
}
}; //在类的外部定义的时候
// 1)加模板头
// 2)类后面加 <T>
template <typename T, int Size>
T & Array <T, Size>::operator [] (int index) {
if (index < || index >= Size) {
cout << "out of range" << endl;
//exit (0);
} else {
return a[index];
} } int main () { //模板的实例化 : 显示实例化 和 隐示实例化
/*
int i (10), j (7);
cout << sum (i, j) << endl; double d1 (12.3), d2 (7.9);
cout << sum <int> (d1, d2) << endl;
*/ //Array <double, 100> ay;
Array <>ay;
ay[] = ;
cout << ay[] << endl; ay = ay + 5.2;
cout << ay[] << endl; return ;
}
接下来是异常处理
需要正确理解在异常处理整个周期中流程
catch (...) // ... 可以捕捉任何类型的异常,并且这句 catch 必须放在所有 catch 的最后
/*************************************************************************
> File Name: Code10.cpp
> Author: Jeremy Wu
> Created Time: Mon 08 Jun 2015 11:44:31 AM CST
************************************************************************/ //异常处理: exception handling
//try \ throw \ catch
//要理解异常处理的执行顺序 #include <iostream>
#include <string> using namespace std; class ExcepClass {
public:
string message () { return "devide number is invalid"; }
}; double devide (int a, int b) {
if ( == b) throw ExcepClass (); //如果符合条件,函数结束,所有局部变量释放
cout << "Just after throw" << endl;
return double (a) / b;
} int main () { cout << "Before exception" << endl; try {
int i (), j ();
cout << devide (i, j) << endl; //出现异常以后的所有代码都不会被执行
cout << "After devide (i, j)" << endl;
} catch (ExcepClass k) { //类型必须匹配捕捉到的类型
cout << k.message () << endl;
} catch (int m) { }
catch (...) { //捕捉任何类型的异常,并且...必须放在最后
cout << "Captur all exception" << endl;
} //结束异常处理
cout << "After capture processing" << endl; return ;
}
至此,一系列上课笔记
END
面向对象程序设计-C++ Stream & Template & Exception【第十五次上课笔记】的更多相关文章
- 201271050130-滕江南-《面向对象程序设计(java)》第十五周学习总结
201271050130-滕江南-<面向对象程序设计(java)>第十五周学习总结 博文正文开头格式:(2分) 项目 内容 这个作业属于哪个课程 https://www.cnblogs.c ...
- 201871010111-刘佳华《面向对象程序设计(java)》第十五周学习总结
201871010111-刘佳华<面向对象程序设计(java)>第十五周学习总结 实验十三 Swing图形界面组件(二) 实验时间 2019-12-6 第一部分:理论知识总结 5> ...
- 201871010123-吴丽丽《面向对象程序设计(Java)》第十五周学习总结
201871010123-吴丽丽<面向对象程序设计(Java)>第十五周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- 201871010104-陈园园 《面向对象程序设计(java)》第十五周学习总结
201871010104-陈园园 <面向对象程序设计(java)>第十五周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- 201871010105-曹玉中《面向对象程序设计(java)》第十五周学习总结
201871010105-曹玉中<面向对象程序设计(java)>第十五周学习总结 项目 内容 这个作业属于哪个过程 https://www.cnblogs.com/nwnu-daizh/ ...
- 201871010107-公海瑜《面向对象程序设计(java)》第十五周学习总结
201871010107-公海瑜<面向对象程序设计(java)>第十五周学习总结 项目 内容 这个作业属于 ...
- 201871010128-杨丽霞《面向对象程序设计(java)》第十五周学习总结
201871010128-杨丽霞<面向对象程序设计(java)>第十五周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- 201871010133-赵永军《面向对象程序设计(java)》第十五周学习总结
201871010133-赵永军<面向对象程序设计(java)>第十五周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- 201871020225-牟星源《面向对象程序设计(java)》第十五周学习总结
201871020225-牟星源<面向对象程序设计(java)>第十五周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
随机推荐
- 《Java4Android视频教程》学习笔记(二)
一:面向对象 1.对象 ①对象的使用方法 对象.变量 对象.方法 ②匿名对象 new A().方法 new A().变量 匿名对象会被分配到对内存中 java内存处理机制会对一定时间内无指针指向的对象 ...
- HDU 4366 Successor(树链剖分+zkw线段树+扫描线)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4366 [题目大意] 有一个公司,每个员工都有一个上司,所有的人呈树状关系,现在给出每个人的忠诚值和 ...
- raphael入门到精通---入门篇之总览
什么是Raphael raphael.js是一小巧的javascript库,它可以在web上画矢量图简化你的工作,如果你想创建你指定的图表,图形区域或者可移动的组件,那么就使用raphael吧 话不多 ...
- [01] Preparation - Sitecore Installment
Sitecore CMS 是一套内容管理系统商业软件,其底层平台依托于微软.net技术.由于最近的一个项目采用了这个平台,所以有机会接触到了这个产品. 虽然接触该产品已有一段时间,但总感觉对这个产品缺 ...
- CentOS下成功挂载xxxxxDVDx.iso并使用yum安装软件
CentOS下成功挂载xxxxxDVDx.iso并使用yum安装软件 **不断尝试,终能到达彼岸** 测试环境为Win7 32位,VirtualBOx4.2.16+CentOS6.5,可分别到virt ...
- <精华篇>:iOS视频大全-持续更新
注意:新浪微博分享的资料和简书分享的资料,略有不同! 小码哥swift3.0版 斗鱼项目视频:点击下载 iOS开发25个项目实战:点击下载 2016PHP全套下载:点击下载 黑马刀哥iOS视频精选 ...
- 解决 Tomcat reload WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [] registered the JDBC driver [com.mysql.jdbc.Driver] but fail
转自:http://www.cnblogs.com/interdrp/p/5632529.html 我的错误如下: 06-Sep-2016 18:57:10.595 WARNING [localhos ...
- PHP输出中文乱码的解决方法
最近在windows上发现PHP程序中输出来的中文有乱码的情况. 看了很多帖子资料说可以在页面上添加: http://www.cnblogs.com/leandro/archive/2008/04/2 ...
- Android 开发笔记“关闭默认键盘”
1.打开AndroidManifest.xml文件 2.在对应的activity中增加配置信息 android:windowSoftInputMode="stateHidden"
- java 简单的词法分析
package com.seakt.example; import java.io.*; import java.lang.String; public class J_Scanner { publi ...