c++ 头文件 实现类 位置
时间: 2023-08-11 20:20:47 浏览: 54
在 C++ 中,通常将类的声明放在头文件中,将类的实现放在源文件中。这样做的好处是,头文件中只包含类的声明,而不包含实现,可以避免头文件重复包含导致的编译错误,同时也方便其他源文件包含该头文件以使用该类。
例如,假设有一个名为 `MyClass` 的类,那么通常会将其声明放在 `MyClass.h` 头文件中,如下所示:
```
// MyClass.h
#ifndef MYCLASS_H
#define MYCLASS_H
class MyClass {
public:
void foo();
void bar();
private:
int m_data;
};
#endif
```
然后,将类的实现放在 `MyClass.cpp` 源文件中,如下所示:
```
// MyClass.cpp
#include "MyClass.h"
void MyClass::foo() {
// TODO
}
void MyClass::bar() {
// TODO
}
```
最后,在需要使用 `MyClass` 类的源文件中,只需要包含 `MyClass.h` 头文件即可。
需要注意的是,头文件中应该只包含类的声明、宏定义、常量等内容,不要在头文件中包含大量的代码实现,以免增加编译时间和编译后的二进制文件大小。
相关问题
c++头文件中声明类及定义
在C++中,头文件用于声明和定义类。声明类的目的是为了在其他文件中使用该类,而不需要暴露类的具体实现细节。头文件通常包含类的成员函数和成员变量的声明。
头文件中声明类时,一般包括类的关键字(class)和类名。例如,声明一个名为Person的类可以写成以下形式:
```cpp
class Person {
// 类的成员变量和成员函数的声明
};
```
在头文件中,我们可以声明类的成员变量和成员函数。例如,下面的例子声明了一个Person类,其中包含了两个私有成员变量(name和age),以及两个公有成员函数(setName和setAge):
```cpp
class Person {
private:
string name; // 姓名
int age; // 年龄
public:
void setName(string name); // 设置姓名
void setAge(int age); // 设置年龄
};
```
在头文件中,我们还可以定义类的成员函数。定义成员函数是为了给函数提供具体的实现。例如,我们可以在头文件中定义上述Person类的成员函数:
```cpp
class Person {
private:
string name; // 姓名
int age; // 年龄
public:
void setName(string name) {
this->name = name;
}
void setAge(int age) {
this->age = age;
}
};
```
头文件中的类声明可以在多个源文件中使用,只需要包含该头文件即可。例如,在其他源文件中使用Person类时,只需要包含头文件即可:
```cpp
#include "person.h"
int main() {
Person p;
p.setName("Tom");
p.setAge(20);
return 0;
}
```
总而言之,头文件主要用于声明和定义类,以便在其他文件中使用。通过头文件,我们可以在不暴露类的具体实现细节的情况下使用类的成员变量和成员函数。
c++实现分数类fraction利用头文件
以下是使用头文件实现分数类fraction的C++代码示例:
fraction.h头文件:
```c++
#ifndef FRACTION_H
#define FRACTION_H
class Fraction {
public:
Fraction(int n = 0, int d = 1); // 构造函数
Fraction(const Fraction& other); // 复制构造函数
Fraction& operator=(const Fraction& other); // 复制赋值运算符
~Fraction(); // 析构函数
int numerator() const; // 获取分子
int denominator() const; // 获取分母
Fraction operator+(const Fraction& other) const; // 加法运算符
Fraction operator-(const Fraction& other) const; // 减法运算符
Fraction operator*(const Fraction& other) const; // 乘法运算符
Fraction operator/(const Fraction& other) const; // 除法运算符
bool operator==(const Fraction& other) const; // 等于运算符
bool operator!=(const Fraction& other) const; // 不等于运算符
bool operator<(const Fraction& other) const; // 小于运算符
bool operator>(const Fraction& other) const; // 大于运算符
bool operator<=(const Fraction& other) const; // 小于等于运算符
bool operator>=(const Fraction& other) const; // 大于等于运算符
private:
int m_numerator; // 分子
int m_denominator; // 分母
void reduce(); // 约分
};
#endif // FRACTION_H
```
fraction.cpp实现文件:
```c++
#include "fraction.h"
#include <algorithm>
Fraction::Fraction(int n, int d) : m_numerator(n), m_denominator(d) {
reduce();
}
Fraction::Fraction(const Fraction& other) : m_numerator(other.m_numerator), m_denominator(other.m_denominator) {
reduce();
}
Fraction& Fraction::operator=(const Fraction& other) {
if (this != &other) {
m_numerator = other.m_numerator;
m_denominator = other.m_denominator;
reduce();
}
return *this;
}
Fraction::~Fraction() {}
int Fraction::numerator() const {
return m_numerator;
}
int Fraction::denominator() const {
return m_denominator;
}
Fraction Fraction::operator+(const Fraction& other) const {
int n = m_numerator * other.m_denominator + other.m_numerator * m_denominator;
int d = m_denominator * other.m_denominator;
return Fraction(n, d);
}
Fraction Fraction::operator-(const Fraction& other) const {
int n = m_numerator * other.m_denominator - other.m_numerator * m_denominator;
int d = m_denominator * other.m_denominator;
return Fraction(n, d);
}
Fraction Fraction::operator*(const Fraction& other) const {
int n = m_numerator * other.m_numerator;
int d = m_denominator * other.m_denominator;
return Fraction(n, d);
}
Fraction Fraction::operator/(const Fraction& other) const {
int n = m_numerator * other.m_denominator;
int d = m_denominator * other.m_numerator;
return Fraction(n, d);
}
bool Fraction::operator==(const Fraction& other) const {
return m_numerator == other.m_numerator && m_denominator == other.m_denominator;
}
bool Fraction::operator!=(const Fraction& other) const {
return !(*this == other);
}
bool Fraction::operator<(const Fraction& other) const {
return m_numerator * other.m_denominator < other.m_numerator * m_denominator;
}
bool Fraction::operator>(const Fraction& other) const {
return m_numerator * other.m_denominator > other.m_numerator * m_denominator;
}
bool Fraction::operator<=(const Fraction& other) const {
return *this < other || *this == other;
}
bool Fraction::operator>=(const Fraction& other) const {
return *this > other || *this == other;
}
void Fraction::reduce() {
if (m_denominator == 0) {
throw "Denominator cannot be zero.";
}
if (m_denominator < 0) {
m_numerator = -m_numerator;
m_denominator = -m_denominator;
}
int gcd = std::gcd(m_numerator, m_denominator);
m_numerator /= gcd;
m_denominator /= gcd;
}
```
使用示例:
```c++
#include <iostream>
#include "fraction.h"
int main() {
Fraction f1(2, 3);
Fraction f2(1, 4);
Fraction f3 = f1 + f2;
std::cout << f1.numerator() << "/" << f1.denominator() << " + "
<< f2.numerator() << "/" << f2.denominator() << " = "
<< f3.numerator() << "/" << f3.denominator() << std::endl;
return 0;
}
```