文章分类 - 自考教材 C++(2019版)课本内容
摘要:/* 函数模板与函数的区别:1)函数模板本身在编译时不会生成任何目标代码,只有当通过模板生成具有的函数实例时才会生成目标代码。2)被多个源文件引用的函数模板,应当连同函数体一同放在头文件中,而不能像普通函数那样只将声明放在头文件中。3)函数指针也只能指向模板的实例,而不能指向模板本身。 */ #in
阅读全文
摘要:/* 背景: 设计程序中的函数时,可能会遇到函数中参数的类型有差异,但需要实现的功能类似的情形。函数重载可以处理这种情形。但是,如果还要交换其他类型的量,则还需编写另外的Swap()函数。 为了提供效率,实现代码复用,C++提供了一种处理机制,即使用函数模板。函数在设计时并不使用实际的类型,而是使用
阅读全文
摘要:#include<iostream>#include<fstream>#include<iomanip>#include<string>using namespace std; int main(){ string file1,file2; int l = 1; char ch; cout<<"请输
阅读全文
摘要:#include <iostream>#include <fstream>#include <math.h>using namespace std; class triangle{private: int a, b, c; double area;public: triangle(int aa, i
阅读全文
摘要:#include<iostream>#include<fstream>using namespace std; int main(){ fstream infile; infile.open("myfile.txt",ios::in); if(!infile) { cout<<"文件打开时发生错误.
阅读全文
摘要:#include <iostream>#include <fstream>using namespace std; int main(){ char k; int i; ofstream outf("c:\\tmp\\tem.dat",ios::trunc); outf << "Hello worl
阅读全文
摘要:#include <iostream>#include <fstream>using namespace std; int main(){ int i; ifstream f1("c:\\tmp\\xt1.txt",ios::in); fstream f2("c:\\tmp\\xt2.txt",io
阅读全文
摘要:#include<iostream>#include<fstream>using namespace std; int main(){ int i; ofstream ftxt1; ftxt1.open("xt1.txt",ios::out); for(i = 1;i < 10;i++) ftxt1
阅读全文
摘要:#include <iostream>#include <fstream>using namespace std; class CStudent{public: char id[11]; //学号 char name[21]; //姓名 int score; //成绩};int main(){ CS
阅读全文
摘要:/* 背景: 如果一个文件只能进行顺序存取操作,则称为顺序文件。典型的顺序文件是键盘、显示器和保存在磁带上的文件。 如果一个文件可以在文件的任意位置进行存取操作,则称为随机文件。磁盘文件就是典型的随机文件。 类istream中与位置指针相关的函数如下:(1)移动读指针函数:a、istream & s
阅读全文
摘要:/* 用成员函数put()和get()读写文件: 可以用类ifstream和类fstream的成员函数get()从文件中一次读取一个字节,也可以用类ofstream和类fstream的成员函数put()向文件中一次写入一个字节。 常用于读写字符或文本文件。 函数get()有3种主要形式: 1、int
阅读全文
摘要:/* 2、用istream::read()成员函数读文件istream & read(char * buffer,int nCount); 该成员函数从文件中读取nCount个字节的内容,存放到buffer所指向的内存缓冲区中,返回值是对函数所作用的对象引用。 3、用ostream::gcount(
阅读全文
摘要:#include<iostream>#include<fstream>using namespace std;class CStudent{ public: char id[11]; char name[21]; int score;};int main(){ char ch; cout<<"确定需
阅读全文
摘要:/* 读写二进制文件: 二进制数据文件以基本类型数据的二进制形式存放,即二进制文件中数据的存储格式与内存格式一致,存储长度仅与数据类型相关。(便于高速处理)C++用binary方式打开二进制文件,调用ifstream或fstream的read()成员函数从文件中读取数据,调用ofstream或fst
阅读全文
摘要:#include<iostream>#include<fstream>#include<cstdlib>using namespace std;const int MAX_NUM=1000;class CStudent{public: char id[11]; char name[21]; int
阅读全文
摘要:#include<iostream>#include<fstream>#include<iomanip>using namespace std;int main(){ char ch,filename[20]; int count=0; bool newline=true; cout<<"请输入文件
阅读全文
摘要:#include <iostream>#include <fstream>#include <iomanip>using namespace std;int main(){ char id[11],name[21]; int score; ifstream inFile; inFile.open("
阅读全文
摘要:#include <iostream>#include <fstream>using namespace std;int main(){ char id[11],name[21]; int score; ofstream outFile; outFile.open("score.txt",ios::
阅读全文
摘要:#include <iostream>#include <fstream>using namespace std;int main(){ ifstream inFile("c:\\tmp\\test.txt",ios::in); //声明对象inFile并调用构造函数 if(inFile) { co
阅读全文
摘要:/* 打开文件目的:1)建立关联。通过指定文件名,建立起文件和文件流对象的关联,以后在对文件进行操作时,就可以通过与之关联的流对象来进行操作。2)指明文件的使用方式和文件格式。使用方式有只读、只写、既读又写、在文件末尾追加数据4种。 文件格式是文本方式或二进制方式。 打开文件的方式有以下两种:1)先
阅读全文