#include
using namespace std;
class CPoint
{
public:
CPoint( int x, int y)
{
ixP = x; iyP = y;
}
private:
int ixP, iyP;
};
class COblong
{
public:
COblong( int x1, int y1, int x2, int y2)
:m_ptLT(x1,y1),m_ptRB(x2,y2)
{
m_ptLT = CPoint(x1, y1);
m_ptRB = CPoint(x2, y2);
}
private:
CPoint m_ptLT, m_ptRB;
};
int main()
{
COblong gr(10, 100, 60, 200);
cout<<"当前矩形的左上角坐标为:"<<gr.m_ptLT.ixP<<","<<gr.m_ptLT.iyP<<endl;
cout<<"当前矩形的右下角坐标为:"<<gr.m_ptRB.ixP<<","<<gr.m_ptRB.iyP<<endl;
return 0;
}
因为类的成员变量都定义为private,所以 gr.m_ptLT.ixP 无法访问 m_ptLT和 ixP,可以在类中实现get_XX()成员函数返回变量的值。
#include <iostream>
using namespace std;
class CPoint
{
public:
CPoint( int x, int y)
{
ixP = x; iyP = y;
}
int get_ixP()
{
return ixP;
}
int get_iyP()
{
return iyP;
}
private:
int ixP, iyP;
};
class COblong
{
public:
COblong( int x1, int y1, int x2, int y2):m_ptLT(x1,y1),m_ptRB(x2,y2)
{
//m_ptLT = CPoint(x1, y1);
//m_ptRB = CPoint(x2, y2);
}
CPoint get_ptLT()
{
return m_ptLT;
}
CPoint get_ptRB()
{
return m_ptRB;
}
private:
CPoint m_ptLT, m_ptRB;
};
int main()
{
COblong gr(10, 100, 60, 200);
cout<<"当前矩形的左上角坐标为:"<<gr.get_ptLT().get_ixP()<<","<<gr.get_ptLT().get_iyP()<<endl;
cout<<"当前矩形的右下角坐标为:"<<gr.get_ptRB().get_ixP()<<","<<gr.get_ptRB().get_iyP()<<endl;
return 0;
}