Hello, I don't know how to output the map in class Running
code]
class Athlete
{
std::string name, country;
int year;
public:
Athlete();
Athlete(std::string _name, std::string _country, int _year);
std::string getName() const;
Athlete& setName(std::string _name);
std::string getCountry() const;
Athlete& setCountry(std:: string _country);
int getYear() const;
Athlete& setYear(int _year);
std::string toString() const;
bool operator<(const Athlete& second) const;
bool operator>(const Athlete& second) const;
friend std::ostream& operator<<(std::ostream& os, const Athlete& a);
friend std::istream& operator>>(std::istream& is, Athlete& a);
};
//that is cpp file
#include "Athlete.h"
#include<sstream>
Athlete::Athlete() :name("noname"), country("no"), year(0)
{
}
Athlete::Athlete(std::string _name,std::string _country, int _year)
{
name = _name;
country = _country;
year = _year;
}
std::string Athlete::getName() const
{
return name;
}
Athlete& Athlete::setName(std::string _name)
{
name = _name;
return *this;
}
std::string Athlete::getCountry() const
{
return country;
}
Athlete& Athlete::setCountry(std::string _country)
{
country = _country;
return *this;
}
int Athlete::getYear() const
{
return year;
}
Athlete& Athlete::setYear(int _year)
{
year = _year;
return *this;
}
std::string Athlete::toString() const
{
std::ostringstream os;
os.width(4);
os << name << " ";
os.width(4);
os <<country << " ";
os.width(4);
os << year << "g.";
return os.str();
}
bool Athlete::operator<(const Athlete& second) const
{
return name < second.name;
}
bool Athlete::operator>(const Athlete& second) const
{
return !(*this < second);
}
std::ostream& operator<<(std::ostream& os, const Athlete& a)
{
os << a.toString();
return os;
}
std::istream& operator>>(std::istream& is, Athlete& a)
{
is >> a.name >> a.country>>a.year;
return is;
}
[/code][
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
#include<iostream>
#include<sstream>
class Time
{
int hours;
int minutes;
int inMinutes() const;
void correctTime();
public:
Time();
Time(int _hours, int _minutes);
int getMinutes() const;
int getHours() const;
Time& setMinutes(int _min);
Time& setHours(int _hours);
std::string printTime() const;
friend std::ostream& operator<<(std::ostream& os, const Time& t);
friend std::istream& operator>>(std::istream& is, Time& t);
Time operator-(const Time& second) const;
bool operator<(const Time& second) const;
bool operator>(const Time& second) const;
bool operator<=(const Time& second) const;
bool operator>=(const Time& second) const;
bool operator==(const Time& second) const;
bool operator!=(const Time& second) const;
};
// cpp file
#include "Time.h"
#include<iostream>
int Time::inMinutes() const
{
return this->hours * 60 + this->minutes;
}
void Time::correctTime()
{
if (minutes >= 60) {
hours += minutes / 60;
minutes = minutes % 60;
}
}
Time::Time() :hours(0), minutes(0)
{
}
Time::Time(int _hours, int _min): hours(_hours), minutes(_min)
{
correctTime();
}
int Time::getMinutes() const
{
return minutes;
}
int Time::getHours() const
{
return hours;
}
Time& Time::setMinutes(int _min)
{
minutes = _min;
correctTime();
return *this;
}
Time& Time::setHours(int _hours)
{
hours = _hours;
return *this;
}
std::string Time::printTime() const
{
std::ostringstream sout;
if (hours < 10)
std::cout << "\nTime: 0" << this->getHours();
else
std::cout << "\nTime: " << this->getHours();
if (minutes < 10)
std::cout << ":0" << this->getMinutes() << std::endl;
else
std::cout << ":" << this->getMinutes() << std::endl;
return sout.str();
}
Time Time::operator-(const Time& second) const
{
Time res;
int difference = inMinutes() - second.inMinutes();
if (difference > 0) {
res = Time(0, difference);
}
else {
res = Time(0, -difference);
res.hours = -res.hours;
}
return res;
}
bool Time::operator<(const Time& second) const
{
return inMinutes() < second.inMinutes();
}
bool Time::operator>(const Time& second) const
{
return !(*this < second);
}
bool Time::operator<=(const Time& second) const
{
return inMinutes() <= second.inMinutes();
}
bool Time::operator>=(const Time& second) const
{
return inMinutes() >= second.inMinutes();
}
bool Time::operator==(const Time& second) const
{
return inMinutes() <= second.inMinutes();
}
bool Time::operator!=(const Time& second) const
{
return !(*this == second);
}
std::ostream& operator<<(std::ostream& os, const Time& t)
{
os << t.printTime();
return os;
}
std::istream& operator>>(std::istream& is, Time& t)
{
is >> t.hours >> t.minutes;
return is;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include "Athlete.h"
#include "Time.h"
#include <map>
#include <string>
#include <iostream>
class Running
{
int running_distance;
std::map<Athlete, Time> participants_time;
public:
Running();
Running( int _run_dist,std::map<Athlete, Time>_participants_time);
void addAthlete(Athlete a, Time t);
std::string toString() const;
};
//cpp file
#include "Running.h"
Running::Running()
{
}
Running::Running( int _run_dist, std::map<Athlete, Time>_participants_time):
running_distance(_run_dist),participants_time(_participants_time)
{
}
void Running::addAthlete(Athlete a, Time t)
{
participants_time.insert(std::pair<Athlete, Time>(a, t));
}
std::string Running::toString() const
{
for (const auto& pt : participants_time) {
std::cout << pt.first << " e s vreme: " << pt.second << std::endl;
}
}
#include<iostream>
#include "Time.h"
#include "Athlete.h"
#include "Running.h"
int main() {
Athlete a{ "Vasil", "Belgia", 1982 };
Time t{ 1, 20 };
Running r{ 200,std::map<Athlete,Time> {a,t} };
}
|