?? c13_10.cpp
字號:
#include <iostream >
using namespace std;
class CRectangle //定義矩形類Rectangle
{
public:
CRectangle();
void SetLength(int length);
int GetLength() const; // const 指示該函數內限制對成員變量的修改。
void SetWidth(int width);
int GetWidth() const ;
private:
int itsLength;
int itsWidth;
};
CRectangle::CRectangle()
{
itsWidth = 5;
itsLength = 10;
}
void CRectangle::SetLength(int length)
{
this->itsLength = length; //顯式地使用this指針來引用成員變量
}
int CRectangle::GetLength() const
{
return this->itsLength; //顯式地使用this指針來引用成員變量
}
void CRectangle::SetWidth(int width)
{
itsWidth = width; //this指針被隱式地使用
}
int CRectangle::GetWidth() const
{
return itsWidth; //this指針被隱式地使用
}
//-------------------------------------------------------//
int main()
{
CRectangle theRect;
cout << "矩形theRect的長是 " << theRect.GetLength() << endl;
cout << "矩形theRect的寬是 " << theRect.GetWidth() << endl;
//調用成員函數,重新設定矩形theRect的長和寬
theRect.SetLength(20);
theRect.SetWidth(10);
cout << "矩形theRect的長是 " << theRect.GetLength() << endl;
cout << "矩形theRect的寬是 " << theRect.GetWidth() << endl;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -