rectangle2.cpp

来自「ThinkingC++中文版」· C++ 代码 · 共 34 行

CPP
34
字号
//: C09:Rectangle2.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Accessors & mutators with "get" and "set"

class Rectangle {
    int width, height;
public:
    Rectangle(int w = 0, int h = 0): width(w), height(h) 
	{}
    int getWidth() const   //access functions
	{ return width; }
    void setWidth(int w) 
	{ width = w; }
    int getHeight() const 
	{ return height; }
    void setHeight(int h) 
	{ height = h; }
};

int main() {
  Rectangle r(19, 47);
  // Change width & height:
  r.setHeight(2 * r.getWidth());
  r.setWidth(2 * r.getHeight());
} ///:~

//access is remarkably efficient. Consider the getWidth(), for example. Without inlines, 
//the code generated for the call to getWidth() would be larger than the code created by the 
//inline, and the execution time would certainly be longer.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?