C++之类的分文件编写(保姆级别教程)

题目要求:

(1)文件划分:类定义文件(*.h),类实现文件(*.cpp),类使用文件(,cpp,主函数文件)。
(2)每个类的设计单独放在1个(
.h)文件中,工程共包括5个文件。
(3)使用条件编译指令以免文件多次包含的情况。
(4)在Dev C++或VC中建立工程:文件-新建-项-(Basic—EmptyProject—C++项目)-新建文件夹Line,项目名称为Line,将所有文件保存在名为Line的文件夹中。VC建立过程是:文件—>新建—>工程—>Win32 Console Application。

下面详细介绍此例题的分文件编写过程

1、首先,未拆写的源码是这样的

#include <iostream>
using namespace std;
#include <cmath>

class Point {
public:
Point(int xx = 0, int yy = 0) {
x = xx;
y = yy;
}
Point(Point& p);
int getX() {
return x;
}
int getY() {
return y;
}
private:
int x, y;
};
Point::Point(Point& p) {
x = p.x;
y = p.y;
cout << "Calling the copy construction of Point" << endl;
}
class Line {
public:
Line(Point xp1, Point xp2);
Line(Line& l);
double getLen() {
return len;
}
private:
Point p1, p2;
double len;
};
Line::Line(Point xp1, Point xp2) :p1(xp1), p2(xp2) {
cout << "Calling constructior of Line" << endl;
double x = static_cast<double> (p1.getX() - p2.getX());
double y = static_cast<double> (p1.getY() - p2.getY());
len = sqrt(x * x + y * y);
}
Line::Line(Line& l) :p1(l.p1), p2(l.p2) {
cout << "Calling constructior of Line" << endl;
len = l.len;
}

int main() {
Point myp1(1, 1), myp2(4, 5);
Line line(myp1, myp2);
Line line2(line);
cout << "The length of the line is";
cout << line.getLen() << endl;
cout << "The length of the line is";
cout << line2.getLen() << endl;
return 0;
}

 上面是未拆写的源码

下面我们对其进行拆分

首先在拆分前:要知道几点内容和基本要求。

1、程序拆分的目的就是为了简洁方便自己,使得代码条理清晰,简洁易懂

2、要明白在头文件(.h)和源文件(.cpp)中分别要做什么事,不同的代码要放到那个位置。(下边详细介绍)

3、注意引用的时候,使用双""进行引用

4、要是一下子做不出来,可以尝试先拆分一个,而不是立刻地拆分开所有,因为有可能一个错误你好改正,好找,但是你一次性弄完,再去找错,那真的会很难搞很难搞。

下面开始整活!!!

1、先打开vs,创建一个项目,命名为Line的项目。

 2、在源文件中创建一个文件,命名为源.cpp,将原本的源代码全部复刻到这个源.cpp的源文件中

3、此题目涉及到的是类,那么我们就来拆分类

3.1记住题目要求,头文件(.h)中进行类的声明(其实在实际中也是这样的规律,头文件中进行声明)

3.1源文件(.cpp)中进行类的定义(就是对类的具体函数什么的,进行一个说明和功能的实现)

首先讲头文件(.h),此题目设计两个类,我们创建两个头文件,分别命名为(Line.h和Point.h)

首先对Line类进行说明和编辑,将源文件中的Line类的所有内容都拷贝一份(快捷键Ctrl+c),然后将源文件中的有关Line类的相关内容全部注释掉。(后面在对Point类的处理中也要这样做)

然后重点来了 !!!

在Line.h的头文件中进行类的声明,类的声明还包括类中的相关成员函数以及构造函数、复制构造函数,所以还要对他们进行声明

具体应该是下面这个样子

注意我划横线的那几行,不管是成员函数还是构造函数,声明的格式必须是那个样子。 

然后,......其实上图第三行可以不写,因为Point 类可以作为Line类的函数里的成员

Line.h(头文件)源码

#include <iostream>
#pragma once
#include "Point.h"
using namespace std;
class Line {
public:
	Line(Point xp1, Point xp2);
	Line(Line& l);
	double getLen();

private:
	Point p1, p2;
	double len;
};

 同样的道理,将Point.h类也这样处理

 

 Point.h(头文件)源码

#include <iostream>
#pragma once
using namespace std;
class Point {
public:
	Point(int xx, int yy);
	Point(Point& p);
	int getX();

	int getY();
private:
	int x, y;
};

OK,头文件完成了,下面进行源文件(.cpp)的编写

1、先是看Line.pp的源文件

 在这个地方,参照成员函数,类的构造函数在类外定义的方法,定义完成就好

还要使用"Point.h"去引用Point.h头文件,因为类的定义在Point.h里,所以要引用,不然编译器会蒙b

源文件(Line.cpp)的源代码

#include <iostream>
#include "Point.h"
using namespace std;
#include "Line.h"
double::Line:: getLen() {
return len;
	}
Line::Line(Point xp1, Point xp2) :p1(xp1), p2(xp2) {
	cout << "Calling constructior of Line" << endl;
	double x = static_cast<double> (p1.getX() - p2.getX());
	double y = static_cast<double> (p1.getY() - p2.getY());
	len = sqrt(x * x + y * y);
}
Line::Line(Line &l) :p1(l.p1), p2(l.p2) {
	cout << "Calling constructior of Line" << endl;
	len = l.len;
}

2、下面看Point.cpp的源文件

 源文件(Point.cpp)的源代码

#include <iostream>
#include "Point.h"
using namespace std;
Point::Point(int xx = 0, int yy = 0) {
		x = xx;
		y = yy;
	}
int::Point::getX() {
	return x;
}
int::Point::getY() {
	return y;
}
Point::Point(Point& p) {
	x = p.x;
	y = p.y;
	cout << "Calling the copy construction of Point" << endl;
}

下面是源.cpp,也就是拥有全部代码的那点

源.cpp的源代码

#include <iostream>
#include <cmath>
#include "Point.h"
#include "Line.h"
using namespace std;
int main() {
	Point myp1(1, 1), myp2(4, 5);
	Line line(myp1, myp2);
	Line line2(line);
	cout << "The length of the line is";
	cout << line.getLen() << endl;
	cout << "The length of the line is";
	cout << line2.getLen() << endl;
	return 0;
}

这就是分文件编写的全部步骤

运行结果:

如果对你有一定帮助,请点个小小的赞。   万分感激!!!

  • 69
    点赞
  • 202
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值