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(); /*測試*/
};