C++基礎 class類型
常規class
class Rectengle { int width, height;
static int a; // 類的公共變量,允許類名直接訪問 public: Rectengle(){}; Rectengle(int x, int y) : width(x), height(y) {}; //overload int area(void) { return width*height; } int getWidth(int);
// +運算符重載 Rectengle operator + (const Rectengle& rect) {
Rectengle newValue;
newValue.width = this.width + rect->width;
newValue.height = this.height + rect-height;
return newValue;
} }; /**
* 方法實現
*/ int Rectengle::getWidth(int a) { return this.width * a; };
可重載的運算符:
+ - * / =
> += -= *= /=
>>
= >>= == !=
= >= ++ -- % & ^ ! |
~ &= ^= |= && || %= [] () , ->* -> new
delete new[] delete[]
class Template
template <class T> class Child { T age; public: Child () { } Child (T a): age(a) { } }; // 指定類型的模版 template <> class Child <char> { char age; public: Child () { } Child (char a): age(a) { } };

浙公網安備 33010602011771號