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

📄 strequal.cpp

📁 本课程主要介绍面向对象程序设计的方法和c++语言的基本概念。以c++语言中的面向对象机制为主。学习者在学习过程中可以通过大量的程序实例和相关练习
💻 CPP
字号:
//strequal.cpp
//overloaded '==' operator compares strings
#include <iostream>
using namespace std;
#include <string.h>      //for strcmp()
////////////////////////////////////////////////////////////////
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 a String
         { cout << str; }
      void getstr()                    //read a string
         { cin.get(str, SZ); }
      bool operator == (String ss) const  //check for equality
         {
         return ( strcmp(str, ss.str)==0 ) ? true : false;
         }
   };
////////////////////////////////////////////////////////////////
int main()
   {
   String s1 = "yes";
   String s2 = "no";
   String s3;

   cout << "\nEnter 'yes' or 'no': ";
   s3.getstr();                        //get String from user

   if(s3==s1)                          //compare with "yes"
      cout << "You typed yes\n";
   else if(s3==s2)                     //compare with "no"
      cout << "You typed no\n";
   else
      cout << "You didn't follow instructions\n";
   return 0;
   }

⌨️ 快捷键说明

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