Day16.2021.12.6

案例:点和圆的关系

两个类,circle和point,三个关系,圆内圆上圆外

 #include"methodState.h"
 #include <math.h>
 /*点圆关系*/
 class Point {
 private:
     double x;
     double y;
 public:
     double getX() {
         return this->x;
     }
     double getY() {
         return this->y;
     }
     void setX(double x) {
         this->x = x;
     }
     void setY(double y) {
         this->y = y;
     }
 };
 class Circle {
 private:
     Point center;
     double r;
 public:
     Point getCenter() {
         return this->center;
     }
     double getR() {
         return this->r;
     }
     void setCenter(Point center) {
         this->center = center;
     }
     void setR(double r) {
         this->r = r;
     }
 };
 ​
 void pointCircleRelation(Point& p,Circle& c) {
     double distance = pow(c.getCenter().getX() - p.getX(),2)+pow(c.getCenter().getY() - p.getY(),2);
     double r = pow(c.getR(),2);
     if (distance > r) cout << "点在圆外" << endl;
     else if (distance == r) cout << "点在圆上" << endl;
     else cout << "点在圆内" << endl;
 }
 int main() {
 ​
     Point p1;
     p1.setX(2);
     p1.setY(0);
     Point p2;
     p2.setX(2);
     p2.setY(2);
     Circle c;
     c.setR(2);
     c.setCenter(p2);
     pointCircleRelation(p1, c);
 ​
 ​
     system("pause");
     return 0;
 }

 

把点类和圆类写成分文件

声明.h文件

 #pragma once   //防止重复包含
 #include<iostream>
 using namespace std;
 #include <math.h>class Point {
 private:
     double x;
     double y;
 public:
     double getX();
     double getY();
     void setX(double x);
     void setY(double y);
 };
 class Circle {
 private:
     Point center;
     double r;
 public:
     Point getCenter();
     double getR();
     void setCenter(Point center);
     void setR(double r);
 };
 ​
 void pointCircleRelation(Point& p, Circle& c);
 

 

定义.cpp文件

 #include"methodState.h"
 //shift+Tab整体缩进
 double Point::getX() {//没有类的属性,但需要加作用域Point::
     return this->x;
 }
 double Point::getY() {
     return this->y;
 }
 void Point::setX(double x) {
     this->x = x;
 }
 void Point::setY(double y) {
     this->y = y;
 }
 Point Circle::getCenter() {
     return this->center;
 }
 double Circle::getR() {
     return this->r;
 }
 void Circle::setCenter(Point center) {
     this->center = center;
 }
 void Circle::setR(double r) {
     this->r = r;
 }
 ​
 void pointCircleRelation(Point& p,Circle& c) {
     double distance = pow(c.getCenter().getX() - p.getX(),2)+pow(c.getCenter().getY() - p.getY(),2);
     double r = pow(c.getR(),2);
     if (distance > r) cout << "点在圆外" << endl;
     else if (distance == r) cout << "点在圆上" << endl;
     else cout << "点在圆内" << endl;
 }

 

main方法

 int main() {
     Point p1;
     p1.setX(2);
     p1.setY(0);
     Point p2;
     p2.setX(2);
     p2.setY(2);
     Circle c;
     c.setR(2);
     c.setCenter(p2);
     pointCircleRelation(p1, c);
     system("pause");
     return 0;
 }

 

构造函数,析构函数

 #include"methodState.h"/*构造函数,析构函数*/
 //编译器自动调用,完成初始化和清理,若没手动写,就自动调用空的
 //构造函数可重载
 //析构函数在类名前加~,不可以重载
 class Student {
 public:
     string name;
     string id;
     Student() { 
         cout << "调用了无参构造" << endl; 
     }
     Student(string name,string id) {
         cout << "调用了有参构造" << endl;
         this->name = name;
         this->id = id;
     }
     ~Student() {
         cout << "调用了析构函数" << endl;
     }
 };
 void test() {
     Student stu1;
     Student stu2("20200101", "xiaoming");
 }
 int main() {
     //局部变量存在栈中,函数执行完自动释放,所以立即看到析构函数
     cout << endl << "test():";
     test();
     //main方法中的局部变量要等主函数执行完才执行析构函数,中间pause了,所以要回车一下才执行析构函数
     cout << "main:";
     Student stu1;
     Student stu2("20200101", "xiaoming");
     
     system("pause");
     return 0;
 }

 

image-20211206150636357

P106结束

来源:b站黑马

posted on 2021-12-06 15:12  托马斯源  阅读(47)  评论(0编辑  收藏  举报