创建 C++ 可重用头文件及其实现文件
可重用性是软件工程中最重要的概念之一。可重用性意味着开发可以在同一程序或不同程序中重用的代码。 C++ 允许通过继承、容器化、多态性和通用性实现可重用性。但是,还有另一种定义独立构建块的方法。这可以通过创建头文件和实现文件来实现。
Header files are the files that include the class declaration. The name of the class is generally the same as that of the header file. (For example, a LinkedList class will be stored inside a LinkedList.h header file)
On the other hand, the implementation file consists of the function definition of the class which was defined inside the header file. Generally, the file name is the name of the class, with a .cpp extension. (For example, a LinkedList class’s function definition will be stored inside a LinkedList.cpp header file)
现在,要创建在上述头文件中定义的类的对象,必须有一个 main()函数。但是等等,在哪里定义 main()函数,特别是哪个文件?
main函数在另一个文件中定义,称为驱动程序文件,或者在某些情况下,称为客户端文件。
示例:这里实现了complexNum类。它被分成两个文件。头文件的扩展名为.h并包含类定义。
头文件:
C++
// Header file complexNum.h
#ifndef COMPLEXNUM_H
#define COMPLEXNUM_H
class complexNum {
private:
int real;
int imaginary;
public:
// With default value,
// default constructor
complexNum(const int a = 0,
const int b = 0);
// setter function
void setNum(const int a,
const int b);
// Prints the complex number
// in the form real + i(imaginary),
// i->iota
void print() const;
// An overloaded operator to compare
// two complex number objects
bool operator==(const complexNum&);
};
#endif
C++
// Implementation file
// complexNum.cpp
#include "complexNum.h"
#include
using namespace std;
// A default constructor
complexNum::complexNum(int a,
int b)
{
real = a;
imaginary = b;
}
// A function to set values
void complexNum::setNum(const int a,
const int b)
{
real = a;
imaginary = b;
}
// A function to print the complex
// number in the form real +
// (imaginary)i, i->iota
void complexNum::print() const
{
cout << real << " + " << imaginary << "i" << endl;
}
// An overloaded operator to
// compare two complex Number
// objects
bool complexNum::operator==(const complexNum& obj)
{
if (this->real == obj.real && this->imaginary == obj.imaginary) {
return true;
}
return false;
}
C++
// Driver file to illustrate
// the implementation of
// complexNum.cpp file
#include "complexNum.h"
#include
using namespace std;
// Driver code
int main()
{
// Defines a complex number
// object (obj1 = 4 + 5i)
complexNum obj1(4, 5);
complexNum obj2;
// Defines a complex number
// object (obj2 = 3 + 4i)
obj2.setNum(3, 4);
// Prints the copmlex number
obj1.print();
obj2.print();
// Checks, if two complex
// number objects are equal or
// not
if (obj1 == obj2) {
cout << "Both the numbers are equal" << endl;
}
else {
cout << "Numbers are not equal" << endl;
}
return 0;
}
实施文件:
C++
// Implementation file
// complexNum.cpp
#include "complexNum.h"
#include
using namespace std;
// A default constructor
complexNum::complexNum(int a,
int b)
{
real = a;
imaginary = b;
}
// A function to set values
void complexNum::setNum(const int a,
const int b)
{
real = a;
imaginary = b;
}
// A function to print the complex
// number in the form real +
// (imaginary)i, i->iota
void complexNum::print() const
{
cout << real << " + " << imaginary << "i" << endl;
}
// An overloaded operator to
// compare two complex Number
// objects
bool complexNum::operator==(const complexNum& obj)
{
if (this->real == obj.real && this->imaginary == obj.imaginary) {
return true;
}
return false;
}
现在要检查正确性并实现上述complexNum类,需要一个驱动程序文件。下面是驱动文件:
C++
// Driver file to illustrate
// the implementation of
// complexNum.cpp file
#include "complexNum.h"
#include
using namespace std;
// Driver code
int main()
{
// Defines a complex number
// object (obj1 = 4 + 5i)
complexNum obj1(4, 5);
complexNum obj2;
// Defines a complex number
// object (obj2 = 3 + 4i)
obj2.setNum(3, 4);
// Prints the copmlex number
obj1.print();
obj2.print();
// Checks, if two complex
// number objects are equal or
// not
if (obj1 == obj2) {
cout << "Both the numbers are equal" << endl;
}
else {
cout << "Numbers are not equal" << endl;
}
return 0;
}
输出:
笔记:
标头、实现以及驱动程序文件应该在同一个文件夹中。否则,在包含语句中提供当前工作目录的链接。
头文件已经被程序员使用了,非常有用,并且在实现各种数据结构和算法时变得得心应手。例如,动态数组可以使用
在不同文件中存储类定义的优点:
- 继承可用于实现代码的可重用性,但其缺点是必须从同一文件内的类继承一个类。不能从不同的文件继承一个类。
- 但是,这个问题是通过使用头文件和实现文件解决的,因此使类可以重用。
- 如果类的实现没有改变,那么就不需要重新编译它。