- // example: class constructor
- #include <iostream>
- using namespace std;
- class Rectangle
- {
- private:
- int width, height;
- public:
- Rectangle ();
- Rectangle (int,int);//初始化目的
- int area () //运算输出目的
- { return (width*height);
- }
- };
- Rectangle::Rectangle (int a, int b):width(a),height(b)
- {
- }
- Rectangle::Rectangle ():width(0),height(0)
- {
- }
- int main ()
- { Rectangle recta;
- Rectangle rectb (5,6);
- cout << "recta area: " << recta.area() << endl;
- cout << "rectb area: " << rectb.area() << endl;
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/2010201410711.html
来源: http://www.codesnippet.cn/detail/2010201410711.html