Witam, zabrałem się za pisanie dosyć banalnego projektu aczkolwiek stanąłem w martwym punkcie. Potrzebuje stworzyć dla mojego projektu wyrzucanie wyjątków, aczkolwiek poradniki w sieci nie dają mi zbyt wiele bo jestem początkującym w temacie c++ i cały kod niewiele mi mówi jak to wszystko działa. Czy byłby ktoś w stanie stworzyć dla mojego przykładu wyrzucanie wyjątków abym mógł zobaczyć jak to wszystko działa? Chciałbym aby wyrzucało mi wyjątek(i np dawało dowolną godzinę do programu,, lub komentarz ze zły zakres i ponowne wpisanie), kiedy ktoś poda godzinę poza zakresem [0-23].
Oto mój kod który napisałem do tej pory.
#include <iostream>
using namespace std;
class Time{
private:
int hours;
int minutes;
public:
Time(){
hours = 5;
minutes =55;
}
Time(int h, int m){
hours = h;
minutes = m;
}
int get_hours() const{
return hours;
}
int get_minutes() const{
return minutes;
}
//setters
void set_hours(int h){
hours = h;
}
void set_minutes(int m){
minutes = m;
}
Time operator-(const Time& t2)
{
Time result;
int all_minutes= (this->hours-t2.hours )*60 + this->minutes- t2.minutes;
result.hours = all_minutes/60;
result.minutes = all_minutes%60;
return result;
}
friend ostream& operator<<(ostream& os, const Time& t){
os<<"(" <<t.hours<<":";
os<<t.minutes<<")";
return os;
}
};
int main(){
Time t1(5,55);
Time t2(2,25);
cout<<t1<<endl;
cout<<t2<<endl;
Time t3 = t1-t2;
cout<<"Time difference between t1 & t2 = "<<t3<<endl;
}