📄 +
字号:
【例5.6】 使用对象成员的例子。
#include<iostream>
using namespace std;
class Point{ //定义点类
int x,y;
public:
void Set(int a,int b){x=a; y=b;}
int Getx(){return x;}
int Gety(){return y;}
};
class Rectangle{//在矩形类里使用Point类的成员
Point Loc;//定义一个Point类的对象作为顶点
int H,W;//H为高,W为宽
public:
void Set(int x,int y,int h,int w);
Point*GetLoc();//用Point类定义返回指针的函数
int GetHeight(){ return H; }
int GetWidth(){ return W; }
};
void Rectangle ∷ Set(int x,int y,int h,int w)
{ Loc.Set(x,y);//初始化坐标顶点
H=h;
W=w;
}
Point *Rectangle ∷ GetLoc()//返回类型Point*,作为Rectangle的成员函数
{ return &Loc;}//返回对象Loc的地址
void main(){
Rectangle rect;
rect.Set(10,2,25,20);
cout<<rect.GetHeight()<<″,″<<rect.GetWidth()<<″,″;
Point*p=rect.GetLoc();//定义Point类的指针对象p并初始化
cout<<p->Getx()<<″,″<<p->Gety()<<endl;
}
程序输出结果为: 25,20,10,2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -