首页 > 代码库 > 【C/C++学院】(5)面向对象编程练习--h和cpp分开编写
【C/C++学院】(5)面向对象编程练习--h和cpp分开编写
抽象一个点,一个圆,并判断点与圆的关系。(在圆内还是圆外)
h文件进行类的声明;
cpp文件为类的实现细节;主要注重的细节为:
=================================================================================================================================
工程代码如下:
//MyCircle.h #pragma once #include"MyPoint.h" class MyCircle { public: void setCir(double x, double y, double r); public: char * judge(double x, double y); char * judge(MyPoint &p); private: double x1, y1; //圆心坐标 double r1; //圆半径 };
//MyPoint.h #pragma once class MyPoint { private: double x0, y0; //点坐标 public: void setPoint(double x, double y); double getX0(); double getY0(); };
//MyCircle.cpp #include "MyCircle.h" void MyCircle::setCir(double x, double y, double r) { x1 = x; y1 = y; r1 = r; } char* MyCircle::judge(double x, double y) { double s = (x1 - x)*(x1 - x) + (y1 - y)*(y1 - y) - r1*r1; if (s > 0) { return "在圆外"; } else { return "在圆内"; } } char* MyCircle::judge(MyPoint &p) { double s = (x1 - p.getX0())*(x1 - p.getX0()) + (y1 - p.getY0())*(y1 - p.getY0()) - r1*r1; if (s > 0) { return "在圆外"; } else { return "在圆内"; } }
//MyPoint.cpp #include "MyPoint.h" void MyPoint::setPoint(double x, double y) { x0 = x; y0 = y; } double MyPoint::getX0() { return x0; } double MyPoint::getY0() { return y0; }
//MainTestClass.cpp #include "iostream" #include "MyCircle.h" #include "MyPoint.h" using namespace std; void main() { MyCircle c1; MyPoint p1; c1.setCir(2, 2, 3); p1.setPoint(1, 1); cout << c1.judge(p1.getX0(), p1.getY0()) << endl; cout << c1.judge(p1) << endl; system("pause"); }
【C/C++学院】(5)面向对象编程练习--h和cpp分开编写
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。