C++练习:设计一个圆形类(Circle),和一个点类(Point),计算点和圆的关系。 顺便熟悉一下分文件编写

简介: C++练习:设计一个圆形类(Circle),和一个点类(Point),计算点和圆的关系。 顺便熟悉一下分文件编写

以下为个人的分文件编写:


▪Code:

4e0ec8e537654516b039a0ae70421ada.png

Circle.h

#pragma once
#include <iostream>
#include "Point.h"
using namespace std;
class Circle {
public:
  Point get_center();
  void set_center(Point center1);
  double get_r();
  void set_r(double& r1);
private:
  Point center;
  double r;
};

Point.h

#pragma once
#include <iostream>
using namespace std;
class Point {
public:
  double getx();
  void setx(double& x1);
  double gety();
  void sety(double& y1);
private:
  double x;
  double y;
};

源.cpp

#include<iostream>
#include"Circle.h"
#include"Point.h"
#include<math.h>
using namespace std;
int get_distance(Point& p1, Point& p2);
void if_point_in_circle(double distance,double r);
int main() {
  double x, y, x1, y1, r;
  Point p1,center;
  Circle c1;
  cout<<"Please enter the Point's x and y coordinates." << endl;
  cin >> x >> y;
  p1.setx(x);
  p1.sety(y);
  cout << "Please enter the Circle's center and radius." << endl;
  cin >> x1 >> y1 >> r;
  center.setx(x1);
  center.sety(y1);
  c1.set_center(center);
  double distance = get_distance(center, p1);
  cout << distance << endl;
  if_point_in_circle(distance,r);
}
int get_distance(Point& p1,Point& p2) {
  int x1 = p1.getx();
  int y1 = p1.gety();
  int x2 = p2.getx();
  int y2 = p2.gety();
  double distance = sqrt(pow(x1-x2,2)+pow(y1-y2,2)); //包含头文件math.h,不懂可以百度
  return distance;
}
void if_point_in_circle(double distance,double r) {
  if (distance > r) {
    cout << "Point is out of the Circle." << endl;
  }
  else if (distance == r) {
    cout << "Point is on the Circle." << endl;
  }
  else {
    cout << "Point is in the Circle" << endl;
  }
}

Circle.cpp

#include "Circle.h"
Point Circle::get_center() {
  return center;
};
void Circle::set_center(Point center1) {
  center = center1;
};
double Circle::get_r() {
  return r;
};
void Circle::set_r(double& r1) { 
  r = r1;
};

Point.cpp

#include "Point.h"
double Point::getx() {
  return x;
}
void Point::setx(double& x1) {
  x = x1;
}
double Point::gety() {
  return y;
}
void Point::sety(double& y1) {
  y = y1;
}
相关文章
|
1天前
|
编译器 C语言 C++
c++初阶------类和对象(六大默认构造函数的揭破)-3
c++初阶------类和对象(六大默认构造函数的揭破)
|
1天前
|
编译器 C语言 C++
c++初阶------类和对象(六大默认构造函数的揭破)-2
c++初阶------类和对象(六大默认构造函数的揭破)
|
1天前
|
存储 编译器 C语言
c++初阶------类和对象(六大默认构造函数的揭破)-1
c++初阶------类和对象(六大默认构造函数的揭破)
|
1天前
|
存储 编译器 C语言
c++初阶-------类和对象-2
c++初阶-------类和对象
|
1天前
|
编译器 C语言 C++
c++初阶-------类和对象-1
c++初阶-------类和对象
|
2天前
|
存储 Java C++
【C++类和对象】探索static成员、友元以及内部类
【C++类和对象】探索static成员、友元以及内部类
|
2天前
|
安全 程序员 编译器
【C++类和对象】初始化列表与隐式类型转换
【C++类和对象】初始化列表与隐式类型转换
|
2天前
|
安全 编译器 C++
【C++类和对象】const成员函数及流插入提取
【C++类和对象】const成员函数及流插入提取
|
2天前
|
存储 C++
【C++类和对象】日期类的实现(下)
【C++类和对象】日期类的实现
|
2天前
|
编译器 C++
【C++类和对象】日期类的实现(上)
【C++类和对象】日期类的实现