Pierwsze "podejście" do klas

0

Witam,

To znów ja, po moim ostatnim poscie tutaj miałem przerwę w nauce programowania, aż do wczoraj. Spowodowana była natłokiem nauki oraz brakiem czasu, ale mniejsza.

Postanowiłem zrobić coś z klasami, jakiegoś praktycznego zastosowania(do klas) to nie ma, ale chciałem sprawdzić czy w ogóle to zadziała. Nie bawiłem się w konstruktory i destruktory bo "program" jest za mały.

main:

#include "pch.h"
#include "question.h"
#include <iostream>


using namespace std; 

int main()
{
	
	Event e1;
	e1.what();
	


}

question.h

#pragma once
#include <iostream>
using namespace std;

class Event
{
	public:
		string day, month, year, hour, minutes;
		string name;
		string whats;
		void what();
		void load();
		void show();
};

question.cpp

#include "pch.h"
#include "question.h"
#include <iostream>
#include <Windows.h>
#include <fstream>
#include <string>
#include <stdio.h>
#include <conio.h>
#include <cstdlib>


using namespace std;

void Event::load()
{

	cout << "\n";
	cout << "Podaj nazwe wydarzenia: "; cin >> name;
	cout << "Podaj dzien: "; cin >> day;
	cout << "Podaj miesiac: "; cin >> month;
	cout << "Podaj rok: "; cin >> year;
	cout << "Podaj godzine: "; cin >> hour;
	cout << "Podaj minuty: "; cin >> minutes;
	ofstream file;
	file.open("C:\\Users\\Zimny\\PROGRAMY\\Program1\\quiz\\" + name + ".txt");
	file << name << endl;
	file << day << endl;
	file << month << endl;
	file << year << endl;
	file << hour << endl;
	file << minutes << endl;
	file.close();
}
void Event::show()
{
	ifstream get("C:\\Users\\Zimny\\PROGRAMY\\Program1\\quiz\\" + name + ".txt");
	getline(get, name);
	getline(get, day);
	getline(get, month);
	getline(get, year);
	getline(get, hour);
	getline(get, minutes);





	cout << endl << name << " " << day << "." << month << "." << year << " " << hour << ":" << minutes << endl;
}
void Event::what()
{
	cout << "Chcesz dodac czy sprawdzic wydarzenie?" << endl;
	cout << "Wpisz S/D:"; cin >> whats;
	while (whats == "S")
	{
		cout << "Podaj nazwe wydarzenia: "; cin >> rename;
		ifstream get("C:\\Users\\Zimny\\PROGRAMY\\Program1\\quiz\\" + rename + ".txt");
		getline(get, name);
		while (rename != name)
		{
			cout << "Wydarzenie o takiej nazwie nie istnieje.\n";
			Event::what();
		}
		Event::show();
		Event::what();
	}
	
	while (whats == "D")
	{
		Event::load();
		Event::what();
	}

}
0

No dobrze, ale jaki jest problem?

1
  1. using namespace std; w headerze jest złe, bo propaguje się do każdego pliku, który używa Twojego headera.
  2. Dlaczego plik z klasą Event nazywa się question? Troche to mylące.
  3. Myląca jest też nazwa klasy - Event sugerowałoby jakąś prostą strukture reprezentującą - no właśnie - pewne zdarzenie. Natomiast Twój Event potrafi operować na plikach, wczytywać dane od usera i je interpretować. Prawdopodobnie najlepszym rozwiązaniem byłoby wydzielenie tych operacji do osobnej klasy a w strukturze Event zostawienie tylko danych reprezentujących to zdarzenie.

1 użytkowników online, w tym zalogowanych: 0, gości: 1