⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strplus.cpp

📁 本课程主要介绍面向对象程序设计的方法和c++语言的基本概念。以c++语言中的面向对象机制为主。学习者在学习过程中可以通过大量的程序实例和相关练习
💻 CPP
字号:
// strplus.cpp
// overloaded '+' operator concatenates strings
#include <iostream>
using namespace std;
#include <string.h>      //for strcpy(), strcat()
#include <stdlib.h>      //for exit()  
////////////////////////////////////////////////////////////////
class String             //user-defined string type
   {
   private:
      enum { SZ=80 };                //size of String objects
      char str[SZ];                  //holds a string
   public:
      String()                       //constructor, no args
         { strcpy(str, ""); }
      String( char s[] )             //constructor, one arg
         { strcpy(str, s); }
      void display() const           //display the String
         { cout << str; }
      String operator + (String ss) const  //add Strings
         {
         String temp;                //make a temporary String
         if( strlen(str) + strlen(ss.str) < SZ )
            {
            strcpy(temp.str, str);   //copy this string to temp
            strcat(temp.str, ss.str);  //add the argument string
            }
         else
            { cout << "\nString overflow"; exit(1); }
         return temp;                //return temp String
         }
   };
////////////////////////////////////////////////////////////////
int main()
   {
   String s1 = "\nMerry Christmas!  ";   //uses constructor 2
   String s2 = "Happy new year!";        //uses constructor 2
   String s3;                            //uses constructor 1

   s1.display();                         //display strings
   s2.display();
   s3.display();

   s3 = s1 + s2;                         //add s2 to s1,
                                         //   assign to s3
   s3.display();                         //display s3
   cout << endl;
   return 0;
   }

⌨️ 快捷键说明

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