将类的声明和实现分开的实例

定义了两个类,一个是MyPoint,一个是MyCircle;
在测试程序的项目中添加类;

1.MyPoint.h//类的声明

#pragma once
class MyPoint
{
private:
int x0;
int y0;
public:
void setxy(int _x0, int _y0);

int getx();

int gety();
};

2.MyPoint.cpp//类的实现,首先应该包含声明

#include "MyPoint.h"
void MyPoint::setxy(int _x0, int _y0)
{
x0 = _x0;
y0 = _y0;
}
int MyPoint::getx()
{
return x0;
}
int MyPoint::gety()
{
return y0;
}

3.MyCircle.h//声明类MyCircle

#pragma once
#include"MyPoint.h"//若有使用另外的类的内容就需要包含此类的头文件

class MyCircle
{
public:
void setxyr(int _x1, int _y1, int _r);

void judge(int x0, int y0);
void judge(MyPoint &p);


private:
int x1;
int y1;
int r;

};


4.MyCircle.cpp//类的实现

#include "MyCircle.h"
#include<iostream>
using namespace std;


void MyCircle::setxyr(int _x1, int _y1, int _r)
{
x1 = _x1;
y1 = _y1;
r = _r;
}


void MyCircle::judge(int x0, int y0)
{
int a = (x1 - x0)*(x1 - x0) + (y1 - y0)*(y1 - y0) - r*r;
if (a > 0)
{
cout << "it is out  of the circle";
}
if (a < 0)
{
cout << "it is in the circle";
}
}
void MyCircle::judge(MyPoint &p)
{
int a = (x1 - p.getx())*(x1 - p.getx()) + (y1 - p.gety())*(y1 - p.gety()) - r*r;
if (a > 0)
{
cout << "it is out  of the circle";
}
if (a < 0)
{
cout << "it is in the circle";
}
}

5.MainClass.cpp//主程序实现

#include<iostream>
#include"MyPoint.h"
#include"MyCircle.h"


using namespace std;
void main()
{
MyPoint myp;
myp.setxy(1, 1);
MyCircle c1;
c1.setxyr(2, 2, 3);
c1.judge(myp);


system("pause");
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值