1. 编译环境
Visual Studio 2019
win10
2. 代码
注意:
(1)这里用了static 修饰,所以必须在类外声明,没声明则不能使用
static 成员变量属于类,不属于某个具体的对象,即使创建多个对象,也只为 static 成员分配一份内存,所有对象使用的都是这份内存中的数据。当某个对象修改了 static 成员,也会影响到其他对象。
(2)private成员
在类的内部, public、protected 、private成员,可以互相访问的,没有访问权限的限制
在类的外部,只能通过对象访问成员,并且通过对象只能访问 public 属性的成员,不能访问 private、protected 属性的成员。
(3)
#include <fstream>
用于文件输出,比如ofstream
using namespace std; 简单来说包含绝大部分C++的库
(4)构造函数和析构函数
有一种特殊的成员函数,它的名字和类名相同,没有返回值,不需要用户显式调用(用户也不能调用),而是在创建对象时自动执行。这种特殊的成员函数就是构造函数
构造函数是可以重载的。
析构函数,也是一种特殊的成员函数,没有返回值,不需要程序员显式调用(程序员也没法显式调用),而是在销毁对象时自动执行。构造函数的名字和类名相同,而析构函数的名字是在类名前面加一个~
符号。
(5).cpp文件
#include "fout_test.h"
//using std::string;
string GNSS_Receiver::Aid_Doppler_Info_str = "Aid_Doppler_Info.txt";
GNSS_Receiver::GNSS_Receiver()
{
uint8_t i;
string tempstr;
#ifdef Aid_Doppler_Info_out
for (i = 0; i < Channel_Count; i++)
{
tempstr = "GPS" + to_string(i) + Aid_Doppler_Info_str;
fout_Aid_Doppler_Info[i].open(tempstr);
}
#endif
}
GNSS_Receiver::~GNSS_Receiver()
{
uint8_t i;
/*close*/
#ifdef Aid_Doppler_Info_out
for (i = 0; i < Channel_Count; i++)
fout_Aid_Doppler_Info[i].close();
#endif
}
/*输出数据到文件*/
void GNSS_Receiver::Fout_Data(uint32_t num)
{
char i = 0;
uint32_t j;
#ifdef Aid_Doppler_Info_out
for (j = 0; j < num; j++)
{
for (i = 0; i < Channel_Count; i++)
{
fout_Aid_Doppler_Info[i] << "well" << " "
<< (int)i << " "
<< "hello wolrd" << endl;
}
}
#endif
cout << "Output File Successful" << endl;
}
/*输出处理*/
void GNSS_Receiver::GNSS_Test()
{
Fout_Data(0xFF);
}
int main(void)
{
GNSS_Receiver* GNSS_Test_File = new GNSS_Receiver;
GNSS_Test_File->GNSS_Test();
}
(6).h文件
#pragma once
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
#define Aid_Doppler_Info_out 1
#define Channel_Count 3
class GNSS_Receiver
{
public:
GNSS_Receiver();
~GNSS_Receiver();
private:
static string Aid_Doppler_Info_str;
ofstream fout_Aid_Doppler_Info[Channel_Count];
void Fout_Data(uint32_t num); /*输出数据次数*/
public:
void GNSS_Test(); /*测试*/
};