C++ 类设计:实现书类 Book,统计数量和总价
class Book{ private: string bkName; double price; static int number; static double totalPrice; public: Book(string name, double p) :bkName(name), price(p) { number++; totalPrice += p; } ~Book() { number--; totalPrice -= price; } double getPrice() const { return price; } string getName() const { return bkName; } static int getNumber() { return number; } static double getTotalPrice() { return totalPrice; } };
// 静态成员变量需要在类外进行初始化 int Book::number = 0; double Book::totalPrice = 0.0;
原文地址: https://www.cveoy.top/t/topic/nXLI 著作权归作者所有。请勿转载和采集!