• 主页

# C++学习：继承问题

``````#include <iostream.h>
#include <math.h>

class CPoint
{
private:
double nPosX,nPosY;

public:
CPoint(double x, double y)
{
nPosX = x;
nPosY = y;
}
void ShowPos()
{
cout<<"当前位置: x = "<<nPosX<<" , y = "<<nPosY<<endl;
}
};
class CLine: public CPoint
{
private:
double nLength;

public:
CLine()
{
nLength = 0;
}
double LineLength(double x1, double y1, double x2, double y2)
{
nLength = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
return nLength;
}
};

class CRect: public CPoint
{
private:
double dWideL;
double dHeightL;

public:
CRect()
{
dWideL = 0;
dheightL = 0;
}
double RectSL(double WideL, double HeightL)
{
return 2*(WideL + HeightL);
}
};

int main()
{
CPoint LU(0.0,1.0);
CPoint LD(0.0,0.0);
CPoint RU(1.0,1.0);
CPoint RD(1.0,0.0);

CLine Wide;
CLine Length;

CRect One;

double AWide;
double ALength;
double P;

AWide = Wide.LineLength(0.0,1.0,1.0,1.0);
ALength = Length.LineLength(0.0,1.0,0.0,0.0);

P = One.RectSL(AWide, ALength);

return 0;
}``````

...全文
70 点赞 收藏 2

2 条回复
a707000646 2012年01月22日
1楼正解
Cline的构造函数可以这么写
CLine(double x, double y):CPoint(x,y)
{
nPosX = x;
nPosY = y;
}

C++ 语言

3.1w+

24.8w+

C++ 语言相关问题讨论，技术干货分享