main.cpp

来自「一本语言类编程书籍」· C++ 代码 · 共 34 行

CPP
34
字号
// Exercise 14.3 Using the subscript operator to reference characters.
// Two more prototypes are added to the MyString class declaration
// declaring the subscript operator functions for const and non-const objects

#include "MyString.h"
#include <iostream>
using std::cout;
using std::endl;
using mySpace::MyString;          // Using declaration

int main() {
  MyString s1("The fifth character is f");
  cout << "\n s1 is: ";
  s1.show();
  cout << "\nThe fifth character in s1 is: " << s1[4];

  cout << "\n\nChanging the fifth character of s1 to z...";
  s1[4] = 'z';
  s1.show();

  cout << "\n\nChanging half the characters of s1 to x...";
  for(int i = 0 ; i<s1.length()/2 ; i++)
    s1[i] = 'x';
  s1.show();
  
  // Demonstrate no subscripting allowed for const MyString objects
  const MyString s2("I am immutable");
  // Uncomment the following statement for an error.
  //s2[4] = 'z';

  cout << endl;

  return 0;
}

⌨️ 快捷键说明

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