1. Composition
1.1. Date
Define a class for Mate
that is described with name, age and gender.
Then, define a class Date
that keeps data for two mates.
In this class, implement a function bool isSuccess()
that should compute the success of the date.
The date is successful if the sum of sums of the ASCII values of the names of the mates is even number.
Also implement a function that will print the mates of the date in format:
Date between: [Name] [Age] [Gender] [Name] [Age] [Gender]
oop_av41_en.cpp
#include <iostream>
#include <cstring>
using namespace std;
enum gender {
male,
female,
other
};
class Mate {
private:
char name[100];
int age;
gender gen;
public:
Mate() {}
Mate(const char *n, int a, gender g) {
strcpy(name, n);
age = a;
gen = g;
}
int getNameNumber() {
int sum = 0;
char *n = name;
while(*n) {
sum += *n;
++n;
}
return sum;
}
};
class Date {
private:
Mate m1;
Mate m2;
public:
Date(Mate& _m1, Mate& _m2) {
m1 = _m1;
m2 = _m2;
}
bool isSuccess() {
return (m1.getNameNumber() + m2.getNameNumber()) % 2 == 0;
}
};
int main() {
Mate m1("Barby", 20, female);
Mate m2("Ken", 25, male);
Mate m3("Sam", 23, other);
Date date(m1, m2);
if(date.isSuccess()) {
cout << "The date was success, lets make another one..." << endl;
} else {
cout << "This did not work out, good bye!" << endl;
}
return 0;
}
1.2. Team
Define a class Team
that is described with name, year of founding and the city.
Define a class Game
that keeps information for the host and the guest (objects of class Team
), goals scored by the host and goals scored by the guest.
Define a function rematch
that as arguments accepts two objects of class Game
and checks if one of them is a rematch of the other.
A match is a rematch if the host of the first game is a guest in the second game, and vice versa.
If the match is a rematch, return the aggregate winner of the fixture (the one that scored more goals on aggregate).
oop_av42_en.cpp
#include <iostream>
#include <cstring>
using namespace std;
class Team {
private:
char name[100];
int yearFounded;
char city[100];
public:
Team(const char *name = "", const int yearFounded = 0,
const char *city = "") {
strcpy(this->name, name);
this->yearFounded = yearFounded;
strcpy(this->city, city);
}
char *getName() {
return name;
}
void print() {
cout << name << " " << yearFounded << endl;
}
};
class Game {
private:
Team host;
Team guest;
int hostGoals;
int guestGoals;
public:
Game(const Team host = Team(), const Team guest = Team(),
const int hostGoals = 0, const int guestGoals = 0) {
this->host = host;
this->guest = guest;
this->hostGoals = hostGoals;
this->guestGoals = guestGoals;
}
Team getHost() const {
return host;
}
Team getGuest() const {
return guest;
}
int getHostGoals() const {
return hostGoals;
}
int getGuestGoals() const {
return guestGoals;
}
};
Team winner(const Game game1, const Game game2) {
if (strcmp(game1.getHost().getName(), game2.getGuest().getName()) == 0 &&
strcmp(game2.getHost().getName(), game1.getGuest().getName()) == 0) {
int goalsHost = game1.getHostGoals() + game2.getGuestGoals();
int goalsGuest = game1.getGuestGoals() + game2.getHostGoals();
if (goalsHost >= goalsGuest) {
return game1.getHost();
} else {
return game1.getGuest();
}
}
return Team();
}
int main() {
Team t1("Barcelona", 1900, "Barcelona");
Team t2("Real Madrid", 1890, "Madrid");
Team t3("Elche", 1950, "Elche");
Game g1(t1, t2, 5, 0);
Game g2(t2, t1, 2, 2);
Game g3(t1, t3, 6, 2);
Team w = winner(g1, g2);
w.print();
return 0;
}