class MyClass{ public: MyClass(){ sum += n; n++; } static void init(){ sum = 0; n = 1; } static int getNum(){ return sum; } private: static int sum; static int n; };
int MyClass::sum = 0; int MyClass::n = 1;
class Base{ public: Base(){ } virtual int add(){
} private: }; class Derive:Base{ public: Derive(){ } virtual int private: }; // class Solution { public: ///构造函数版本 int Sum_Solution1(int n) { MyClass::init(); MyClass*h = new MyClass[n]; int res = h[0].getNum(); return res; }
///中断条件版本 int Sum_Solution2(int n) { int res = n; res && (res+=Sum_Solution(n-1)); return res; }
///虚函数版本 int Sum_Solution3(int n) { int res = n; res && (res+=Sum_Solution(n-1)); return res; } };