面向对象编程之.h和.cpp文件分开编写
来源:互联网 发布:淘宝五金店铺介绍 编辑:程序博客网 时间:2024/05/17 22:38
对于一个小程序,一般不需要编写头文件,但是对于一个复杂的大项目,模块化编写程序,便于理解,且容易下手,将问题分解成一小块一小块,逐个击破:
抽象一个点,一个圆,并判断点与圆的关系。(在圆内还是圆外)
h文件进行类的声明;
cpp文件为类的实现细节;主要注重的细节为:
在类外定义成员函数;
使用域作用符;
工程代码如下:
//MyCircle.h #pragma once #include"MyPoint.h" class MyCircle { public: void setCir(double x, double y, double r); public: charchar * judge(double x, double y); charchar * 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"); }
0 0
- 面向对象编程之.h和.cpp文件分开编写
- 【C/C++学院】(5)面向对象编程练习--h和cpp分开编写
- C++模板中声明和定义是否可以分开存放在.h和.cpp文件中
- C++之*.h和*.cpp文件详解
- MFC框架分开.h和.CPP的意义
- cpp文件和.h文件
- 告别cpp&h:hpp文件编写心得
- 告别cpp&h:hpp文件编写心得
- 告别cpp&h:hpp文件编写心得
- 告别cpp&h:hpp文件编写心得
- 告别cpp&h:hpp文件编写心得
- 【cpp面向对象编程学习笔记】(二)
- MFC中如何导入已经编写好的.cpp和.h文件
- .h文件和.cpp文件怎么写?
- 关于.h文件和.cpp文件
- 复数类_所有函数都写在类的外部(h和cpp分开)
- 模板类:函数实现(.cpp)和函数声明(.h)分开的问题
- 初建cpp工程(.h文件和.cpp文件具体内容)
- Ubuntu默认防火墙安装、启用、配置、端口、查看状态相关信息
- Python标准库 - time module
- 让log中的关键词过滤的酷炫
- C语言第十一篇:循环累加
- iOS导航条颜色色差的问题
- 面向对象编程之.h和.cpp文件分开编写
- 回归只是最开始的选择
- Spring boot 入门 实例
- Java中 set、 list、 array、 Map相互转换
- Openwrt无线管理工具/命令
- ActionBarDrawerToggle和ToolBar结合使用
- myBatis学习笔记
- python得到所有在leetcode上Accepted的代码(一)
- solution Of Pat 1052. Linked List Sorting (25)