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

📄 howmany.cpp

📁 ThinkingC++中文版
💻 CPP
字号:
//: C11:HowMany.cpp
// A class that counts its objects
#include <iostream>
#include <string>
using namespace std;

int objectcount = 0;
void print(const string& msg = "");

class HowMany {
public:
  HowMany()  //The constructor increments the count each time an object is created, 
  { 
	  objectcount++; 
  }
  
  ~HowMany() //the destructor decrements it.
  {
    objectcount--;
    ::print("~HowMany()");
  }
};
 
void print(const string& msg) 
{
    if(msg.size() != 0) 
		cout << msg << ": ";
    cout << "objectCount = " << objectcount << endl;
}


// Pass and return BY VALUE:
HowMany f(HowMany x) {
  print("x argument inside f()");
  return x;
}

void main() 
{
  HowMany h1;   //call the default constructor
  print("after construction of h1");
  HowMany h2(h1);//等价于 HowMany h2 = h1; 
  print("after construction of h2");

  HowMany h4 = f(h1); //how to create a new object from an existing object?
  print("after call to f()");
} ///:~

⌨️ 快捷键说明

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