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

📄 usinginvariants.cpp

📁 24学时攻克C++光盘源代码 深入浅出 方便实用
💻 CPP
字号:
 // Listing 21.4 Invariants
 #define DEBUG
 #define SHOW_INVARIANTS
 #include <iostream>
 #include <string.h>

 #ifndef DEBUG
 #define ASSERT(x)
 #else
 #define ASSERT(x) \
     if (! (x)) \
     { \
         std::cout << "ERROR!! Assert " << #x << " failed\n"; \
         std::cout << " on line " << __LINE__  << "\n"; \
         std::cout << " in file " << __FILE__ << "\n";  \
     }
 #endif

 class String
 {
 public:
     // constructors
     String();
     String(const char *const);
     String(const String &);
     ~String();

     char & operator[](int offset);
     char operator[](int offset) const;

     String & operator= (const String &);
     int GetLen()const { return itsLen; }
     const char * GetString() const { return itsString; }
     bool Invariants() const;

 private:
     String (int);         // private constructor
     char * itsString;
     unsigned short itsLen;
 };

 // default constructor creates string of 0 bytes
 String::String()
 {
     itsString = new char[1];
     itsString[0] = '\0';
     itsLen=0;
     ASSERT(Invariants());
 }

 // private (helper) constructor, used only by
 // class methods for creating a new string of
 // required size.  Null filled.
 String::String(int len)
 {
     itsString = new char[len+1];
     for (int i = 0; i<=len; i++)
         itsString[i] = '\0';
     itsLen=len;
     ASSERT(Invariants());
 }

 // Converts a character array to a String
 String::String(const char * const cString)
 {
     itsLen = strlen(cString);
     itsString = new char[itsLen+1];
     for (int i = 0; i<itsLen; i++)
         itsString[i] = cString[i];
     itsString[itsLen]='\0';
     ASSERT(Invariants());
 }

 // copy constructor
 String::String (const String & rhs)
 {
     itsLen=rhs.GetLen();
     itsString = new char[itsLen+1];
     for (int i = 0; i<itsLen;i++)
         itsString[i] = rhs[i];
     itsString[itsLen] = '\0';
     ASSERT(Invariants());
 }

 // destructor, frees allocated memory
 String::~String ()
 {
     ASSERT(Invariants());
     delete [] itsString;
     itsLen = 0;
 }

 // operator equals, frees existing memory
 // then copies string and size
 String& String::operator=(const String & rhs)
 {
     ASSERT(Invariants());
     if (this == &rhs)
         return *this;
     delete [] itsString;
     itsLen=rhs.GetLen();
     itsString = new char[itsLen+1];
     for (int i = 0; i<itsLen;i++)
         itsString[i] = rhs[i];
     itsString[itsLen] = '\0';
     ASSERT(Invariants());
     return *this;
 }

 //non constant offset operator, returns
 // reference to character so it can be
 // changed!
 char & String::operator[](int offset)
 {
     ASSERT(Invariants());
     if (offset > itsLen)
         return itsString[itsLen-1];
     else
         return itsString[offset];
     ASSERT(Invariants());
 }

 // constant offset operator for use
 // on const objects (see copy constructor!)
 char String::operator[](int offset) const
 {
     ASSERT(Invariants());
     if (offset > itsLen)
         return itsString[itsLen-1];
     else
         return itsString[offset];
     ASSERT(Invariants());
 }

 // ensure that the string has some length or
// the pointer is null and the length is zero
 bool String::Invariants() const
 {
 #ifdef SHOW_INVARIANTS
     std::cout << " String OK ";
 #endif
     return ( (itsLen && itsString) || (!itsLen && !itsString) );
 }

 class Animal
 {
 public:
     Animal():itsAge(1),itsName("John Q. Animal")
         {ASSERT(Invariants());}
     Animal(int, const String&);
     ~Animal(){}
     int GetAge() { ASSERT(Invariants()); return itsAge;}
     void SetAge(int Age)
         {
             ASSERT(Invariants());
             itsAge = Age;
             ASSERT(Invariants());
         }
     String& GetName() { ASSERT(Invariants()); return itsName;  }
     void SetName(const String& name)
         {
             ASSERT(Invariants());
             itsName = name;
             ASSERT(Invariants());
         }
     bool Invariants();
 private:
     int itsAge;
     String itsName;
 };

 Animal::Animal(int age, const String& name):
 itsAge(age),
 itsName(name)
 {
     ASSERT(Invariants());
 }

 bool Animal::Invariants()
 {
 #ifdef SHOW_INVARIANTS
     std::cout << " Animal OK ";
 #endif
     return (itsAge > 0 && itsName.GetLen());
 }

 int main()
 {
     Animal sparky(5,"Sparky");
     std::cout << "\n" << sparky.GetName().GetString() << " is ";
     std::cout << sparky.GetAge() << " years old.";
     sparky.SetAge(8);
     std::cout << "\n" << sparky.GetName().GetString() << " is ";
     std::cout << sparky.GetAge() << " years old.";
    return 0;
 }

⌨️ 快捷键说明

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