| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> c/c++ >> xiu lian 8 nian C++ mian xiang dui xiang cheng xu she ji zhi ti hui

xiu lian 8 nian C++ mian xiang dui xiang cheng xu she ji zhi ti hui

2008-06-01 02:09:19 编辑來源:互联网 评论
 
 
 
本文为【修练8年C++面向对象程序设计之体会】的拼音翻译版
  liu nian qian , wo gang re lian “ mian xiang dui xiang ”(Object-Oriented) shi , yi kou qi ji zhu le jin shi ge ding yi 。 liu nian hou , wo cong ji shi wan xing cheng xu zhong gun pa chu lai yu bei xie dian xin de ti hui shi , que wu fa jie shi shen me shi “ mian xiang dui xiang ”, jiu xiang shuo bu qing chu shen me shi shu xue na yang 。 ruan jian gong cheng zhong de shi mao shu yu “ mian xiang dui xiang fen xi ” he “ mian xiang dui xiang she ji ”, tong chang shi zhen dui “ xu qiu fen xi ” he “ xi tong she ji ” huan jie de 。“ mian xiang dui xiang ” you ji da xue pai , jiu xiang ru lai fo 、 shang di he zhen zhu yong ge zi de fang shi ding yi le zhe ge shi jie , bing liu xia yi dui jing shu lai jie shi zhe ge shi jie 。
  you xie xue zhe jian yi zhe yang zhao “ dui xiang ”: fen xi yi ge ju zi de yu fa , zhao chu ming ci he dong ci , ming ci jiu shi dui xiang , dong ci ze shi dui xiang de fang fa ( ji han shu )。
  dang nian guo min dang de wen ren wei le dui kang mao ze dong de 《 qin yuan chun · xue 》, te yi qing qing chao yi lao men xie le yi xie dui zhang gong zheng de shi , qing jiang jie shi guo mu 。 lao jiang kan le qi de da ma :“ niang xi pi , quan dou you yi gu guan cai li fu shi de qi wei 。” wo kan le ji qian ye de ruan jian gong cheng zi liao , zhong yu fa xian zi ji you xie “ ruo zhi ”, wu fa li jie “ mian xiang dui xiang ” de li lun , tong shi xing wu dao “ bian cheng shi ying dao li 。”
  mian xiang dui xiang cheng xu she ji yu yan hen duo , ru Smalltalk、Ada、Eiffel、Object Pascal、Visual Basic、C++ deng deng 。C++ yu yan zui tao ren xi huan , yin wei ta jian rong C yu yan , bing qie ju bei C yu yan de xing neng 。 jin ji nian , yi zhong jiao java de chun mian xiang dui xiang yu yan hong ji yi shi , bu shao ren jiao han zhe yao yong Java ge C++ de ming 。 wo ren wei Java hao bi shi C++ de wai sheng , sui ran bu shi zhi jie yi chuan de , dan ye ji fen xiang yang 。 wai sheng zai jiu jiu shen shang wan shua shi sa le yi pao niao , liang ren bu gai wei ci er zheng chao 。
  guan yu C++ cheng xu she ji de shu ji fei chang duo , ben zhang bu jiang C++ de yu fa , zhi jiang yi xie xiao xiao de bian cheng dao li 。 jia ru wo neng zao ji nian ming bai zhe xie xiao dao li , jiu ke yi da da gai shan shu shi wan xing cheng xu de zhi liang le 。
  1. C++ mian xiang dui xiang cheng xu she ji de chong yao gai nian
  zao qi ge ming ying pian li you zhe yang yi ge jiao se , ta shuo :“ wo shi dang dai biao , wo dai biao dang , wo jiu shi dang 。” hou lai ta gei tong zhi men dai lai le zai nan 。
  hui yong C++ de cheng xu yuan yi ding dong de mian xiang dui xiang cheng xu she ji ma ?
  bu hui yong C++ de cheng xu yuan yi ding bu dong de mian xiang dui xiang cheng xu she ji ma ?
  liang zhe dou wei bi 。 jiu xiang huai dan ru dang hou wei bi neng cheng wei hao ren , hao ren bu ru dang wei bi bian cheng huai dan na yang 。
  wo bu pa chu fan zhong nu di shuo ju da hua :“C++ mei you gao shou ,C yu yan cai you gao shou 。” zai yong C he C++ bian cheng 8 nian zhi hou , wo shen shen di yi han zi ji bu shi C yu yan de gao shou , geng yi han mei you ren dian bo wo ru he jin xing mian xiang dui xiang cheng xu she ji 。 wo he hen duo C++ cheng xu yuan yi yang , zai xiang yong dao C++ yu fa de hao chu shi bian yi wei zi ji yi jing ming bai le mian xiang dui xiang cheng xu she ji 。 jiu xiang ji diao ya gao mai ya gao pi na yang , zhen shi bao tian tian wu ya 。
  ren men bu dong pin yin ye hui jiang pu tong hua , jia ru dong de pin yin ze hui ba pu tong hua jiang de geng hao 。 bu dong mian xiang dui xiang cheng xu she ji ye ke yi yong C++ bian cheng , jia ru dong de mian xiang dui xiang cheng xu she ji ze hui ba C++ cheng xu bian de geng hao 。 ben jie jiang shu san ge fei chang ji chu de gai nian :“ lei yu dui xiang ”、“ ji xu yu zu he ”、“ xu han shu yu duo tai ”。 li jie zhe xie gai nian , you zhu yu ti gao cheng xu de zhi liang , fei fan shi ti gao “ ke fu yong xing ” yu “ ke kuo chong xing ”。
  1.1 lei yu dui xiang
  dui xiang (Object) shi lei (Class) de yi ge shi li (Instance)。 jia ru jiang dui xiang bi zuo fang zi , na me lei jiu shi fang zi de she ji tu zhi 。 suo yi mian xiang dui xiang cheng xu she ji de chong dian shi lei de she ji , er bu shi dui xiang de she ji 。 lei ke yi jiang shu ju he han shu feng zhuang zai yi qi , qi zhong han shu biao shi le lei de xing wei ( huo cheng fu wu )。 lei ti gong yao hai zi public、PRotected he private yong yu sheng ming na xie shu ju he han shu shi gong you de 、 shou bao hu de huo zhe shi si you de 。
  zhe yang ke yi da dao xin xi yin cang de mu de , ji rang lei jin jin gong kai bi xu yao rang wai jie zhi dao de nei rong , er yin cang qi ta yi qie nei rong 。 wo men bu ke yi lan yong lei de feng zhuang gong neng , bu yao ba ta dang cheng huo guo , shen me dong xi dou wang li reng 。
  lei de she ji shi yi shu ju wei zhong xin , huan shi yi xing wei wei zhong xin ?
  zhu zhang “ yi shu ju wei zhong xin ” de na yi pai ren guan zhu lei de nei bu shu ju jie gou , ta men xi guan shang jiang private lei xing de shu ju xie zai qian mian , er jiang public lei xing de han shu xie zai hou mian , ru biao 8.1(a) suo shi 。
  zhu zhang “ yi xing wei wei zhong xin ” de na yi pai ren guan zhu lei ying gai ti gong shen me yang de fu wu he jie kou , ta men xi guan shang jiang public lei xing de han shu xie zai qian mian , er jiang private lei xing de shu ju xie zai hou mian , ru biao 8.1(b) suo shi 。
修练8年C++面向对象程序设计之体会

  hen duo C++ jiao ke shu zhu zhang zai she ji lei shi “ yi shu ju wei zhong xin ”。 wo jian chi bing qie jian yi du zhe zai she ji lei shi “ yi xing wei wei zhong xin ”, ji shou xian kao lv lei ying gai ti gong shen me yang de han shu 。Microsoft gong si de COM gui fan de gai xin shi jie kou she ji ,COM de jie kou jiu xiang dang yu lei de gong you han shu [Rogerson 1999]。 zai cheng xu she ji fang mian , za men bu yao fu yi Microsoft gong si de feng ge 。
  she ji gu li de lei shi bi jiao qing yi de , nan de shi zheng que she ji ji lei ji qi pai sheng lei 。 yin wei you xie cheng xu yuan gao bu qing chu “ ji xu ”(Inheritance)、“ zu he ”(Composition)、“ duo tai ”( Polymorphism) zhe xie gai nian 。
修练8年C++面向对象程序设计之体会
geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo
  1.2 ji xu yu zu he
  jia ru A shi ji lei ,B shi A de pai sheng lei , na me B jiang ji xu A de shu ju he han shu 。 shi li cheng xu ru xia :
  
  class A
  
   {
  public:
  void Func1(void);
  void Func2(void);
  };
  class B : public A
  {
  public:
  void Func3(void);
  void Func4(void);
  };
  // Example
  main()
  {
  B b; // B de yi ge dui xiang
  b.Func1(); // B cong A ji xu le han shu Func1
  b.Func2(); // B cong A ji xu le han shu Func2
  b.Func3();
  b.Func4();
  }
  zhe ge jian dan de shi li cheng xu shuo ming le yi ge shi shi :C++ de “ ji xu ” te xing ke yi ti gao cheng xu de ke fu yong xing 。 zheng yin wei “ ji xu ” tai you yong 、 tai qing yi yong , cai yao fang zhi luan yong “ ji xu ”。 wo men yao gei “ ji xu ” li yi xie shi yong gui ze :
  yi 、 jia ru lei A he lei B hao bu xiang guan , bu ke yi wei le shi B de gong neng geng duo xie er rang B ji xu A de gong neng 。
  bu yao jiao de “ bu chi bai bu chi ”, rang yi ge hao duan duan de jian zhuang qing nian wu yuan wu gu di chi ren shen bu shen ti 。
  er 、 jia ru lei B you bi yao shi yong A de gong neng , ze yao fen liang zhong qing kuang kao lv :
  (1) ruo zai luo ji shang B shi A de “ yi zhong ”(a kind of ), ze da ying B ji xu A de gong neng 。 ru nan ren (Man) shi ren (Human) de yi zhong , nan hai (Boy) shi nan ren de yi zhong 。 na me lei Man ke yi cong lei Human pai sheng , lei Boy ke yi cong lei Man pai sheng 。 shi li cheng xu ru xia :
  class Human
  {
  …
  };
  class Man : public Human
  {
  …
  };
  class Boy : public Man
  {
  …
  };
  (2) ruo zai luo ji shang A shi B de “ yi bu fen ”(a part of), ze bu da ying B ji xu A de gong neng , er shi yao yong A he qi ta dong xi zu he chu B。 li ru yan (Eye)、 bi (Nose)、 kou (Mouth)、 er (Ear) shi tou (Head) de yi bu fen , suo yi lei Head ying gai you lei Eye、Nose、Mouth、Ear zu he er cheng , bu shi pai sheng er cheng 。 shi li cheng xu ru xia :
  class Eye
  {
  public:
  void Look(void);
  };
  class Nose
  {
  public:
  void Smell(void);
  };
  class Mouth
  {
  public:
  void Eat(void);
  };
  class Ear
  {
  public:
  void Listen(void);
  };
  // zheng que de she ji , rong chang de cheng xu
  class Head
  {
  public:
  void Look(void) { m_eye.Look(); }
  void Smell(void) { m_nose.Smell(); }
  void Eat(void) { m_mouth.Eat(); }
  void Listen(void) { m_ear.Listen(); }
  private:
  Eye m_eye;
  Nose m_nose;
  Mouth m_mouth;
  Ear m_ear;
  };
  jia ru da ying Head cong Eye、Nose、Mouth、Ear pai sheng er cheng , na me Head jiang zi dong ju you Look、Smell、Eat、Listen zhe xie gong neng :
  // cuo wu de she ji
  class Head : public Eye, public Nose, public Mouth, public Ear
  {
  };
  shang shu cheng xu shi fen jian duan bing qie yun xing zheng que , dan shi zhe zhong she ji que shi cuo wu de 。 hen duo cheng xu yuan jing bu qi “ ji xu ” de you huo er fan xia she ji cuo wu 。
  yi zhi gong ji shi jing di zhui da yi zhi gang xia le dan de mu ji , ni zhi dao wei shen me ma ?
  yin wei mu ji xia le ya dan 。
  ben shu 3.3 jie jiang guo “ yun xing zheng que ” de cheng xu bu jian de jiu shi gao zhi liang de cheng xu , ci chu jiu shi yi ge li zheng 。
修练8年C++面向对象程序设计之体会
geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo
  1.3 xu han shu yu duo tai
  chu le ji xu wai ,C++ de ling yi ge you liang te xing shi zhi chi duo tai , ji da ying jiang pai sheng lei de dui xiang dang zuo ji lei de dui xiang shi yong 。 jia ru A shi ji lei ,B he C shi A de pai sheng lei , duo tai han shu Test de shen shu shi A de zhi zhen 。 na me Test han shu ke yi yin yong A、B、C de dui xiang 。 shi li cheng xu ru xia :
  
  class A
  {
  public:
  void Func1(void);
  };
  void Test(A *a)
  {
  a->Func1();
  }
  class B : public A
  {
  …
  };
  class C : public A
  {
  …
  };
  // Example
  main()
  {
  A a;
  B b;
  C c;
  Test(&a);
  Test(&b);
  Test(&c);
  };
  yi shang cheng xu kan bu chu “ duo tai ” you shen me jia zhi , jia shang xu han shu he chou xiang ji lei hou ,“ duo tai ” de wei li jiu xian shi chu lai le 。
  C++ yong yao hai zi virtual lai sheng ming yi ge han shu wei xu han shu , pai sheng lei de xu han shu jiang (override) ji lei dui ying de xu han shu de gong neng 。 shi li cheng xu ru xia :
  class A
  {
  public:
  virtual void Func1(void){ cout<< “This is A::Func1 \n”}
  };
  void Test(A *a)
  {
  a->Func1();
  }
  class B : public A
  
   {
  public:
  virtual void Func1(void){ cout<< “This is B::Func1 \n”}
  };
  class C : public A
  {
  public:
  virtual void Func1(void){ cout<< “This is C::Func1 \n”}
  };
  // Example
  main()
  {
  A a;
  B b;
  C c;
  Test(&a); // shu chu This is A::Func1
  Test(&b); // shu chu This is B::Func1
  Test(&c); // shu chu This is C::Func1
  };
  jia ru ji lei A ding yi ru xia :
  class A
  {
  public:
  virtual void Func1(void)=0;
  };
  na me han shu Func1 jiao zuo chun xu han shu , han you chun xu han shu de lei jiao zuo chou xiang ji lei 。 chou xiang ji lei zhi guan ding yi chun xu han shu de xing shi , ju ti de gong neng you pai sheng lei shi xian 。
  jie he “ chou xiang ji lei ” he “ duo tai ” you ru xia tu chu you dian :
  (1) ying yong cheng xu bu bi wei mei yi ge pai sheng lei bian xie gong neng tiao yong , zhi xu yao dui chou xiang ji lei jin xing chu li ji ke 。 zhe yi
  zhao jiao “ yi bu bian ying wan bian ”, ke yi da da ti gao cheng xu de ke fu yong xing ( zhe shi jie kou she ji de fu yong , er bu shi dai ma shi xian de fu yong )。
  (2) pai sheng lei de gong neng ke yi bei ji lei zhi zhen yin yong , zhe jiao xiang hou jian rong , ke yi ti gao cheng xu de ke kuo chong xing he ke wei hu xing 。 yi qian xie de cheng xu ke yi bei jiang lai xie de cheng xu tiao yong bu zu wei qi , dan shi jiang lai xie de cheng xu ke yi bei yi qian xie de cheng xu tiao yong na ke le bu qi 。
修练8年C++面向对象程序设计之体会
geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo
  2 liang hao de bian cheng feng ge
  nei gong shen hou de wu lin gao shou chu zhao wang wang ping dan wu qi 。 tong li , bian cheng gao shou ye bu hui yong qi men guai zhao xie cheng xu 。 liang hao de bian cheng feng ge shi chan sheng gao zhi liang cheng xu de qian ti 。
  2.1 ming ming yue ding
  
  you bu shao ren bian cheng shi yong pin yin gei han shu huo bian liang ming ming , zhe yang zuo bing bu neng shuo ming ni hen ai guo , que hui rang yong ci cheng xu de ren mi hu ( hen duo nan fang ren bu dong pin yin , wo jiu bu dong )。 cheng xu zhong de ying wen yi ban bu hui tai fu duo , yong ci yao li qiu zhun que 。 xiong ya li ming ming fa shi Microsoft gong si chang dao de [Maguire 1993], sui ran hen fan suo , dan yong xi guan le ye jiu cheng le zi ran 。 mei you ren qiang po ni cai yong he zhong ming ming fa , dan you yi dian ying gai zuo dao : zi ji de cheng xu ming ming bi xu yi zhi 。
  yi xia shi wo bian cheng shi cai yong de ming ming yue ding :
  (1) hong ding yi yong da xie zi mu jia xia hua xian biao shi , ru MAX_LENGTH;
  (2) han shu yong da xie zi mu kai tou de dan ci zu he er cheng , ru SetName, GetName ;
  (3) zhi zhen bian liang jia qian zhui p, ru *pNode ;
  (4)BOOL bian liang jia qian zhui b, ru bFlag ;
  (5)int bian liang jia qian zhui i, ru iWidth ;
  (6)float bian liang jia qian zhui f, ru fWidth ;
  (7)double bian liang jia qian zhui d, ru dWidth ;
  (8) zi fu chuan bian liang jia qian zhui str, ru strName ;
  (9) mei ju bian liang jia qian zhui e, ru eDrawMode ;
  (10) lei de cheng yuan bian liang jia qian zhui m_, ru m_strName, m_iWidth ;
  dui yu int, float, double xing de bian liang , jia ru bian liang ming de han yi shi fen ming xian , ze bu jia qian zhui , bi mian fan suo 。 ru yong yu xun huan de int xing bian liang i,j,k ;float xing de san wei zuo biao (x,y,z) deng 。
  2.2 shi yong duan yan
  cheng xu yi ban fen wei Debug ban ben he Release ban ben ,Debug ban ben yong yu nei bu tiao shi ,Release ban ben fa xing gei yong hu shi yong 。 duan yan assert shi jin zai Debug ban ben qi zuo yong de hong , ta yong yu jian cha “ bu ying gai ” fa sheng de qing kuang 。 yi xia shi yi ge nei cun fu zhi cheng xu , zai yun xing guo cheng zhong , jia ru assert de shen shu wei jia , na me cheng xu jiu hui zhong zhi ( yi ban di huan hui chu xian ti shi dui hua , shuo ming zai shen me di fang yin fa le assert)。
  // fu zhi bu chong die de nei cun kuai
  void memcpy(void *pvTo, void *pvFrom, size_t size)
  {
  void *pBTo = (byte *) pvTo;
  void *pbFrom = (byte *) pvFrom;
  assert( pvTo != NULL && pvFrom != NULL );
  while(size - - > 0 )
  *pbTo + + = *pbFrom + + ;
  return (pvTo);
  }
  assert bu shi yi ge cang cu pin cou qi lai de hong , wei le bu zai cheng xu de Debug ban ben he Release ban ben yin qi cha bie ,assert bu ying gai chan sheng ren he fu zuo yong 。 suo yi assert bu shi han shu , er shi hong 。 cheng xu yuan ke yi ba assert kan cheng yi ge zai ren he xi tong zhuang tai xia dou ke yi an quan shi yong de wu hai ce shi shou duan 。
  hen shao you bi gen zong dao cheng xu de duan yan , que bu zhi dao gai duan yan de zuo yong geng rang ren ju sang de shi le 。 ni hua le hen duo shi jian , bu shi wei le pai chu cuo wu , er zhi shi wei le nong qing chu zhe ge cuo wu dao di shi shen me 。 you de shi hou , cheng xu yuan ou ran huan hui she ji chu you cuo wu de duan yan 。 suo yi jia ru gao bu qing chu duan yan jian cha de shi shen me , jiu hen nan pan ding cuo wu shi chu xian zai cheng xu zhong , huan shi chu xian zai duan yan zhong 。 xing yun de shi zhe ge wen ti hen hao jie jue , zhi yao jia shang qing chu de zhu shi ji ke 。 zhe ben shi xian er yi jian de shi qing , ke shi hen shao you cheng xu yuan zhe yang zuo 。 zhe hao bi yi ge ren zai sen lin li , kan dao shu shang ding zhe yi kuai “ wei xian ” de da pai zi 。 dan wei xian dao di shi shen me ? shu yao dao ? you fei jing ? you ye shou ? chu fei gao su ren men “ wei xian ” shi shen me , fou ze zhe ge jing gao pai nan yi qi dao ji ji you xiao de zuo yong 。 nan yi li jie de duan yan jing chang bei cheng xu yuan hu lue , shen zhi bei shan chu 。[Maguire 1993]
  yi xia shi shi yong duan yan de ji ge yuan ze :
  (1) shi yong duan yan bu zhuo bu ying gai fa sheng de fei fa qing kuang 。 bu yao hun xiao fei fa qing kuang yu cuo wu qing kuang zhi jian de qu bie , hou zhe shi bi ran cun zai de bing qie shi yi ding yao zuo chu chu li de 。
  (2) shi yong duan yan dui han shu de shen shu jin xing que ren 。
  (3) zai bian xie han shu shi , yao jin xing fan fu de kao cha , bing qie zi wen :“ wo da suan zuo na xie jia ding ?” yi dan que ding le de
  
   jia ding , jiu yao shi yong duan yan dui jia ding jin xing jian cha 。
  (4) yi ban jiao ke shu dou gu li cheng xu yuan men jin xing fang cuo xing de cheng xu she ji , dan yao ji zhu zhe zhong bian cheng feng ge hui yin man cuo wu 。 dang jin xing fang cuo xing bian cheng shi , jia ru “ bu ke neng fa sheng ” de shi qing de que fa sheng le , ze yao shi yong duan yan jin xing bao jing 。
修练8年C++面向对象程序设计之体会
geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo
  2.3 new、delete yu zhi zhen
  zai C++ zhong , cao zuo fu new yong yu shen qing nei cun , cao zuo fu delete yong yu shi fang nei cun 。 zai C yu yan zhong , han shu malloc yong yu shen qing nei cun , han shu free yong yu shi fang nei cun 。 you yu C++ jian rong C yu yan , suo yi new、delete、malloc、free dou you ke neng yi qi shi yong 。
   new neng bi malloc gan geng duo de shi , ta ke yi shen qing dui xiang de nei cun , er malloc bu neng 。C++ he C yu yan zhong de zhi zhen wei meng wu bi , yong cuo le hui dai lai zai nan 。 dui yu yi ge zhi zhen p, jia ru shi yong new shen qing de nei cun , ze bi xu yong delete er bu neng yong free lai shi fang 。 jia ru shi yong malloc shen qing de nei cun , ze bi xu yong free er bu neng yong delete lai shi fang 。 zai yong delete huo yong free shi fang p suo zhi de nei cun hou , ying gai ma shang xian shi di jiang p zhi wei NULL, yi fang xia ci shi yong p shi fa sheng cuo wu 。 shi li cheng xu ru xia :
  void Test(void)
  {
  float *p;
  p = new float[100];
  if(p==NULL) return;
  …// do something
  delete p;
  p=NULL; // liang hao de bian cheng feng ge
  // ke yi ji xu shi yong p
  p = new float[500];
  if(p==NULL) return;
  …// do something else
  delete p;
  p=NULL;
  }
  wo men huan yao yu fang “ ye zhi zhen ”,“ ye zhi zhen ” shi zhi xiang “ la ji ” nei cun de zhi zhen , zhu yao cheng yin you liang zhong :
  (1) zhi zhen mei you chu shi hua 。
  (2) zhi zhen zhi xiang yi jing shi fang de nei cun , zhe zhong qing kuang zui rang ren fang bu sheng fang , shi li cheng xu ru xia :
  class A
  {
  public:
  void Func(void){…}
  };
  void Test(void)
  {
  A *p;
  {
  A a;
  p = &a; // zhu chong a de sheng ming qi
  }
  p->Func(); // p shi “ ye zhi zhen ”, cheng xu chu cuo
  }
  2.4 shi yong const
  zai ding yi yi ge chang liang shi ,const bi #define geng jia ling huo 。 yong const ding yi de chang liang han you shu ju lei xing , gai chang liang ke yi shen yu luo ji yun suan 。 li ru :
  const int LENGTH = 100; // LENGTH shi int lei xing
  const float MAX=100; // MAX shi float lei xing
  #define LENGTH 100 // LENGTH wu lei xing
  #define MAX 100 // MAX wu lei xing
  chu le neng ding yi chang liang wai ,const huan you liang ge “ bao hu ” gong neng :
  yi 、 qiang zhi bao hu han shu de shen shu zhi bu fa sheng bian hua
  yi xia cheng xu zhong , han shu f bu hui gai bian shu ru shen shu name de zhi , dan shi han shu g he h dou you ke neng gai bian name de zhi 。
  void f(String s); // pass by value
  void g(String &s); // pass by referance
  void h(String *s); // pass by pointer
  main()
  {
  String name=“Dog”;
  f(name); // name de zhi bu hui gai bian
  g(name); // name de zhi ke neng gai bian
  h(name); // name de zhi ke neng gai bian
  }
  dui yu yi ge han shu er yan , jia ru qi ‘&’ huo ‘*’ lei xing de shen shu zhi zuo shu ru yong , bu zuo shu chu yong , na me ying dang zai gai shen shu qian jia shang const, yi que bao han shu de dai ma bu hui gai bian gai shen shu de zhi ( jia ru gai bian le gai shen shu de zhi , bian yi qi hui chu xian cuo wu jing gao )。 yin ci shang shu cheng xu zhong de han shu g he h ying gai ding yi cheng :
  void g(const String &s);
  void h(const String *s);
  er 、 qiang zhi bao hu lei de cheng yuan han shu bu gai bian ren he shu ju cheng yuan de zhi
  yi xia cheng xu zhong , lei stack de cheng yuan han shu Count jin yong yu ji shu , wei le que bao Count bu gai bian lei zhong de ren he shu ju cheng yuan de zhi , ying jiang han shu Count ding yi cheng const lei xing 。
  class Stack
  {
  public:
  void push(int elem);
  void pop(void);
  int Count(void) const; // const lei xing de han shu
  private:
  int num;
  int data[100];
  };
  int Stack::Count(void) const
  {
  ++ num; // bian yi cuo wu ,num zhi fa sheng bian hua
  pop(); // bian yi cuo wu ,pop jiang gai bian cheng yuan bian liang de zhi
  return num;
  }
修练8年C++面向对象程序设计之体会
geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo
  2.5 qi ta jian yi
  (1) bu yao bian xie yi tiao guo fen fu duo de yu ju , jin cou de C++/C dai ma bing bu jian dao neng de dao gao xiao lv de ji qi dai ma , que hui jiang di cheng xu de ke li jie xing , cheng xu chu cuo wu de ji lv ye hui ti gao 。
  
   (2) bu yao bian xie ji duo zhong gong neng yu yi shen de han shu , zai han shu de fan hui zhi zhong , bu yao jiang zheng chang zhi he cuo wu biao zhi hun zai yi qi 。
  
   (3) bu yao jiang BOOL zhi TRUE he FALSE dui ying yu 1 he 0 jin xing bian cheng 。 da duo shu bian cheng yu yan jiang FALSE ding yi wei 0, ren he fei 0 zhi dou shi TRUE。Visual C++ jiang TRUE ding yi wei 1, er Visual Basic ze jiang TRUE ding yi wei -1。 shi li cheng xu ru xia :
  BOOL flag;
  …
  if(flag) { // do something } // zheng que de yong fa
  if(flag==TRUE) { // do something } // wei xian de yong fa
  if(flag==1) { // do something } // wei xian de yong fa
  if(!flag) { // do something } // zheng que de yong fa
  if(flag==FALSE) { // do something } // bu he li de yong fa
  if(flag==0) { // do something } // bu he li de yong fa
  (4) xiao xin bu yao jiang “= =” xie cheng “=”, bian yi qi bu hui zi dong fa xian zhe zhong cuo wu 。
  (5) bu yao jiang 123 xie cheng 0123, hou zhe shi ba jin zhi de shu zhi 。
  (6) jiang zi ji jing chang fan de bian cheng cuo wu ji lu xia lai , zhi cheng biao ge tie zai ji suan ji pang bian 。
  3 xiao jie
  C++/C cheng xu she ji ru tong shao lin shi de wu gong yi yang bo da jing shen , wo lian le 8 nian , da gai zhi xue dao er san cheng 。 suo yi wu lun shen me shi hou , dou bu yao jiao de zi ji de bian cheng shui ping tian xia di yi , kan dao bie ren hao de ji shu he feng ge , yao xu xin xue xi 。 ben zhang de nei rong shao de ke lian , jiu xiang kou ke shi zhi gei ni yi ke yang mei chi , ni yi ding bu guo yin 。 wo jie hua xian fo , tui jian yi ben hao shu :Marshall P. Cline zhu de 《C++ FAQs》[Cline 1995]。 ni kan le hou yi ding hui zan bu jue kou 。 hui bian xie C++/C cheng xu , bu yao yin ci zi de yang yang , zhe zhi shi cheng xu yuan ji ben de ji neng yao qiu er yi 。 jia ru ba xi tong fen xi he xi tong she ji bi zuo “ zhan lue jue ce ”, na me bian cheng chong qi liang zhi shi “ zhan shu ”。 jia ru zhi hui guan shi ge da ben dan , shi bing zai yong gan ye hui chi bai zhang 。 suo yi wo men cheng xu yuan bu yao zhi ba yan guang ding zai cheng xu shang , yao rang zi ji bo xue duo cai 。 wo men ying gai xiang bei jing hu tong li de xiao hai men xue xi , ta men xiao xiao nian ji jiu neng zhi dian jiang shan , ping lun shi jie da shi 。
修练8年C++面向对象程序设计之体会
geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo【原文】【汉音对照
 
 
 
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

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

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

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

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

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

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

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

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

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

 
 
   liu nian qian , wo gang re lian “ mian xiang dui xiang ”(Object-Oriented) shi , yi kou qi ji zhu le jin shi ge ding yi 。 liu nian hou , wo cong ji shi wan xing cheng xu zhong gun pa chu lai yu bei xie dian xin de ti hui shi , que wu fa jie shi shen me shi “ mian xiang dui xiang ”, jiu xiang shuo bu qing chu shen me shi shu xue na yang 。 ruan jian gong cheng zhong de shi mao shu yu “ mian xiang dui xiang fen xi ” he “ mian xiang dui xiang she ji ”, tong chang shi zhen dui “ xu qiu fen xi ” he “ xi tong she ji ” huan jie de 。“ mian xiang dui xiang ” you ji da xue pai , jiu xiang ru lai fo 、 shang di he zhen zhu yong ge zi de fang shi ding yi le zhe ge shi jie , bing liu xia yi dui jing shu lai jie shi zhe ge shi jie 。    you xie xue zhe jian yi zhe yang zhao “ dui xiang ”: fen xi yi ge ju zi de yu fa , zhao chu ming ci he dong ci , ming ci jiu shi dui xiang , dong ci ze shi dui xiang de fang fa ( ji han shu )。    dang nian guo min dang de wen ren wei le dui kang mao ze dong de 《 qin yuan chun · xue 》, te yi qing qing chao yi lao men xie le yi xie dui zhang gong zheng de shi , qing jiang jie shi guo mu 。 lao jiang kan le qi de da ma :“ niang xi pi , quan dou you yi gu guan cai li fu shi de qi wei 。” wo kan le ji qian ye de ruan jian gong cheng zi liao , zhong yu fa xian zi ji you xie “ ruo zhi ”, wu fa li jie “ mian xiang dui xiang ” de li lun , tong shi xing wu dao “ bian cheng shi ying dao li 。”    mian xiang dui xiang cheng xu she ji yu yan hen duo , ru Smalltalk、Ada、Eiffel、Object Pascal、Visual Basic、C++ deng deng 。C++ yu yan zui tao ren xi huan , yin wei ta jian rong C yu yan , bing qie ju bei C yu yan de xing neng 。 jin ji nian , yi zhong jiao java de chun mian xiang dui xiang yu yan hong ji yi shi , bu shao ren jiao han zhe yao yong Java ge C++ de ming 。 wo ren wei Java hao bi shi C++ de wai sheng , sui ran bu shi zhi jie yi chuan de , dan ye ji fen xiang yang 。 wai sheng zai jiu jiu shen shang wan shua shi sa le yi pao niao , liang ren bu gai wei ci er zheng chao 。    guan yu C++ cheng xu she ji de shu ji fei chang duo , ben zhang bu jiang C++ de yu fa , zhi jiang yi xie xiao xiao de bian cheng dao li 。 jia ru wo neng zao ji nian ming bai zhe xie xiao dao li , jiu ke yi da da gai shan shu shi wan xing cheng xu de zhi liang le 。 1. C++ mian xiang dui xiang cheng xu she ji de chong yao gai nian    zao qi ge ming ying pian li you zhe yang yi ge jiao se , ta shuo :“ wo shi dang dai biao , wo dai biao dang , wo jiu shi dang 。” hou lai ta gei tong zhi men dai lai le zai nan 。    hui yong C++ de cheng xu yuan yi ding dong de mian xiang dui xiang cheng xu she ji ma ?    bu hui yong C++ de cheng xu yuan yi ding bu dong de mian xiang dui xiang cheng xu she ji ma ?    liang zhe dou wei bi 。 jiu xiang huai dan ru dang hou wei bi neng cheng wei hao ren , hao ren bu ru dang wei bi bian cheng huai dan na yang 。    wo bu pa chu fan zhong nu di shuo ju da hua :“C++ mei you gao shou ,C yu yan cai you gao shou 。” zai yong C he C++ bian cheng 8 nian zhi hou , wo shen shen di yi han zi ji bu shi C yu yan de gao shou , geng yi han mei you ren dian bo wo ru he jin xing mian xiang dui xiang cheng xu she ji 。 wo he hen duo C++ cheng xu yuan yi yang , zai xiang yong dao C++ yu fa de hao chu shi bian yi wei zi ji yi jing ming bai le mian xiang dui xiang cheng xu she ji 。 jiu xiang ji diao ya gao mai ya gao pi na yang , zhen shi bao tian tian wu ya 。    ren men bu dong pin yin ye hui jiang pu tong hua , jia ru dong de pin yin ze hui ba pu tong hua jiang de geng hao 。 bu dong mian xiang dui xiang cheng xu she ji ye ke yi yong C++ bian cheng , jia ru dong de mian xiang dui xiang cheng xu she ji ze hui ba C++ cheng xu bian de geng hao 。 ben jie jiang shu san ge fei chang ji chu de gai nian :“ lei yu dui xiang ”、“ ji xu yu zu he ”、“ xu han shu yu duo tai ”。 li jie zhe xie gai nian , you zhu yu ti gao cheng xu de zhi liang , fei fan shi ti gao “ ke fu yong xing ” yu “ ke kuo chong xing ”。 1.1 lei yu dui xiang    dui xiang (Object) shi lei (Class) de yi ge shi li (Instance)。 jia ru jiang dui xiang bi zuo fang zi , na me lei jiu shi fang zi de she ji tu zhi 。 suo yi mian xiang dui xiang cheng xu she ji de chong dian shi lei de she ji , er bu shi dui xiang de she ji 。 lei ke yi jiang shu ju he han shu feng zhuang zai yi qi , qi zhong han shu biao shi le lei de xing wei ( huo cheng fu wu )。 lei ti gong yao hai zi public、PRotected he private yong yu sheng ming na xie shu ju he han shu shi gong you de 、 shou bao hu de huo zhe shi si you de 。    zhe yang ke yi da dao xin xi yin cang de mu de , ji rang lei jin jin gong kai bi xu yao rang wai jie zhi dao de nei rong , er yin cang qi ta yi qie nei rong 。 wo men bu ke yi lan yong lei de feng zhuang gong neng , bu yao ba ta dang cheng huo guo , shen me dong xi dou wang li reng 。    lei de she ji shi yi shu ju wei zhong xin , huan shi yi xing wei wei zhong xin ?    zhu zhang “ yi shu ju wei zhong xin ” de na yi pai ren guan zhu lei de nei bu shu ju jie gou , ta men xi guan shang jiang private lei xing de shu ju xie zai qian mian , er jiang public lei xing de han shu xie zai hou mian , ru biao 8.1(a) suo shi 。    zhu zhang “ yi xing wei wei zhong xin ” de na yi pai ren guan zhu lei ying gai ti gong shen me yang de fu wu he jie kou , ta men xi guan shang jiang public lei xing de han shu xie zai qian mian , er jiang private lei xing de shu ju xie zai hou mian , ru biao 8.1(b) suo shi 。 [url=http://www.wangchao.net.cn/bbs/pydetail_1785440.html][img]http://image.wangchao.net.cn/it/1323423329670.gif[/img][/url]    hen duo C++ jiao ke shu zhu zhang zai she ji lei shi “ yi shu ju wei zhong xin ”。 wo jian chi bing qie jian yi du zhe zai she ji lei shi “ yi xing wei wei zhong xin ”, ji shou xian kao lv lei ying gai ti gong shen me yang de han shu 。Microsoft gong si de COM gui fan de gai xin shi jie kou she ji ,COM de jie kou jiu xiang dang yu lei de gong you han shu [Rogerson 1999]。 zai cheng xu she ji fang mian , za men bu yao fu yi Microsoft gong si de feng ge 。    she ji gu li de lei shi bi jiao qing yi de , nan de shi zheng que she ji ji lei ji qi pai sheng lei 。 yin wei you xie cheng xu yuan gao bu qing chu “ ji xu ”(Inheritance)、“ zu he ”(Composition)、“ duo tai ”( Polymorphism) zhe xie gai nian 。 [url=http://www.wangchao.net.cn/bbs/pydetail_1785440.html][img]http://image.wangchao.net.cn/it/1323423344755.gif[/img][/url] geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo 1.2 ji xu yu zu he    jia ru A shi ji lei ,B shi A de pai sheng lei , na me B jiang ji xu A de shu ju he han shu 。 shi li cheng xu ru xia : class A { public: void Func1(void); void Func2(void); }; class B : public A { public: void Func3(void); void Func4(void); }; // Example main() { B b; // B de yi ge dui xiang b.Func1(); // B cong A ji xu le han shu Func1 b.Func2(); // B cong A ji xu le han shu Func2 b.Func3(); b.Func4(); }    zhe ge jian dan de shi li cheng xu shuo ming le yi ge shi shi :C++ de “ ji xu ” te xing ke yi ti gao cheng xu de ke fu yong xing 。 zheng yin wei “ ji xu ” tai you yong 、 tai qing yi yong , cai yao fang zhi luan yong “ ji xu ”。 wo men yao gei “ ji xu ” li yi xie shi yong gui ze :    yi 、 jia ru lei A he lei B hao bu xiang guan , bu ke yi wei le shi B de gong neng geng duo xie er rang B ji xu A de gong neng 。    bu yao jiao de “ bu chi bai bu chi ”, rang yi ge hao duan duan de jian zhuang qing nian wu yuan wu gu di chi ren shen bu shen ti 。    er 、 jia ru lei B you bi yao shi yong A de gong neng , ze yao fen liang zhong qing kuang kao lv :   (1) ruo zai luo ji shang B shi A de “ yi zhong ”(a kind of ), ze da ying B ji xu A de gong neng 。 ru nan ren (Man) shi ren (Human) de yi zhong , nan hai (Boy) shi nan ren de yi zhong 。 na me lei Man ke yi cong lei Human pai sheng , lei Boy ke yi cong lei Man pai sheng 。 shi li cheng xu ru xia : class Human { … }; class Man : public Human { … }; class Boy : public Man { … };   (2) ruo zai luo ji shang A shi B de “ yi bu fen ”(a part of), ze bu da ying B ji xu A de gong neng , er shi yao yong A he qi ta dong xi zu he chu B。 li ru yan (Eye)、 bi (Nose)、 kou (Mouth)、 er (Ear) shi tou (Head) de yi bu fen , suo yi lei Head ying gai you lei Eye、Nose、Mouth、Ear zu he er cheng , bu shi pai sheng er cheng 。 shi li cheng xu ru xia : class Eye { public: void Look(void); }; class Nose { public: void Smell(void); }; class Mouth { public: void Eat(void); }; class Ear { public: void Listen(void); }; // zheng que de she ji , rong chang de cheng xu class Head { public: void Look(void) { m_eye.Look(); } void Smell(void) { m_nose.Smell(); } void Eat(void) { m_mouth.Eat(); } void Listen(void) { m_ear.Listen(); } private: Eye m_eye; Nose m_nose; Mouth m_mouth; Ear m_ear; };    jia ru da ying Head cong Eye、Nose、Mouth、Ear pai sheng er cheng , na me Head jiang zi dong ju you Look、Smell、Eat、Listen zhe xie gong neng : // cuo wu de she ji class Head : public Eye, public Nose, public Mouth, public Ear { };    shang shu cheng xu shi fen jian duan bing qie yun xing zheng que , dan shi zhe zhong she ji que shi cuo wu de 。 hen duo cheng xu yuan jing bu qi “ ji xu ” de you huo er fan xia she ji cuo wu 。    yi zhi gong ji shi jing di zhui da yi zhi gang xia le dan de mu ji , ni zhi dao wei shen me ma ?    yin wei mu ji xia le ya dan 。    ben shu 3.3 jie jiang guo “ yun xing zheng que ” de cheng xu bu jian de jiu shi gao zhi liang de cheng xu , ci chu jiu shi yi ge li zheng 。 [url=http://www.wangchao.net.cn/bbs/pydetail_1785440.html][img]http://image.wangchao.net.cn/it/1323423344799.gif[/img][/url] geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo 1.3 xu han shu yu duo tai    chu le ji xu wai ,C++ de ling yi ge you liang te xing shi zhi chi duo tai , ji da ying jiang pai sheng lei de dui xiang dang zuo ji lei de dui xiang shi yong 。 jia ru A shi ji lei ,B he C shi A de pai sheng lei , duo tai han shu Test de shen shu shi A de zhi zhen 。 na me Test han shu ke yi yin yong A、B、C de dui xiang 。 shi li cheng xu ru xia : class A { public: void Func1(void); }; void Test(A *a) { a->Func1(); } class B : public A { … }; class C : public A { … }; // Example main() { A a; B b; C c; Test(&a); Test(&b); Test(&c); };    yi shang cheng xu kan bu chu “ duo tai ” you shen me jia zhi , jia shang xu han shu he chou xiang ji lei hou ,“ duo tai ” de wei li jiu xian shi chu lai le 。   C++ yong yao hai zi virtual lai sheng ming yi ge han shu wei xu han shu , pai sheng lei de xu han shu jiang (override) ji lei dui ying de xu han shu de gong neng 。 shi li cheng xu ru xia : class A { public: virtual void Func1(void){ cout<< “This is A::Func1 \n”} }; void Test(A *a) { a->Func1(); } class B : public A { public: virtual void Func1(void){ cout<< “This is B::Func1 \n”} }; class C : public A { public: virtual void Func1(void){ cout<< “This is C::Func1 \n”} }; // Example main() { A a; B b; C c; Test(&a); // shu chu This is A::Func1 Test(&b); // shu chu This is B::Func1 Test(&c); // shu chu This is C::Func1 };    jia ru ji lei A ding yi ru xia : class A { public: virtual void Func1(void)=0; };    na me han shu Func1 jiao zuo chun xu han shu , han you chun xu han shu de lei jiao zuo chou xiang ji lei 。 chou xiang ji lei zhi guan ding yi chun xu han shu de xing shi , ju ti de gong neng you pai sheng lei shi xian 。    jie he “ chou xiang ji lei ” he “ duo tai ” you ru xia tu chu you dian :   (1) ying yong cheng xu bu bi wei mei yi ge pai sheng lei bian xie gong neng tiao yong , zhi xu yao dui chou xiang ji lei jin xing chu li ji ke 。 zhe yi zhao jiao “ yi bu bian ying wan bian ”, ke yi da da ti gao cheng xu de ke fu yong xing ( zhe shi jie kou she ji de fu yong , er bu shi dai ma shi xian de fu yong )。   (2) pai sheng lei de gong neng ke yi bei ji lei zhi zhen yin yong , zhe jiao xiang hou jian rong , ke yi ti gao cheng xu de ke kuo chong xing he ke wei hu xing 。 yi qian xie de cheng xu ke yi bei jiang lai xie de cheng xu tiao yong bu zu wei qi , dan shi jiang lai xie de cheng xu ke yi bei yi qian xie de cheng xu tiao yong na ke le bu qi 。 [url=http://www.wangchao.net.cn/bbs/pydetail_1785440.html][img]http://image.wangchao.net.cn/it/1323423344815.gif[/img][/url] geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo 2 liang hao de bian cheng feng ge    nei gong shen hou de wu lin gao shou chu zhao wang wang ping dan wu qi 。 tong li , bian cheng gao shou ye bu hui yong qi men guai zhao xie cheng xu 。 liang hao de bian cheng feng ge shi chan sheng gao zhi liang cheng xu de qian ti 。 2.1 ming ming yue ding    you bu shao ren bian cheng shi yong pin yin gei han shu huo bian liang ming ming , zhe yang zuo bing bu neng shuo ming ni hen ai guo , que hui rang yong ci cheng xu de ren mi hu ( hen duo nan fang ren bu dong pin yin , wo jiu bu dong )。 cheng xu zhong de ying wen yi ban bu hui tai fu duo , yong ci yao li qiu zhun que 。 xiong ya li ming ming fa shi Microsoft gong si chang dao de [Maguire 1993], sui ran hen fan suo , dan yong xi guan le ye jiu cheng le zi ran 。 mei you ren qiang po ni cai yong he zhong ming ming fa , dan you yi dian ying gai zuo dao : zi ji de cheng xu ming ming bi xu yi zhi 。    yi xia shi wo bian cheng shi cai yong de ming ming yue ding :   (1) hong ding yi yong da xie zi mu jia xia hua xian biao shi , ru MAX_LENGTH;   (2) han shu yong da xie zi mu kai tou de dan ci zu he er cheng , ru SetName, GetName ;   (3) zhi zhen bian liang jia qian zhui p, ru *pNode ;   (4)BOOL bian liang jia qian zhui b, ru bFlag ;   (5)int bian liang jia qian zhui i, ru iWidth ;   (6)float bian liang jia qian zhui f, ru fWidth ;   (7)double bian liang jia qian zhui d, ru dWidth ;   (8) zi fu chuan bian liang jia qian zhui str, ru strName ;   (9) mei ju bian liang jia qian zhui e, ru eDrawMode ;   (10) lei de cheng yuan bian liang jia qian zhui m_, ru m_strName, m_iWidth ;    dui yu int, float, double xing de bian liang , jia ru bian liang ming de han yi shi fen ming xian , ze bu jia qian zhui , bi mian fan suo 。 ru yong yu xun huan de int xing bian liang i,j,k ;float xing de san wei zuo biao (x,y,z) deng 。 2.2 shi yong duan yan    cheng xu yi ban fen wei Debug ban ben he Release ban ben ,Debug ban ben yong yu nei bu tiao shi ,Release ban ben fa xing gei yong hu shi yong 。 duan yan assert shi jin zai Debug ban ben qi zuo yong de hong , ta yong yu jian cha “ bu ying gai ” fa sheng de qing kuang 。 yi xia shi yi ge nei cun fu zhi cheng xu , zai yun xing guo cheng zhong , jia ru assert de shen shu wei jia , na me cheng xu jiu hui zhong zhi ( yi ban di huan hui chu xian ti shi dui hua , shuo ming zai shen me di fang yin fa le assert)。 // fu zhi bu chong die de nei cun kuai void memcpy(void *pvTo, void *pvFrom, size_t size) { void *pBTo = (byte *) pvTo; void *pbFrom = (byte *) pvFrom; assert( pvTo != NULL && pvFrom != NULL ); while(size - - > 0 ) *pbTo + + = *pbFrom + + ; return (pvTo); }   assert bu shi yi ge cang cu pin cou qi lai de hong , wei le bu zai cheng xu de Debug ban ben he Release ban ben yin qi cha bie ,assert bu ying gai chan sheng ren he fu zuo yong 。 suo yi assert bu shi han shu , er shi hong 。 cheng xu yuan ke yi ba assert kan cheng yi ge zai ren he xi tong zhuang tai xia dou ke yi an quan shi yong de wu hai ce shi shou duan 。    hen shao you bi gen zong dao cheng xu de duan yan , que bu zhi dao gai duan yan de zuo yong geng rang ren ju sang de shi le 。 ni hua le hen duo shi jian , bu shi wei le pai chu cuo wu , er zhi shi wei le nong qing chu zhe ge cuo wu dao di shi shen me 。 you de shi hou , cheng xu yuan ou ran huan hui she ji chu you cuo wu de duan yan 。 suo yi jia ru gao bu qing chu duan yan jian cha de shi shen me , jiu hen nan pan ding cuo wu shi chu xian zai cheng xu zhong , huan shi chu xian zai duan yan zhong 。 xing yun de shi zhe ge wen ti hen hao jie jue , zhi yao jia shang qing chu de zhu shi ji ke 。 zhe ben shi xian er yi jian de shi qing , ke shi hen shao you cheng xu yuan zhe yang zuo 。 zhe hao bi yi ge ren zai sen lin li , kan dao shu shang ding zhe yi kuai “ wei xian ” de da pai zi 。 dan wei xian dao di shi shen me ? shu yao dao ? you fei jing ? you ye shou ? chu fei gao su ren men “ wei xian ” shi shen me , fou ze zhe ge jing gao pai nan yi qi dao ji ji you xiao de zuo yong 。 nan yi li jie de duan yan jing chang bei cheng xu yuan hu lue , shen zhi bei shan chu 。[Maguire 1993]    yi xia shi shi yong duan yan de ji ge yuan ze :   (1) shi yong duan yan bu zhuo bu ying gai fa sheng de fei fa qing kuang 。 bu yao hun xiao fei fa qing kuang yu cuo wu qing kuang zhi jian de qu bie , hou zhe shi bi ran cun zai de bing qie shi yi ding yao zuo chu chu li de 。   (2) shi yong duan yan dui han shu de shen shu jin xing que ren 。   (3) zai bian xie han shu shi , yao jin xing fan fu de kao cha , bing qie zi wen :“ wo da suan zuo na xie jia ding ?” yi dan que ding le de jia ding , jiu yao shi yong duan yan dui jia ding jin xing jian cha 。   (4) yi ban jiao ke shu dou gu li cheng xu yuan men jin xing fang cuo xing de cheng xu she ji , dan yao ji zhu zhe zhong bian cheng feng ge hui yin man cuo wu 。 dang jin xing fang cuo xing bian cheng shi , jia ru “ bu ke neng fa sheng ” de shi qing de que fa sheng le , ze yao shi yong duan yan jin xing bao jing 。 [url=http://www.wangchao.net.cn/bbs/pydetail_1785440.html][img]http://image.wangchao.net.cn/it/1323423344851.gif[/img][/url] geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo 2.3 new、delete yu zhi zhen    zai C++ zhong , cao zuo fu new yong yu shen qing nei cun , cao zuo fu delete yong yu shi fang nei cun 。 zai C yu yan zhong , han shu malloc yong yu shen qing nei cun , han shu free yong yu shi fang nei cun 。 you yu C++ jian rong C yu yan , suo yi new、delete、malloc、free dou you ke neng yi qi shi yong 。 new neng bi malloc gan geng duo de shi , ta ke yi shen qing dui xiang de nei cun , er malloc bu neng 。C++ he C yu yan zhong de zhi zhen wei meng wu bi , yong cuo le hui dai lai zai nan 。 dui yu yi ge zhi zhen p, jia ru shi yong new shen qing de nei cun , ze bi xu yong delete er bu neng yong free lai shi fang 。 jia ru shi yong malloc shen qing de nei cun , ze bi xu yong free er bu neng yong delete lai shi fang 。 zai yong delete huo yong free shi fang p suo zhi de nei cun hou , ying gai ma shang xian shi di jiang p zhi wei NULL, yi fang xia ci shi yong p shi fa sheng cuo wu 。 shi li cheng xu ru xia : void Test(void) { float *p; p = new float[100]; if(p==NULL) return; …// do something delete p; p=NULL; // liang hao de bian cheng feng ge // ke yi ji xu shi yong p p = new float[500]; if(p==NULL) return; …// do something else delete p; p=NULL; }    wo men huan yao yu fang “ ye zhi zhen ”,“ ye zhi zhen ” shi zhi xiang “ la ji ” nei cun de zhi zhen , zhu yao cheng yin you liang zhong :   (1) zhi zhen mei you chu shi hua 。   (2) zhi zhen zhi xiang yi jing shi fang de nei cun , zhe zhong qing kuang zui rang ren fang bu sheng fang , shi li cheng xu ru xia : class A { public: void Func(void){…} }; void Test(void) { A *p; { A a; p = &a; // zhu chong a de sheng ming qi } p->Func(); // p shi “ ye zhi zhen ”, cheng xu chu cuo } 2.4 shi yong const    zai ding yi yi ge chang liang shi ,const bi #define geng jia ling huo 。 yong const ding yi de chang liang han you shu ju lei xing , gai chang liang ke yi shen yu luo ji yun suan 。 li ru : const int LENGTH = 100; // LENGTH shi int lei xing const float MAX=100; // MAX shi float lei xing #define LENGTH 100 // LENGTH wu lei xing #define MAX 100 // MAX wu lei xing    chu le neng ding yi chang liang wai ,const huan you liang ge “ bao hu ” gong neng :    yi 、 qiang zhi bao hu han shu de shen shu zhi bu fa sheng bian hua    yi xia cheng xu zhong , han shu f bu hui gai bian shu ru shen shu name de zhi , dan shi han shu g he h dou you ke neng gai bian name de zhi 。 void f(String s); // pass by value void g(String &s); // pass by referance void h(String *s); // pass by pointer main() { String name=“Dog”; f(name); // name de zhi bu hui gai bian g(name); // name de zhi ke neng gai bian h(name); // name de zhi ke neng gai bian }    dui yu yi ge han shu er yan , jia ru qi ‘&’ huo ‘*’ lei xing de shen shu zhi zuo shu ru yong , bu zuo shu chu yong , na me ying dang zai gai shen shu qian jia shang const, yi que bao han shu de dai ma bu hui gai bian gai shen shu de zhi ( jia ru gai bian le gai shen shu de zhi , bian yi qi hui chu xian cuo wu jing gao )。 yin ci shang shu cheng xu zhong de han shu g he h ying gai ding yi cheng : void g(const String &s); void h(const String *s);    er 、 qiang zhi bao hu lei de cheng yuan han shu bu gai bian ren he shu ju cheng yuan de zhi    yi xia cheng xu zhong , lei stack de cheng yuan han shu Count jin yong yu ji shu , wei le que bao Count bu gai bian lei zhong de ren he shu ju cheng yuan de zhi , ying jiang han shu Count ding yi cheng const lei xing 。 class Stack { public: void push(int elem); void pop(void); int Count(void) const; // const lei xing de han shu private: int num; int data[100]; }; int Stack::Count(void) const { ++ num; // bian yi cuo wu ,num zhi fa sheng bian hua pop(); // bian yi cuo wu ,pop jiang gai bian cheng yuan bian liang de zhi return num; } [url=http://www.wangchao.net.cn/bbs/pydetail_1785440.html][img]http://image.wangchao.net.cn/it/1323423344865.gif[/img][/url] geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo 2.5 qi ta jian yi   (1) bu yao bian xie yi tiao guo fen fu duo de yu ju , jin cou de C++/C dai ma bing bu jian dao neng de dao gao xiao lv de ji qi dai ma , que hui jiang di cheng xu de ke li jie xing , cheng xu chu cuo wu de ji lv ye hui ti gao 。   (2) bu yao bian xie ji duo zhong gong neng yu yi shen de han shu , zai han shu de fan hui zhi zhong , bu yao jiang zheng chang zhi he cuo wu biao zhi hun zai yi qi 。   (3) bu yao jiang BOOL zhi TRUE he FALSE dui ying yu 1 he 0 jin xing bian cheng 。 da duo shu bian cheng yu yan jiang FALSE ding yi wei 0, ren he fei 0 zhi dou shi TRUE。Visual C++ jiang TRUE ding yi wei 1, er Visual Basic ze jiang TRUE ding yi wei -1。 shi li cheng xu ru xia : BOOL flag; … if(flag) { // do something } // zheng que de yong fa if(flag==TRUE) { // do something } // wei xian de yong fa if(flag==1) { // do something } // wei xian de yong fa if(!flag) { // do something } // zheng que de yong fa if(flag==FALSE) { // do something } // bu he li de yong fa if(flag==0) { // do something } // bu he li de yong fa   (4) xiao xin bu yao jiang “= =” xie cheng “=”, bian yi qi bu hui zi dong fa xian zhe zhong cuo wu 。   (5) bu yao jiang 123 xie cheng 0123, hou zhe shi ba jin zhi de shu zhi 。   (6) jiang zi ji jing chang fan de bian cheng cuo wu ji lu xia lai , zhi cheng biao ge tie zai ji suan ji pang bian 。 3 xiao jie   C++/C cheng xu she ji ru tong shao lin shi de wu gong yi yang bo da jing shen , wo lian le 8 nian , da gai zhi xue dao er san cheng 。 suo yi wu lun shen me shi hou , dou bu yao jiao de zi ji de bian cheng shui ping tian xia di yi , kan dao bie ren hao de ji shu he feng ge , yao xu xin xue xi 。 ben zhang de nei rong shao de ke lian , jiu xiang kou ke shi zhi gei ni yi ke yang mei chi , ni yi ding bu guo yin 。 wo jie hua xian fo , tui jian yi ben hao shu :Marshall P. Cline zhu de 《C++ FAQs》[Cline 1995]。 ni kan le hou yi ding hui zan bu jue kou 。 hui bian xie C++/C cheng xu , bu yao yin ci zi de yang yang , zhe zhi shi cheng xu yuan ji ben de ji neng yao qiu er yi 。 jia ru ba xi tong fen xi he xi tong she ji bi zuo “ zhan lue jue ce ”, na me bian cheng chong qi liang zhi shi “ zhan shu ”。 jia ru zhi hui guan shi ge da ben dan , shi bing zai yong gan ye hui chi bai zhang 。 suo yi wo men cheng xu yuan bu yao zhi ba yan guang ding zai cheng xu shang , yao rang zi ji bo xue duo cai 。 wo men ying gai xiang bei jing hu tong li de xiao hai men xue xi , ta men xiao xiao nian ji jiu neng zhi dian jiang shan , ping lun shi jie da shi 。 [url=http://www.wangchao.net.cn/bbs/pydetail_1785440.html][img]http://image.wangchao.net.cn/it/1323423344880.gif[/img][/url] geng duo nei rong qing kan C/C++ ji shu zhuan ti C/C++ jin jie ji shu wen dang C/C++ xiang guan wen zhang zhuan ti , huo
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
时尚秀气模特裴裴(8)
时尚秀气模特裴裴(7)
时尚秀气模特裴裴(6)
时尚秀气模特裴裴(5)
白墙黑瓦
广州东郊[原创]
八美至丹巴的东谷天然盆景
不一样的
 
>>返回首页<<
 
 
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
 
©2005- 王朝网络 版权所有