将类的定义放在头文件中,把成员函数的实现代码放在一个cpp文件中

写这种.h和.cpp文件分开的大程序,虽然对很多人来说很简单,对自己来说算是第一次吧,好好学C++,加油~

题目:定义Point类,由Point派生出Circle类,再由Circle派生出Cylinder类。将类的定义部分分别作为3个头文件,对他们的成员函数的定义分别作为3个源文件

1、Point.h文件

 1 #ifndef POINT_H2 #define POINT_H3 #include<iostream>    //头文件也需要包含这个4 using namespace std;5 6 class Point7 {8 protected:9     float x, y;
10 public:
11     Point(float a = 0, float b = 0);     //带默认参数的构造函数的声明
12     float getX();
13     float getY();
14     void setPoint(float, float);
15     friend ostream& operator<<(ostream &output, const Point &p);    //运算符重载
16
17 };
18
19 #endif

2、Circle.h文件

 1 #ifndef CIRCLE_H2 #define CIRCLE_H3 4 #include "Point.h"5 6 class Circle:public Point7 {8 public:9     Circle(float a = 0, float b = 0, float r = 0);
10     float getR();
11     void setR(float r);
12     float area();
13     friend ostream& operator<<(ostream &output, Circle &c);    //运算符重载
14 protected:
15     float radius;
16 };
17
18 #endif

3、Cylinder.h文件

 1 #ifndef CYLINDER_H2 #define CYLINDER_H3 4 #include "Circle.h"5 6 class Cylinder :public Circle7 {8 public:9     Cylinder(float a = 0, float b = 0, float r = 0, float h = 0);
10     void setH(float h);
11     float getH();
12     float area();
13     float volume();
14     friend ostream& operator<<(ostream &, Cylinder &);
15 private:
16     float height;
17 };
18
19 #endif

4、Point.cpp文件

 1 #include<iostream>2 #include"Point.h"3 using namespace std;4 5 Point::Point(float a, float b)6 {7     x = a;8     y = b;9 }
10
11 float Point::getX()
12 {
13     return x;
14 }
15 float Point::getY()
16 {
17     return y;
18 }
19 void Point::setPoint(float a, float b)
20 {
21     x = a;
22     y = b;
23 }
24
25 ostream& operator<<(ostream &output, const Point &p)    //重载运算符“<<”
26 {
27     output << "(" << p.x << "," << p.y << ")" << endl;
28     return output;
29 }

5、Circle.cpp文件

 1 #include<iostream>2 #include"Circle.h"3 using namespace std;4 5 Circle::Circle(float a, float b, float r) :Point(a, b), radius(r) { }6 7 float Circle::getR()8 {9     return radius;
10 }
11
12 void Circle::setR(float r)
13 {
14     radius = r;
15 }
16
17 float Circle::area()
18 {
19     return 3.14*radius*radius;
20 }
21
22 ostream& operator<<(ostream &output, Circle &c)    //运算符重载
23 {
24     output << "圆心:(" << c.x << "," << c.y << ")   半径:" << c.radius << "     面积:" << c.area() << endl;
25     return output;
26 }

  6、Cylinder.cpp文件

 1 #include<iostream>2 #include"Cylinder.h"3 using namespace std;4 5 Cylinder::Cylinder(float a, float b, float r, float h) :Circle(a, b, r), height(h) { }6 7 void Cylinder::setH(float h)8 {9     height = h;
10 }
11
12 float Cylinder::getH()
13 {
14     return height;
15 }
16
17 float Cylinder::area()
18 {
19     return 2 * Circle::area() + 2 * 3.14*radius*height;
20 }
21
22 float Cylinder::volume()
23 {
24     return Circle::area()*height;
25 }
26
27 ostream& operator<<(ostream &output, Cylinder &cy)    //cy.area() 属于同名覆盖
28 {
29     output << "圆心:(" << cy.x << "," << cy.y << ")   半径:" << cy.radius << "  高:" << cy.height << "\n                  面积: " << cy.area() << "   体积:" << cy.volume() << endl;
30     return output;
31 }

7、main函数

 1 //习题6.12 //#include<iostream>3 //#include"Point.h"4 //#include"Circle.h"5 #include"Cylinder.h"6 //using namespace std;7 8 int main()9 {
10     Cylinder cy1(3, 3, 5, 6);
11     cout << "original cylinder:" << cy1 << endl;
12     cy1.setPoint(-2, -2);
13     cy1.setR(8);
14     cy1.setH(10);
15     cout << "new cylinder: " << cy1 << endl;
16     Point &p1 = cy1;
17     cout << "as a point: " << p1 << endl;
18     Circle &c1 = cy1;
19     cout << "as a circle: " << c1 << endl;
20     return 0;
21 }

运行结果:

总结:

1、在写头文件是要注意写

#ifndef POINT_H
#define POINT_H

……

#endif

可以避免多次包含同一头文件,防止重定义

2、基本的头文件要写在.h文件中

将类的定义放在头文件中,把成员函数的实现代码放在一个cpp文件中相关推荐

  1. C++声明、定义、类的定义、头文件作用、头文件重复引用

    转载至:点击打开链接 C++声明.定义.类的定义.头文件作用.头文件重复引用,不具名空间 转自:http://www.cnblogs.com/rocketfan/archive/2009/10/02/ ...

  2. C++模板学习02(类模板)(类模板语法、类模板与函数模板的区别、类模板中的成员函数创建时机、类模板对象做函数参数、类模板与继承、类模板成员函数类外实现、类模板分文件编写、类模板与友元)

    C++引用详情(引用的基本语法,注意事项,做函数的参数以及引用的本质,常量引用) 函数高级C++(函数的默认参数,函数的占位参数,函数重载的基本语法以及注意事项) C++类和对象-封装(属性和行为作为 ...

  3. C++ 在一个cpp文件中使用另一个cpp文件中定义的函数

    C++ 在一个cpp文件中 使用另一个cpp文件中定义的函数 建立一个console项目 头文件 cpp文件 main.cpp 建立一个console项目 以dev为例,在[文件][新建][项目],新 ...

  4. C++中模板类中的成员函数以及模板函数在类外定义

    在C++中,类中的成员函数可以在类外完成定义,从而显得类中的成员函数看起来简洁明了.但是模板类里的成员函数和模板函数与普通的成员函数在类外定义不同. 先定义一个模板类以及成员函数和模板函数: 接下我们 ...

  5. include.cpp(main函数的cpp文件)文件中包含另一个.cpp文件的错误及原因

    当一个main函数的cpp文件包含另一个cpp文件,使用#include "data.cpp"  是不可以的,例如下面 include.cpp #include <stdio ...

  6. 关于.cpp文件包含另一个.cpp文件出错的原因以及解决办法

    今天打开自己以前写代码突然觉得在main函数中如果要用的很多自己实现的函数,如果把它们都放在main.cpp文件中太臃肿了.调试起来特别不方便.能不能把这些自己实现的函数放到另外一个文件中呢? 可是又 ...

  7. c语言的结构体能存放函数吗,在C语言结构体中添加成员函数

    我们在使用C语言的结构体时,经常都是只定义几个成员变量,而学过面向对象的人应该知道,我们定义类时,不只是定义了成员变量,还定义了成员方法,而类的结构和结构体非常的相似,所以,为什么不想想如何在C语言结 ...

  8. R语言广义线性模型函数GLM、R中有几种logistic回归扩展和变异、robust包中的glmRob函数鲁棒logistic回归、ms包中的lrm函数拟合序数逻辑回归

    R语言广义线性模型函数GLM.glm函数构建逻辑回归模型(Logistic regression).R中有几种logistic回归扩展和变异.robust包中的glmRob函数鲁棒logistic回归 ...

  9. 总结C++中取成员函数地址的几种方法

    这里, 我整理了4种C++中取成员函数地址的方法, 第1,2,4种整理于网上的方法, 第3种cdecl_cast是我自己想到的. 其中, 第4种(汇编)的方法不能在VC6上编译通过. 推荐使用第1,2 ...

最新文章

  1. 一块V100运行上千个智能体、数千个环境,这个「曲率引擎」框架实现RL百倍提速...
  2. b转换成mb php_攻防世界之WEB篇,php反序列化漏洞,网络安全入门篇
  3. python定义一个类和子类_Python定义类、定义子类以及super()函数的使用
  4. ThinkPad系列笔记本待机恢复后,双击我的电脑始终运行而无法打开
  5. 电子老鼠闯迷宫pascal解题程序
  6. 接口测试工具postman(六)添加变量(参数化)
  7. 11_模型的选择与调优,交叉验证,超参数搜索-网格搜索sklearn.model_selection.GridSearchCV
  8. lvs主从服务器转发风暴(广播风暴、大流量)
  9. MariaDB数据库日志
  10. 教你如何用Harbor 私有镜像仓库搭建
  11. GridView里的一点小功能:截取多余字符、改变鼠标经过行的样式
  12. 韩顺平php视频笔记77 抽象类vs接口 关键字final const
  13. java中方法未定义_java - Java SE中的未定义方法错误 - 堆栈内存溢出
  14. [转载] 使用python完成冒泡排序_使用python实现-冒泡排序
  15. 计算机/程序员常用英语(持续添加,包括一些简写)
  16. CCNA认证考试介绍
  17. java毕业设计开题报告SSM图书馆预约占座系统
  18. 高分辨率扫描出来的图片有摩尔纹_文档扫描仪选购指南:扫描仪哪个牌子比较好?...
  19. 百度网盘正版免费扩容教程
  20. 网易邮箱账号注册twitter开发者api权限,无法收到确认邮件

热门文章

  1. Android MVC模式在android系统中的体现
  2. SQL Server Reporting Services(简称SSRS)
  3. RuntimeError: Bool type is not supported by dlpack
  4. 编写你的第一个 Django 应用,第 3 部分
  5. 面试题 16.18. Pattern Matching LCCI
  6. 【Linux系统编程应用】Linux音频编程实战(一)
  7. java内存图解_图解JAVA内存模型(JMM:JAVA Memory Model)
  8. 初学者linux和ubuntu,linux初学者也必须知道的几个ubuntu最基础命令
  9. matlab验证对称三相电路,不对称三相电路中,中线的电流为()。 A.0 B. C. D....
  10. 每天一道LeetCode-----计算二叉树的最大深度及最小深度,判断二叉树是否是高度平衡二叉树