414.cpp

来自「C++实训教程」· C++ 代码 · 共 33 行

CPP
33 行
字号
//414.CPP  创建对象的方法:全局对象、静态对象、使用引用、使用指针和局部对象
#include <iostream.h>
#include <cstring.h>

string sg("Global.");// a global string
void main()
{
  string& s1=*(new string("using reference")); //using reference
  string* pS2=new string ("using pointer");    //using pointer
  static string ss("Static.");
  string& s5=*(new string(s1));                //reference,new,copy

  cout <<"Display a global string    = " << sg << endl;
  cout <<"Display a string 1(&)      = " << s1 << endl;
  cout <<"Display a string 2(*)      = " <<*pS2<< endl;
  cout <<"Display a string 5(copy s4)= " << s5 << endl;
  cout <<"Display a static string    = " << ss << endl;
  s5 = "A new fifth string";
  cout <<"Display a new string 5 =     " << s5 << endl;
  delete &s1;
  delete pS2;
  delete &s5;
}
/*
Display a global string    = Global.
Display a string 1(&)      = using reference
Display a string 2(*)      = using pointer
Display a string 5(copy s4)= using reference
Display a static string    = Static.
Display a new string 5 =     A new fifth string

*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?