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

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

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

1、Point.h文件

 1 #ifndef POINT_H
 2 #define POINT_H
 3 #include<iostream>    //头文件也需要包含这个
 4 using namespace std;
 5
 6 class Point
 7 {
 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_H
 2 #define CIRCLE_H
 3
 4 #include "Point.h"
 5
 6 class Circle:public Point
 7 {
 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_H
 2 #define CYLINDER_H
 3
 4 #include "Circle.h"
 5
 6 class Cylinder :public Circle
 7 {
 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.1
 2 //#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文件中

时间: 2024-07-29 23:32:06

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

【C++】笔记一:Microsoft Visual Studio 2010软件的安装与建立第一个cpp文件

笔记一:Microsoft Visual Studio 2010软件的安装与建立第一个cpp文件 我学习C++使用软件为Microsoft Visual Studio 2010. 首先,软件的安装包 链接:https://pan.baidu.com/s/1kW3ChL1 密码:lg9p 下载软件并安装不多说. 新建项目 第一步,打开Microsoft Visual Studio 2010 第二步,依次选择[文件]--[新建]--[项目] 第三步,选择[Visual C++]-- [Win32控制

继承的基本概念: (1)Java不支持多继承,也就是说子类至多只能有一个父类。 (2)子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法。 (3)子类中定义的成员变量和父类中定义的成员变量相同时,则父类中的成员变量不能被继承。 (4)子类中定义的成员方法,并且这个方法的名字返回类型,以及参数个数和类型与父类的某个成员方法完全相同,则父类的成员方法不能被继承。 分析以上程

继承的基本概念: (1)Java不支持多继承,也就是说子类至多只能有一个父类. (2)子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法.(3)子类中定义的成员变量和父类中定义的成员变量相同时,则父类中的成员变量不能被继承.(4)子类中定义的成员方法,并且这个方法的名字返回类型,以及参数个数和类型与父类的某个成员方法完全相同,则父类的成员方法不能被继承. 分析以上程序示例,主要疑惑点是“子类继承父类的成员变量,父类对象是否会实例化?私有成员变量是否会被继承?被继承的成员变量

浅析在类模版中构建成员函数时,使用memcpy产生的副作用

一般情况下我们在对类模版中的成员函数进行构建时会经常对一些数据进行复制拷贝,而通常情况下我们都不提倡用memcpy进行拷贝,因为在类模版中所传进来的类型可以是内置类型也可以是非内置类型,除非你在成员函数中使用memcpy前进行类型萃取,否则它所带来的副作用的后果也是很可怕的.memcpy在对内置类型可正常拷贝,而在对非内置类型拷贝时会出现浅拷贝的现象. 下面我们可以通过一个简单的顺序表程序来分析memcpy对非内置类型所产生的副作用: #include<iostream> #include&l

c++ 类中模版成员函数

C++函数模版与类模版. template <class T> void SwapFunction(T &first, T &second){ }//函数模版 template <class T>//类模版 class CTemplate{ public: void SWap(T &first, T &second){ } }; #include <iostream> class Single{ public: static Single

拷贝构造,深度拷贝,关于delete和default相关的操作,explicit,类赋初值,构造函数和析构函数,成员函数和内联函数,关于内存存储,默认参数,静态函数和普通函数,const函数,友元

 1.拷贝构造 //拷贝构造的规则,有两种方式实现初始化. //1.一个是通过在后面:a(x),b(y)的方式实现初始化. //2.第二种初始化的方式是直接在构造方法里面实现初始化. 案例如下: #include<iostream> //如果声明已经定义,边不会生成 class classA { private: int a; int b; public: //拷贝构造的规则,有两种方式实现初始化 //1.一个是通过在后面:a(x),b(y)的方式实现初始化 //2.第二种初始化的方式是直

【原创】如何将多个工作簿中相同格式的工作表合并到一个工作表中

如何将多个工作簿中相同格式的工作表合并到一个工作表中 Sub Books2Sheets() '定义对话框变量 Application.ScreenUpdating = False Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) '新建一个工作簿 Dim newwb As Workbook Set newwb = Workbooks.Add With fd If .Show = -1 The

VBA 把一个工作簿中的表的数据传递到另一个工作簿中

Private Sub CommandButton2_Click() For Z = 2 To Sheet2.[b65536].End(3).Row Next Application.ScreenUpdating = False Dim j As Integer Dim souce As Worksheet Dim target As Workbook Set souce = ThisWorkbook.Worksheets("正式表") Set target = Workbooks.O

c++ 一个cpp文件如何调用另一个cpp文件已经定义的类?我不想重复定义

文件test1.cpp有类class A;文件test2.cpp有类class B.如在test2.cpp中想用A:#include "test1.cpp" 当然一般的做法是将类的声明放在.h文件里,定义放在.cpp文件,然后可以直接在B类中保存一个A类的指针,那样即可实现使用A类了. 原文地址:https://www.cnblogs.com/ryanzheng/p/8455031.html

Java继承类中static成员函数的重写

在java中,static成员函数是否可以被重写呢? 结论是,你可以在子类中重写一个static函数,但是这个函数并不能像正常的非static函数那样运行. 也就是说,虽然你可以定义一个重写函数,但是该函数没有多态特性.让我们测试一下: 1 class testClass1{ 2 static void SMothod(){ 3 System.out.println("static in testClass1"); 4 } 5 } 6 class testClass2 extends