📄 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 + -