| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> c/c++ >> C++的iostream标准库介绍(3) C++ de iostream biao zhun ku jie shao (3)
 

C++的iostream标准库介绍(3)

2008-06-01 02:07:19 编辑來源:互联网 繁體版 评论
 
 
本文为【C++的iostream标准库介绍(3)】的汉字拼音对照版显示拼音
  CyuyantigonglegeshihuashurushuchudefangfaC++yetongyangdanshiC++dekongzhifu使shiyongqilaigengweijiandanfang便bianzaic++xiayouliangzhongfangfakongzhigeshihuashurushuchu

  .youliuduixiangdechengyuanhanshu

  liruxialiechengxuyichengyuanhanshudefangshikongzhishuchudejingdu:

  //chengxuzuozhe:guanning

  //zhandian:www.cndev-lab.com

  //suoyou稿gaojianyunyoubanquan,ruyaozhuanzai,qingwubiwenmingchuchuhezuozhe

  

  #include <iostream>

  using namespace std;

  

  int main()

  {

   float pi=3.14159f;

   cout<<pi<<endl;

   cout.precision(2);

   cout<<pi<<endl;

   system("pause");

  }

  .使shiyongC++shurushuchukongzhifukongzhifushizaituowenjianiomanip.hzhongdingyideduixiangyuchengyuanhanshuyouyiyangdexiaoguokongzhifububixiangchengyuanhanshuxuenayangdandutiaoyongtakeyizhijiecharuliuzhong使shiyong

  liruxialiechengxuyikongzhifudefangshikongzhishuchudejingdu:

  //chengxuzuozhe:guanning

  //zhandian:www.cndev-lab.com

  //suoyou稿gaojianyunyoubanquan,ruyaozhuanzai,qingwubiwenmingchuchuhezuozhe

  

  #include <iostream>

  #include <iomanip>

  using namespace std;

  

  int main()

  {

   float pi=3.14159f;

   cout<<pi<<endl;

   cout<<setprecision(4);

   cout<<pi<<endl;

   system("pause");

  }

  xiabiaowomenliechuleyixiebijiaochangyongdekongzhifuhaoyouyupianfuyouxianduzheqinggenjuzijidexuyaochayuexiangguanshuji

  duiyuiostreambiaozhunkulaishuobaohanlezhongduodechengyuanhanshugehanshudouyouqizishendezuoyongpianfuwentibizhezaizhelibunengyiyishuominglijuyouyubiaozhunshuruduixiangcintigongshurudeshihouhuizidongyikonggezuoweifenjiegeiwomenhuoquyixingdaiyoukonggedewanzhengzifuchuandailailekunnanzaizhelibuchongyigefeichangyongyoudechengyuanhanshugetline()

  qihanshuyuanxingwei

  getlin(chiar *str,int size,char='\n');

  diyigeshenshushizifushuzuyongyucunfangzhengxingwenbendiergeshenshuduqudezuidazifugeshudisangeshenshuweizuoweifenjiejiexiandezifumoshuxishi\nhuanxingfu

  shilidaimaruxia

  //chengxuzuozhe:guanning

  //zhandian:www.cndev-lab.com

  //suoyou稿gaojianyunyoubanquan,ruyaozhuanzai,qingwubiwenmingchuchuhezuozhe

  

  #include <iostream>

  #include <iomanip>

  using namespace std;

  

  int main()

  {

   char str[100];

   cin.getline(str,sizeof(str),'\n');

   cout<<str<<endl;

   system("pause");

  }

  tongguoshangmianneirongdexuexiwomenduii/oyouleyixiejibendianjibendeshuxixianzaishigaiqieruzhengtideshihoulejutixuexiyixiaruhechongzaizuoyiyuyouyicaozuofu

  xianshuozuoyi<<caozuofuyejiushiwomenchangshuodeshuchucaozuofu

  duiyuzidingyileilaishuochongzaizuoyicaozuofudefangfawomenchang使shiyongleideyouyuanfangshijinxingcaozuo

  shilidaimaruxia

  //chengxuzuozhe:guanning

  //zhandian:www.cndev-lab.com

  //suoyou稿gaojianyunyoubanquan,ruyaozhuanzai,qingwubiwenmingchuchuhezuozhe

  

  #include <iostream>

  using namespace std;

  

  class Test

  

   {

   public:

   Test(int age = 0,char *name = "\0")

   {

   Test::age = age;

   strcpy(Test::name,name);

   }

   void outmembers(ostream &out)

   {

   out<<"Age"<<age<<endl<<"Name"<<this->name<<endl;

   }

   friend ostream& operator <<(ostream& ,Test&);

   protected:

   int age;

   char name[50];

  };

  ostream& operator <<(ostream& out,Test &temp)

  {

   temp.outmembers(out);

   return out;

  }

  int main()

  {

   Test a(24,"guanning");

   cout<<a;

   system("pause");

  }

  shanglidaimazhongwomenduivoid outmembers(ostream &out)deshenshu使shiyongostreamdingyizhuyaoshiweilekeyixiangtachuandirenheostreamleiduixiangbuguangshicoutyekeyishiofstremhuozheshiostrstreamheostringstreamleiduixiangzuodaotongyongxing

  chongzaiyunsuanfuwomenzhidaokeyishifeichengyuanfangshiyekeyishichengyuanfangshideduiyu<<laishuotongyangyekeyishichengyuanfangshidanwoshifenbutuijianzhemezuoyinweiduiyuleidechengyuanhanshulaishuodiyigeshenshushizhongshihuibeiyincangdeerqieyidingshidangqianleileixingde

  xiamiandeshilidaimajiushijiangshangmiande<<chongzaihanshuxiugaichengchengyuanfangshidezuofa

  //chengxuzuozhe:guanning

  //zhandian:www.cndev-lab.com

  //suoyou稿gaojianyunyoubanquan,ruyaozhuanzai,qingwubiwenmingchuchuhezuozhe

  

  #include <iostream>

  using namespace std;

  

  class Test

  {

   public:

   Test(int age = 0,char *name = "\0")

   {

   Test::age = age;

   strcpy(Test::name,name);

   }

   void outmembers(ostream &out)

   {

   out<<"Age"<<age<<endl<<"Name"<<this->name<<endl;

  

   }

   ostream& operator <<(ostream &out)

   {

   this->outmembers(out);

   return out;

   }

   protected:

   int age;

   char name[50];

  };

  int main()

  {

   Test a(24,"guanning");

   a<<cout;

   system("pause");

  }

  congdaimashixianshangwomenjianghanshuxiugaichengleostream& operator <<(ostream &out)pobudeyijiangostreamleixingdeyinyongshenshufangdaolehoumianzheshiyinweichengyuanfangshiyunsuanfuchongzaihanshudiyigeshenshuhuibeiyincangerqieyidingshidangqianleileixingdezheheostreamleixingchongtuleyouciwomenzai使shiyongcoutshuchudeshihoujiubixuxiechenga<<cout;zheyangyilaidaimadekeduxingjiudadajiangdilezhedaodishizuoyihuanshiyouyineweiciwozaiyicishuomingduiyuzuoyiheyouyiyunsuanfudechongzaishishifenbutuijian使shiyongchengyuanhanshudefangshibianxiede

  weilegongguxuexixiamianwomenyifstreamduixiangshuchuweilizuoyigelianxi

  daimaruxia

  //chengxuzuozhe:guanning

  //zhandian:www.cndev-lab.com

  //suoyou稿gaojianyunyoubanquan,ruyaozhuanzai,qingwubiwenmingchuchuhezuozhe

  #include <iostream>

  #include <fstream>

  using namespace std;

  

  class Test

  {

   public:

   Test(int age = 0,char *name = "\0")

   {

   Test::age = age;

   strcpy(Test::name,name);

   }

   void outmembers(ostream &out)

   {

   out<<"Age"<<age<<endl<<"Name"<<this->name<<endl;

   }

   friend ostream& operator <<(ostream& ,Test&);

   protected:

   int age;

   char name[50];

  };

  

   ostream& operator <<(ostream& out,Test &temp)

  {

   temp.outmembers(out);

   return out;

  }

  int main()

  {

   Test a(24,"guanning");

   ofstream myfile("c:\\1.txt",ios::out,0);

   if (myfile.rdstate() == ios_base::goodbit)

   {

   myfile<<a;

   cout<<"wenjianchuangjianchenggongxieruzhengchang"<<endl;

   }

   if (myfile.rdstate() == ios_base::badbit)

   {

   cout<<"wenjianchuangjianshibaicipancuowu"<<endl;

   }

   system("pause");

  }

  duiyuzuoyiyunsuanfuchongzaihanshulaishuoyouyubutuijian使shiyongchengyuanfangshiname使shiyongfeichengyuanfangshizaileiyouduochongjixudeqingkuangxiajiubuneng使shiyongxuhanshujinxingzuoyiyunsuanfuchongzaidequfenweiledadaonenggouqufenxianshidemudegeimeigeleifenbietianjiabutongdexuhanshushibiyaode

  shilidaimaruxia

  //chengxuzuozhe:guanning

  //zhandian:www.cndev-lab.com

  //suoyou稿gaojianyunyoubanquan,ruyaozhuanzai,qingwubiwenmingchuchuhezuozhe

  

  #include <iostream>

  #include <fstream>

  using namespace std;

  

  class Student

  {

   public:

   Student(int age = 0,char *name = "\0")

   {

   Student::age = age;

   strcpy(Student::name,name);

   }

   virtual void outmembers(ostream &out) = 0;

   friend ostream& operator <<(ostream& ,Student&);

   protected:

   int age;

   char name[50];

  };

  ostream& operator <<(ostream& out,Student &temp)

  {

  

   temp.outmembers(out);

   return out;

  }

  class Academician:public Student

  {

   public:

   Academician(int age = 0,char *name = "\0",char *speciality="\0"):Student(age,name)

   {

   strcpy(Academician::speciality,speciality);

   }

   virtual void outmembers(ostream &out)

   {

   out<<"Age"<<age<<endl<<"Name"<<name<<endl<<

   "speciality"<<speciality<<endl;

   }

   protected:

   char speciality[80];

  };

  class GraduateStudent:public Academician

  {

   public:

   GraduateStudent(int age = 0,char *name = "\0",char *speciality="\0",

   char *investigate="\0"):Academician(age,name,speciality)

   {

   strcpy(GraduateStudent::investigate,investigate);

   }

   virtual void outmembers(ostream &out)

   {

   out<<"Age"<<age<<endl<<"Name"<<name<<endl<<

   "speciality"<<speciality<<endl<<"investigate"<<investigate<<endl;

   }

   protected:

   char investigate[100];

  };

  int main()

  {

   Academician a(24,"guanning","Computer Science");

   cout<<a;

   GraduateStudent b(24,"yanyanling","Computer Science","GIS System");

  

   cout<<b;

   system("pause");

  }

  zaishangmiandedaimazhongweilenenggouqufenshuchuaduixiangyubduixiangwomenyongxuhanshudefangshichongzailejixuleiAcademicianyuduochongjixuleiGraduateStudentdeoutmemberschengyuanhanshuyouyuostream& operator <<(ostream& out,Student &temp) yunsuanfuchongzaihanshushiStudentjileideStudent &tempshenshutongguoxuhanshudedingyikeyishiyingbutongpaishengleiduixiangsuoyizaiqineibutiaoyongtemp.outmembers(out); xitongkeshibiebutongjileideoutmembers()chengyuanhanshu

  zuihoukanyixiayouyiyunsuanfudechongzaiyouyiyunsuanfuwomenyechangjiaotashuruyunsuanfuhaoduiyutalaishuojutishixianhezuoyiyunsuanfudechongzaichabiebingbudaduiyuyouduochengyuanduixiangdeleilaishuozhiyaobaozhengnenggouwanzhengshurugechengyuanduixiangdashujujiukeyile

  shiliruxia

  //chengxuzuozhe:guanning

  //zhandian:www.cndev-lab.com

  //suoyou稿gaojianyunyoubanquan,ruyaozhuanzai,qingwubiwenmingchuchuhezuozhe

  

  #include <iostream>

  using namespace std;

  

  class Test

  {

   public:

   Test(int age = 0,char *name = "\0")

   {

   Test::age = age;

   strcpy(Test::name,name);

   }

   void inputmembers(istream &out)

   {

   cout<<"please input age";

   cin>>Test::age;

   cout<<"please input name";

   cin>>Test::name;

   }

   friend istream& operator >>(istream& ,Test&);

   public:

   int age;

   char name[50];

  };

  istream& operator >>(istream& input,Test &temp)

  {

   temp.inputmembers(input);

   return input;

  }

  int main()

  {

   Test a;

   cin>>a;

  

   cout<<a.age<<""<<a.name<<endl;

   system("pause");

  }原文】【拼音
 
 
 
上一篇《高质量C++/C编程指南(七)》
下一篇《C++运算符重载转换运算符》
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
  C语言提供了格式化输入输出的方法,C++也同样,但是C++的控制符使用起来更为简单方便,在c++下有两中方法控制格式化输入输出。   1.有流对象的成员函数。  例如,下列程序以成员函数的方式控制输出的精度: //程序作者:管宁 //站点:www.cndev-lab.com //所有稿件均有版权,如要转载,请务必闻名出处和作者 #include <iostream> using namespace std; int main() { float pi=3.14159f; cout<<pi<<endl; cout.precision(2); cout<<pi<<endl; system("pause"); }   2.使用C++输入输出控制符,控制符是在拖文件iomanip.h中定义的对象,与成员函数有一样的效果,控制符不必像成员函数学那样单独调用,它可以直接插入流中使用。   例如,下列程序以控制符的方式控制输出的精度: //程序作者:管宁 //站点:www.cndev-lab.com //所有稿件均有版权,如要转载,请务必闻名出处和作者 #include <iostream> #include <iomanip> using namespace std; int main() { float pi=3.14159f; cout<<pi<<endl; cout<<setprecision(4); cout<<pi<<endl; system("pause"); }   下表我们列出了一些比较常用的控制符号,由于篇幅有限读者请根据自己的需要查阅相关书籍:   对于iostream标准库来说包含了众多的成员函数,各函数都有其自身的作用,篇幅问题笔者在这里不能一一说明例举,由于标准输入对象cin提供输入的时候会自动以空格作为分界,给我们获取一行带有空格的完整字符串带来了困难,在这里补充一个非常用有的成员函数----getline()。   其函数原型为:   getlin(chiar *str,int size,char='\n');   第一个参数是字符数组,用于存放整行文本,第二个参数读取的最大字符个数,第三个参数为作为分界界限的字符,默熟悉是\n,换行符。   示例代码如下: //程序作者:管宁 //站点:www.cndev-lab.com //所有稿件均有版权,如要转载,请务必闻名出处和作者 #include <iostream> #include <iomanip> using namespace std; int main() { char str[100]; cin.getline(str,sizeof(str),'\n'); cout<<str<<endl; system("pause"); }   通过上面内容的学习,我们对i/o有了一些基本点基本的熟悉,现在是该切入正题的时候了,具体学习一下,如何重载左移与右移操作符。   先说左移(<<)操作符,也就是我们常说的输出操作符。   对于自定义类来说,重载左移操作符的方法我们常使用类的友元方式进行操作。   示例代码如下: //程序作者:管宁 //站点:www.cndev-lab.com //所有稿件均有版权,如要转载,请务必闻名出处和作者 #include <iostream> using namespace std; class Test { public: Test(int age = 0,char *name = "\0") { Test::age = age; strcpy(Test::name,name); } void outmembers(ostream &out) { out<<"Age:"<<age<<endl<<"Name:"<<this->name<<endl; } friend ostream& operator <<(ostream& ,Test&); protected: int age; char name[50]; }; ostream& operator <<(ostream& out,Test &temp) { temp.outmembers(out); return out; } int main() { Test a(24,"管宁"); cout<<a; system("pause"); }   上例代码中,我们对void outmembers(ostream &out)的参数使用ostream定义主要是为了可以向它传递任何ostream类对象不光是cout也可以是ofstrem或者是ostrstream和ostringstream类对象,做到通用性。   重载运算符,我们知道可以是非成员方式也可以是成员方式的,对于<<来说同样也可以是成员方式,但我十分不推荐这么做,因为对于类的成员函数来说,第一个参数始终是会被隐藏的,而且一定是当前类类型的。   下面的示例代码就是将上面的<<重载函数修改成成员方式的做法: //程序作者:管宁 //站点:www.cndev-lab.com //所有稿件均有版权,如要转载,请务必闻名出处和作者 #include <iostream> using namespace std; class Test { public: Test(int age = 0,char *name = "\0") { Test::age = age; strcpy(Test::name,name); } void outmembers(ostream &out) { out<<"Age:"<<age<<endl<<"Name:"<<this->name<<endl; } ostream& operator <<(ostream &out) { this->outmembers(out); return out; } protected: int age; char name[50]; }; int main() { Test a(24,"管宁"); a<<cout; system("pause"); }   从代码实现上,我们将函数修改成了ostream& operator <<(ostream &out),迫不得已将ostream类型的引用参数放到了后面,这是因为,成员方式运算符重载函数第一个参数会被隐藏,而且一定是当前类类型的,这和ostream类型冲突了。由此我们在使用cout输出的时候就必须写成a<<cout;,这样一来代码的可读行就大大降低了,这到底是左移还是右移呢?为此我再一次说明,对于左移和右移运算符的重载是十分不推荐使用成员函数的方式编写的。   为了巩固学习,下面我们以fstream对象输出为例做一个练习。   代码如下: //程序作者:管宁 //站点:www.cndev-lab.com //所有稿件均有版权,如要转载,请务必闻名出处和作者 #include <iostream> #include <fstream> using namespace std; class Test { public: Test(int age = 0,char *name = "\0") { Test::age = age; strcpy(Test::name,name); } void outmembers(ostream &out) { out<<"Age:"<<age<<endl<<"Name:"<<this->name<<endl; } friend ostream& operator <<(ostream& ,Test&); protected: int age; char name[50]; }; ostream& operator <<(ostream& out,Test &temp) { temp.outmembers(out); return out; } int main() { Test a(24,"管宁"); ofstream myfile("c:\\1.txt",ios::out,0); if (myfile.rdstate() == ios_base::goodbit) { myfile<<a; cout<<"文件创建成功,写入正常!"<<endl; } if (myfile.rdstate() == ios_base::badbit) { cout<<"文件创建失败,磁盘错误!"<<endl; } system("pause"); }   对于左移运算符重载函数来说,由于不推荐使用成员方式,那么使用非成员方式在类有多重继续的情况下,就不能使用虚函数进行左移运算符重载的区分,为了达到能够区分显示的目的,给每个类分别添加不同的虚函数是必要的。   示例代码如下: //程序作者:管宁 //站点:www.cndev-lab.com //所有稿件均有版权,如要转载,请务必闻名出处和作者 #include <iostream> #include <fstream> using namespace std; class Student { public: Student(int age = 0,char *name = "\0") { Student::age = age; strcpy(Student::name,name); } virtual void outmembers(ostream &out) = 0; friend ostream& operator <<(ostream& ,Student&); protected: int age; char name[50]; }; ostream& operator <<(ostream& out,Student &temp) { temp.outmembers(out); return out; } class Academician:public Student { public: Academician(int age = 0,char *name = "\0",char *speciality="\0"):Student(age,name) { strcpy(Academician::speciality,speciality); } virtual void outmembers(ostream &out) { out<<"Age:"<<age<<endl<<"Name:"<<name<<endl<< "speciality:"<<speciality<<endl; } protected: char speciality[80]; }; class GraduateStudent:public Academician { public: GraduateStudent(int age = 0,char *name = "\0",char *speciality="\0", char *investigate="\0"):Academician(age,name,speciality) { strcpy(GraduateStudent::investigate,investigate); } virtual void outmembers(ostream &out) { out<<"Age:"<<age<<endl<<"Name:"<<name<<endl<< "speciality:"<<speciality<<endl<<"investigate:"<<investigate<<endl; } protected: char investigate[100]; }; int main() { Academician a(24,"管宁","Computer Science"); cout<<a; GraduateStudent b(24,"严燕玲","Computer Science","GIS System"); cout<<b; system("pause"); }   在上面的代码中为了能够区分输出a对象与b对象,我们用虚函数的方式重载了继续类Academician与多重继续类GraduateStudent的outmembers成员函数,由于ostream& operator <<(ostream& out,Student &temp) 运算符重载函数是Student基类的,Student &temp参数通过虚函数的定义可以适应不同派生类对象,所以在其内部调用temp.outmembers(out); 系统可识别不同继类的outmembers()成员函数。   最后看一下,右移运算符的重载,右移运算符我们也常叫它输入运算符号,对于它来说,具体实现和左移运算符的重载差别并不大,对于有多成员对象的类来说,只要保证能够完整输入各成员对象大数据就可以了。   示例如下: //程序作者:管宁 //站点:www.cndev-lab.com //所有稿件均有版权,如要转载,请务必闻名出处和作者 #include <iostream> using namespace std; class Test { public: Test(int age = 0,char *name = "\0") { Test::age = age; strcpy(Test::name,name); } void inputmembers(istream &out) { cout<<"please input age:"; cin>>Test::age; cout<<"please input name:"; cin>>Test::name; } friend istream& operator >>(istream& ,Test&); public: int age; char name[50]; }; istream& operator >>(istream& input,Test &temp) { temp.inputmembers(input); return input; } int main() { Test a; cin>>a; cout<<a.age<<""<<a.name<<endl; system("pause"); }
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
桌球宝贝_性感在蔓延(3)
桌球宝贝_性感在蔓延(2)
银杏树下清纯女孩(9)
银杏树下清纯女孩(8)
痞子的甘南日记
疑是银河落九天
雪域坝上四——纯美色
冬日恋歌——西城杨柳弄轻柔
 
>>返回首页<<
 
 
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
©2005- 王朝网络 版权所有