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) {
    }
};

 

posted @ 2023-09-05 00:55  水水君  阅读(18)  评论(0编辑  收藏  举报