C++整理08
崖高人远
这个作者很懒,什么都没留下…
展开
-
数组初始化对象,ifstream,ofstream
#include #include #include using namespace std;class Student{private://私有成员变量 char name[10]; int age; bool gender;public: Student() { } Student(char* name,int age,bool gender):age(ag转载 2012-10-01 01:26:17 · 1479 阅读 · 0 评论 -
throw一层一层的向被调用者抛出异常
#include using namespace std;void m4() throw(const char*) //throw里面只能写类型,不能添加别的什么{ throw "旅游出事了,死了100";}void m3() throw(const char*){ try { m4(); } catch(const char * e) { cout << "m3转载 2012-10-01 16:40:49 · 529 阅读 · 0 评论 -
unformat(程序有点问题)
#include #include using namespace std;int main(){ char c1,c2,c3,c4; cin >> c1 >> c2 >> c3 >> c4; cout << c1 << c2 << c3 << c4 << endl; cin.get(c1); cin.get(c1).get(c2).get(c3).get(c4); cout转载 2012-10-01 23:55:03 · 359 阅读 · 0 评论 -
ofstream写文件
#include #include using namespace std;//写文件int main(){ cout << "Hello" << endl; ofstream fout("a.txt"); //将我已存在的文件的内容给覆盖了,话说这是不是用二进制给写进去的? //用cat 和 more 都可以查看a.txt 的内容,无异常 fout << "Hello" <转载 2012-10-01 22:15:39 · 495 阅读 · 0 评论 -
ifstream构造方法,成员函数getline
#include #include using namespace std;int main(){ //这里没有填写文件的打开模式 //ifstream构造方法为: //ofstream(const char* szName,int nMode = ios::out,int nProt = filebuf::openprot //szName 指定将要打开的文件名 nMode 指转载 2012-10-01 18:45:30 · 1860 阅读 · 0 评论 -
模拟异常类的编写
#include #include using namespace std;//总异常class BankException{};//业务异常class BusinessException : public BankException{};class TransferException : public BusinessException{};class Logi转载 2012-10-01 16:57:08 · 387 阅读 · 0 评论 -
vdestroy
#include using namespace std;class Parent{public: Parent() { cout << "Parent()" << endl; } virtual ~Parent() { cout << "~Parent()" << endl; } };class Child : public Parent{public:转载 2012-10-01 23:55:33 · 285 阅读 · 0 评论 -
fstream成员函数tellg,tellp,seekg,get,seekp,peek
#include using namespace std;int main(){ //Linux下的资料查得特别蛋疼。这个程序自己都没有运行。 fstream file("a.txt"); cout << file.tellg() << endl; cout << file.tellp() << endl; char ch; file.get(ch); cout << fi转载 2012-10-01 23:33:04 · 1481 阅读 · 0 评论 -
标准输出、标准日志输出、标注错误输出、清空缓冲区
#include using namespace std;int main(){ //1.有输入。2.手动刷新。3.程序运行结束。4.输出换行。5.缓冲区满。 //这五种情况会清空缓冲区 //标注输出带缓冲区,可以重定向,什么叫重定向? cout << "Hello" ; //标准输出 //日志输出同cerr clog << "LOGERR" ; //日志输出 //错误输转载 2012-10-01 23:23:15 · 519 阅读 · 0 评论 -
fstream文件打开模式
#include #include using namespace std;int main(){ //模式:out 打开文件,用于输出 //in 如果制定了此模式,则原始文件(如果存在的化)将不会被截断(截断啥意思?) //trunc 如果文件已经存在,则它的内容将被清空。如果指定了ios::out模式,并且没有指定ios::app,ios::ate,ios::in模式,则就隐转载 2012-10-01 18:26:14 · 512 阅读 · 0 评论 -
格式化输出相关
#include #include using namespace std;//格式化输出int main(){ int x = -10; double d = 3.14; cout << x << d << endl; cout << '[' << setw(10) << x << ']' << endl; cout << left; cout << '[' << set转载 2012-10-01 17:34:51 · 370 阅读 · 0 评论 -
子类函数执行的异常范围不能比父类广
//并没有打算让这个程序运行,只是说明一下#include using namespace std;class A{public: virtual void f() //抛出任何类型的异常,所以class B 的虚函数f()这行不会报错 { } virtual void g() throw(int,double) { } virtual void h() throw(i转载 2012-10-01 17:08:54 · 1186 阅读 · 0 评论 -
异常抛出类型
#include #include using namespace std;void transfer() throw(const char*,int,double) //如果不要throw语句,则说明捕捉任何异常。写了可能抛出的异常类型,就不会捕捉其他类型的异常了?{ cout << "开始转账业务处理" << endl; srand(time(0)); int x = rand转载 2012-10-01 14:33:56 · 371 阅读 · 0 评论 -
throw、try、catch
#include #include using namespace std;/******************************************************** * function :visit database * * param :no * * return :0 successfule -1 failed *转载 2012-10-01 14:08:14 · 281 阅读 · 0 评论 -
数组初始化对象,ifstream,ofstream 2
#include #include #include using namespace std;class Student{private: //只是保存名字的变量类型换成了string string name; int age; bool gender;public: Student() { } Student(string name,int age,bool g转载 2012-10-01 01:38:03 · 473 阅读 · 0 评论 -
模拟数据库类
#include using namespace std;//抽象类,相当与接口class Database{public: //连接到数据库 纯虚函数 virtual void connect() = 0; //执行SQL语句 纯虚函数 virtual void query(string sql) = 0;};//程序员使用数据开发class Program{转载 2012-10-01 02:41:09 · 374 阅读 · 0 评论 -
主函数默认参数,ifstream,ofstream,文件内容拷贝
#include #include using namespace std;//带参数的主函数int main(int argc, char** argv){ if(3 != argc) { cerr << "Usage:" << argv[0] << "Source target" << endl; } //argv是文件名 ifstream fin(argv[1]);转载 2012-10-01 02:10:58 · 982 阅读 · 0 评论 -
fstream(存在问题 读取前面的stu.dat 会出现段错误)
#include #include #include using namespace std;class Student{//私有成员变量private: string name; int age; bool gender;public://空参构造函数 Student() { }//带参构造函数 Student(string name, int age ,转载 2012-10-01 01:52:14 · 1640 阅读 · 0 评论 -
io出错处理
#include using namespace std;int main(){ cout << cin << endl; cout << "请输入一个整数:" ; int x; cin >> x; cout << "x = " << x << endl; //如果这里接收的是字符或字符串,那么cin就会出错。 cout << cin << endl; if(!cin)转载 2012-10-01 21:53:52 · 492 阅读 · 0 评论