问题描述:
核心1:在一个类中,可以让另一个类作为本类的成员。
//要求:
//判断点与圆的关系
#include<iostream>
#include<string>
using namespace std;
class Circle
{
public:
void setm_R(double R)
{
m_R = R;
}
double readm_R()
{
return m_R;
}
void setcenter(CenterPoint theP)
{
p = theP;
}
double readcenter_x()
{
return p.readpoint_x();
}
double readcenter_y()
{
return p.readpoint_y();
}
private://属性设置为私有
double m_R;
point p;
};
class point
{
public:
void setpoint_x(double thex)
{
point_x = thex;
}
double readpoint_x()
{
return point_x;
}
void setpoint_y(double they)
{
point_y = they;
}
double readpoint_y()
{
return point_y;
}
private:
double point_x;
double point_y;
};
void isSame(Circle c, point p)
{
if ((p.readpoint_x() - c.readcenter_x()) * (p.readpoint_x() - c.readcenter_x()) +
(p.readpoint_y() - c.readcenter_y()) * (p.readpoint_y() - c.readcenter_y()) > c.readm_R() * c.readm_R())
{
cout << "点在圆外" << endl;
}
else if ((p.readpoint_x() - c.readcenter_x()) * (p.readpoint_x() - c.readcenter_x()) +
(p.readpoint_y() - c.readcenter_y()) * (p.readpoint_y() - c.readcenter_y()) == c.readm_R() * c.readm_R())
{
cout << "点在圆上" << endl;
}
else
{
cout << "点在圆内" << endl;
}
}
int main()
{
point cp;
cp.settpoint_x(10);
cp.settpoint_y(0);
Circle c;
c.setm_R(10);
c.setcenter(cp);
point p;
p.setpoint_x(10);
p.setpoint_y(9);
isSame(c, p);
system("pause");
return 0;
}
核心2 怎么把一个类拆到不同的文件中:一个类拆成.h(头文件)和.cpp(源文件)
.h包含函数和变量的声明;
.cpp只包含具体的函数;
//要求:
//判断点与圆的关系
#include<iostream>
#include<string>
#include"point.h"
#include"Circle.h"
using namespace std;
void isSame(Circle c, point p)
{
if ((p.readpoint_x() - c.readcenter_x()) * (p.readpoint_x() - c.readcenter_x()) +
(p.readpoint_y() - c.readcenter_y()) * (p.readpoint_y() - c.readcenter_y()) > c.readm_R() * c.readm_R())
{
cout << "点在圆外" << endl;
}
else if ((p.readpoint_x() - c.readcenter_x()) * (p.readpoint_x() - c.readcenter_x()) +
(p.readpoint_y() - c.readcenter_y()) * (p.readpoint_y() - c.readcenter_y()) == c.readm_R() * c.readm_R())
{
cout << "点在圆上" << endl;
}
else
{
cout << "点在圆内" << endl;
}
}
int main()
{
point cp;
cp.setpoint_x(10);
cp.setpoint_y(0);
Circle c;
c.setm_R(10);
c.setcenter(cp);
point p;
p.setpoint_x(10);
p.setpoint_y(9);
isSame(c, p);
system("pause");
return 0;
}
point.h
#pragma once
#include<iostream>
using namespace std;
class point
{
public:
void setpoint_x(double thex);
double readpoint_x();
void setpoint_y(double they);
double readpoint_y();
private:
double point_x;
double point_y;
};
point.cpp
#include"point.h"
void point::setpoint_x(double thex)//point作用域下的成员函数
{
point_x = thex;
}
double point::readpoint_x()
{
return point_x;
}
void point::setpoint_y(double they)
{
point_y = they;
}
double point::readpoint_y()
{
return point_y;
}
Circle.h
#pragma once
#include<iostream>
#include"point.h"
using namespace std;
class Circle
{
public:
void setm_R(double R);
double readm_R();
void setcenter(point theP);
double readcenter_x();
double readcenter_y();
private://属性设置为私有
double m_R;
point p;
};
Circle.cpp
#include"Circle.h"
void Circle::setm_R(double R)
{
m_R = R;
}
double Circle::readm_R()
{
return m_R;
}
void Circle::setcenter(point theP)
{
p = theP;
}
double Circle::readcenter_x()
{
return p.readpoint_x();
}
double Circle::readcenter_y()
{
return p.readpoint_y();
}