C++/多文件编程/类和对象/Circle和Point类举例

//main.cpp
/*This program is to create a circle class and realize the function
of judging the position relationship between two circles
*/

#include
#include “point.h”
#include “circle.h”
using namespace std;

//class Point{//the class of point
//public:
// int setX(int x){
// myX=x;
// }
// int setY(int y){
// myY=y;
// }
// int getX() const {
// return myX;
// }
// int getY() const{
// return myY;
// }
//
//private:
// int myX;
// int myY;
//};
//class Circle{//the class of circle
//public:
// int setR(int r){//set the radius
// myR=r;
// }
// int getR() const{//get the radius
// return myR;
// }
// int setCenter(Point center){//set the center of the circle
// myCenter=center;
// }
// Point getCenter() const{//get the center of the circle and use const to make it unmodifiable
// return myCenter;
// }
//
//private:
// int myR;//radius
// Point myCenter;//customize a class and instantiate an object named myCenter
//};

void isInCircle(Circle &c,Point &p){
int disdanceCirclePoint=(c.getCenter().getX()-p.getX())(c.getCenter().getX()-c.getCenter().getX())+
(c.getCenter().getY()-p.getY())
(c.getCenter().getY()-p.getY());//calculate the disdance between circle and point

int radiusSqure=c.getR()*c.getR();//calculate the radius of a square

if(disdanceCirclePoint == radiusSqure){//judge the relationship between circle and point and cout the result
    cout<<"point on the circle"<<endl;
}else if(disdanceCirclePoint>radiusSqure){
    cout<<"point outside the circle"<<endl;
}else{
    cout<<"point in the circle"<<endl;
}

}

int main()
{ //create the object of class Circle and class Point
Circle circleA;
Point pointP,circleCenterA;

//initialization data of circleA and pointP
circleCenterA.setX(3);
circleCenterA.setY(2);
circleA.setCenter(circleCenterA);

circleA.setR(5);
pointP.setX(3);
pointP.setY(8);

isInCircle(circleA,pointP);
return 0;

}

//circle.cpp
#include “circle.h”
int Circle::setR(int r){//set the radius
myR=r;
}
int Circle::getR() const{//get the radius
return myR;
}
int Circle::setCenter(Point center){//set the center of the circle
myCenter=center;
}
Point Circle::getCenter() const{//get the center of the circle and use const to make it unmodifiable
return myCenter;
}

//point.cpp
#include “point.h”
int Point::setX(int x){
myX=x;
}
int Point::setY(int y){
myY=y;
}
int Point::getX() const {
return myX;
}
int Point::getY() const{
return myY;
}

//circle.h
#pragma once
#include
#include “point.h”//这里用到了point类的实例化对象,所以要包含类point的头文件
using namespace std;

class Circle{//the class of circle
public:
int setR(int r);//set the radius
int getR() const;//get the radius
int setCenter(Point center);//set the center of the circle
Point getCenter() const;//get the center of the circle and use const to make it unmodifiable

private:
int myR;//radius
Point myCenter;//customize a class and instantiate an object named myCenter
};

//point.h
#pragma once
#include
using namespace std;

class Point{//the class of point
public:
int setX(int x);

int setY(int y);

int getX() const ;

int getY() const;

private:
int myX;
int myY;
};

1、类.h文件加#pragram once
#include
using namespace std;
以及省略掉具体函数实现的类
注意:如果这个类中还调用了其他的类的实例化对象,则需要将这个类包含进来

2、类.cpp文件中加#include “类.h”
存放函数的具体实现

3、源代码的头文件将封装好的类的头文件包含进来

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值