Vote count:
0
So i have stuck in dead end and looking for some help. So i have 3 classes 2 of them contains another class object. So the problem is that i want if I want to start over and load data from files and process it. But problem starts then i need to clear up the old data. So as far as i think i should use destructor. But i'm not sure that calling one to clear one class data it would clear anothers too. So here is my MainForm header file:
public ref class MainForm : public System::Windows::Forms::Form
{
public:
MainForm(void)
{
/* ----------- Constructor ----------- */
InitializeComponent();
info = new Firma;
infoc = new Firma;
/* ----------- End ----------- */
}
/* ----------- Public ----------- */
/* ----------- End ----------- */
protected:
/* ----------- Protected ----------- */
/* ----------- End ----------- */
~MainForm()
{
if (components)
{
/* ----------- Destructor ----------- */
delete components;
delete info;
delete infoc;
/* ----------- End ----------- */
}
}
private: System::Windows::Forms::MenuStrip^ menuStrip1;
protected:
private: System::Windows::Forms::ToolStripMenuItem^ failasToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ atidarytiFailaToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ issaugotiFailaToolStripMenuItem;
private: System::Windows::Forms::ToolStripSeparator^ toolStripSeparator1;
private: System::Windows::Forms::ToolStripMenuItem^ baigtiToolStripMenuItem;
private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
private: System::Windows::Forms::SaveFileDialog^ saveFileDialog1;
private: System::Windows::Forms::RichTextBox^ richTextBox1;
private:
/* ----------- Private ----------- */
Firma *info, *infoc;
And here should initialize to clear data
private: System::Void naujasToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
{
atidarytiFailaToolStripMenuItem->Enabled = true;
richTextBox2->Text = "Failu nera";
info
}
Here is the info of classes:
#pragma once
#include "Taksi.h"
#include <string>
#include <sstream>
#include <iomanip>
class Firma
{
public:
static const int Cd = 5;
private:
Taksi *Fi;
int n;
int nmax;
void KeistiMasyvoDydi(int kiek);
public:
Firma(int nmx = 0);
~Firma();
void DetiTaksi(Taksi & taks);
Taksi ImtiTaksi(int ind) { return Fi[ind]; }
int ImtiN() { return n; }
void Eksplot(string & mar, int & rid);
string Spalva();
void Check();
};
Methods:
#include "Firma.h"
#include <string>
#include <sstream>
#include <iomanip>
#include <iostream>
using namespace std;
Firma::Firma(int nmx) : Fi(nullptr), n(0), nmax(nmx)
{
if (nmax > 0)
{
Fi = new Taksi[nmax];
}
}
Firma::~Firma()
{
if (Fi)
{
delete[] Fi;
n = 0;
}
}
void Firma::KeistiMasyvoDydi(int kiek)
{
if (kiek > nmax)
{
Taksi *Fnaujas = new Taksi[kiek];
for (int i = 0; i < n; i++)
{
Fnaujas[i] = Fi[i];
}
delete[] Fi;
Fi = Fnaujas;
nmax = kiek;
}
else
{
if (kiek < nmax)
{
Taksi *Fnaujas = new Taksi[kiek];
for (int i = 0; i < kiek; i++)
{
Fnaujas[i] = Fi[i];
}
delete[] Fi;
Fi = Fnaujas;
n = nmax = kiek;
}
}
}
void Firma::DetiTaksi(Taksi & taks)
{
if (n == nmax)
{
KeistiMasyvoDydi(n + Cd);
}
Fi[n] = taks;
n++;
}
void Firma::Check()
{
cout << n << endl;
}
void Firma::Eksplot(string & mar, int & rid)
{
mar = "";
rid = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < Fi[i].ImtiN(); j++)
{
if (Fi[i].ImtiTaksista(j).ImtiRid() > rid)
{
rid = Fi[i].ImtiTaksista(j).ImtiRid();
mar = Fi[i].ImtiTaksista(j).ImtiMar();
}
}
}
}
string Firma::Spalva()
{
stringstream eil;
bool yra;
for (int i = 0; i < n; i++)
{
for (int k = 0; k < Fi[i].ImtiN(); k++)
{
yra = false;
for (int j = 0; j < n; j++)
{
for (int l = 0; l < Fi[j].ImtiN() && j != i; l++)
{
if (Fi[i].ImtiTaksista(k).ImtiSpa() == Fi[j].ImtiTaksista(l).ImtiSpa())
{
yra = true;
}
}
}
if (yra == false)
{
eil << left << setw(12) << Fi[i].ImtiP() << " - " << right << setw(14) << Fi[i].ImtiTaksista(k).ImtiSpa() << endl;
}
}
}
return eil.str();
}
And here's another one and strange here that i cant use destructor here or it needs to be a some sort of specific one or something
#pragma once
#include <string>
#include <sstream>
#include <iomanip>
#include "Taksistas.h"
class Taksi
{
public:
static const int Cd = 5;
private:
Taksistas *Ta;
int n;
int nmax;
string pav;
void KeistiMasyvoDydi(int kiek);
public:
Taksi(int nmx = 0);
~Taksi();
void DetiTaksista(Taksistas & taks, string pv);
Taksistas ImtiTaksista(int ind) { return Ta[ind]; }
int ImtiN() { return n; }
string ImtiP() { return pav; }
string Spauzdinti();
};
Methods:
#include "Taksi.h"
#include <string>
#include <sstream>
#include <iomanip>
#include <iostream>
using namespace std;
Taksi::Taksi(int nmx) : Ta(nullptr), n(0), nmax(nmx), pav("")
{
if (nmax > 0)
{
Ta = new Taksistas[nmax];
}
}
Taksi::~Taksi()
{
if (Ta)
{
delete[] Ta;
n = 0;
}
}
void Taksi::KeistiMasyvoDydi(int kiek)
{
if (kiek > nmax)
{
Taksistas *Tnaujas = new Taksistas[kiek];
for (int i = 0; i < n; i++)
{
Tnaujas[i] = Ta[i];
}
delete[] Ta;
Ta = Tnaujas;
nmax = kiek;
}
else
{
if (kiek < nmax)
{
Taksistas *Tnaujas = new Taksistas[kiek];
for (int i = 0; i < kiek; i++)
{
Tnaujas[i] = Ta[i];
}
delete[] Ta;
Ta = Tnaujas;
n = nmax = kiek;
}
}
}
void Taksi::DetiTaksista(Taksistas & taks, string pv)
{
if (n == nmax)
{
KeistiMasyvoDydi(n + Cd);
}
Ta[n] = taks;
pav = pv;
n++;
}
string Taksi::Spauzdinti()
{
stringstream eil;
eil << " " << pav << "\n\n";
for (int i = 0; i < n; i++)
{
eil << Ta[i].Spauzdinti();
}
return eil.str();
}
And the last one which data the data:
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
class Taksistas
{
private:
string pav; // Vairuotojo pavarde.
string mar; // Masinos marke.
string num; // Masinos valstybinis numeris.
string dat; // Masinos pagaminimo metai.
string spa; // Masinos spalva.
int rid; // Rida (tuks. km) per metus.
public:
Taksistas() : pav(""), mar(""), num(""), dat(""), spa(""), rid(0) {}
Taksistas(string pv, string ma, string nu, string da, string sp, int ri);
void DetiTaks(string pav, string mar, string num, string dat, string spa, int rid);
string ImtiPav() { return pav; }
string ImtiMar() { return mar; }
string ImtiNum() { return dat; }
string ImtiDat() { return dat; }
string ImtiSpa() { return spa; }
int ImtiRid() { return rid; }
string Spauzdinti();
};
So as far as i understand that i dont need to clean all data i just need to reset objects length to 0 and then new data is filled it would be overwrite. Not sure if it would be free up to use for other programs and overwrite.
Deleting object in MVSD GUI c++
Aucun commentaire:
Enregistrer un commentaire