c++复习:类
一、不能被继承的类
构造函数析构函数定义为 private
public中定义static函数调用构造函数析构函数
class Sealed_classes { public: static Sealed_classes* getnew() { return new Sealed_classes(); } static void delete_new(Sealed_classes clss) { delete clss; } private: Sealed_classes(){} ~Sealed_classes(){} };
二、不用*/、循环、条件判断求1+2+...+n的和
使用构造函数+static实现求和
class Tmp { public: Tmp(){N++;sum+=N;} static void reset(){N=0;sum=0;} static int get_sum(){return sum;} private: static int N; static int sum; }; int main() { Tmp::reset(); Tmp *a=new tmp[n]; delete []a; a=NULL; return Tmp::get_sum(); }