C++中常见问题总结.docVIP

  • 8
  • 0
  • 约3.78千字
  • 约 5页
  • 2015-09-05 发布于重庆
  • 举报
C中常见问题总结,结构设计常见问题总结,rtx常见问题总结,网络编程常见问题总结,外企面试中常见问题,销售中客户常见问题,面试中常见的问题,工作中常见的沟通问题,高中生常见心理问题,销售中常见的问题

1 关于C++中标准IO库中流的概念(istringstream,ifstream,istream等) istream,ostream用于控制窗口的输入输出,需要头文件#inclujdeiostream ifstream,ofstream用于将文件转化为一个流对象(IO),实现向对控制台一样简单的输入输出,需要头文件#includefstream istringstream,ostringstream用于将一个字符串(其实是内存中的数据)转化为一个流对象,实现流的输入输出,需要头文件#includesstream 以上代码在使用之前千万别忘了加上命名空间 using namespace std;否则将会出现许多莫名其妙的错误 For example: #includeiostream //控制窗口流头文件 #includefstream //文件流头文件 #includesstream //内存流头文件 #includestring using namespace std;//命名空间不可或缺的一句 //窗口输入输出流 void win_io(istream input){ //parameter needs a win_stream string line; cout 请任意输入一些字符,按回车结束 endl; getline(input,line); cout 从控制窗口读取的内容为: line endl; } //文件输入输出流 void file_io(){ string file_name(./Smile.txt); //文件名,当前目录下名为Smile的txt文件 cout 文件中的内容为: endl; ifstream in_file(file_name.c_str()); //将流与一个文件绑定, //file_name.c_str()是将string类型转化为c风格字符串,这是历史问题,不必过分追究 string line; while(getline(in_file,line)) cout line endl; } //将内存中的内容转化为流对象 void buf_io(){ string buf(hello world); istringstream sstr(buf); string word; while(sstr word) cout word endl; } 2 关于C++中的类(class/struct)有关的问题 class Point{ public: Point(){ this-x = 0; this-y = 0; } //打印点的位置 void print(){ cout x y endl; } //析构函数,不能有返回值,不能有参数,而且一个类只能有一个析构函数,可以有多个构造函数 ~Point(){ cout 析构函数被调用 endl; } private: double x; double y; }; int main(int argc,char *argv[]){ Point pt; pt.print(); return 0; } 1 C++中可以用class,也可用struct定义一个类,他们的区别在于默认的访问方式不同,struct是public,而class是private,其他都是一样的,特别需要强调的是,在C语言中,struct中是不能包括function的,而在C++中时可以的,除此之外,二者没什么差别 2 C++中都必须要有一个构造函数,构造函数而类同名,且没有返回值,如果你自己没有显示写一个构造函数,那么,C++编译器将会为你自动生成一个没有参数的构造函数; 另外,一旦你显示写了一个构造函数(不管有没有参数)编译器将不再为你自动生成一个构造函数 重载函数条件为 函数的参数类型不同 或者是 参数的个数不同(但是带有默认缺省值的函数也是不能构成重载的,不能有二义性),返回值类型不同不能构成重载 如:1 int Point(){}; Void Point(){};//不能构成重载 int print(int a){}; int print(int a,int b =5){};//不能构成重载 4 继承(public,protected,private) Public能在任何情况下访问, Protected只能在类内部和子类中被访问 Private只能在类内

文档评论(0)

1亿VIP精品文档

相关文档