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

📄 howmany2.cpp

📁 Thinking in C++ 2.0书籍源码光盘
💻 CPP
字号:
//: C11:HowMany2.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
// The copy-constructor
#include <fstream>
#include <cstring>
using namespace std;
ofstream out("HowMany2.out");

class HowMany2 {
  static const int bufsize = 30;
  char name[bufsize]; // Object identifier
  static int object_count;
public:
  HowMany2(const char* id = 0) {
    if(id) strncpy(name, id, bufsize);
    else *name = 0;
    ++object_count;
    print("HowMany2()");
  }
  // The copy-constructor:
  HowMany2(const HowMany2& h) {
    strncpy(name, h.name, bufsize);
    strncat(name, " copy", bufsize - strlen(name));
    ++object_count;
    print("HowMany2(HowMany2&)");
  }
  // Can't be static (printing name):
  void print(const char* msg = 0) const {
    if(msg) out << msg << endl;
    out << '\t' << name << ": "
        << "object_count = "
        << object_count << endl;
  }
  ~HowMany2() {
    --object_count;
    print("~HowMany2()");
  }
};

int HowMany2::object_count = 0;

// Pass and return BY VALUE:
HowMany2 f(HowMany2 x) {
  x.print("x argument inside f()");
  out << "returning from f()" << endl;
  return x;
}

int main() {
  HowMany2 h("h");
  out << "entering f()" << endl;
  HowMany2 h2 = f(h);
  h2.print("h2 after call to f()");
  out << "call f(), no return value" << endl;
  f(h);
  out << "after call to f()" << endl;
} ///:~

⌨️ 快捷键说明

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