Exercise: Simulating Ilkay Gundogan's Football Transfer Using constructors and destructors in C++
In this exercise, we will simulate the football transfer of Ilkay Gundogan using C++ constructors and destructors. We'll create a class called "FootballPlayer" that represents a player and use it to model Gundogan's transfer from Manchester City to Barcelona.
1. Your task is to modify the given code to include additional functionality and output. Follow the instructions below:
<h2>1.1 Modify the constructor of the <code>FootballPlayer</code> class to include an additional parameter called <code>oldClub</code>.</h2>
<p>Inside the constructor, display a message indicating that Gundogan has joined Manchester City from his old club. Use the <code>oldClub</code> parameter to complete the message. For example: "Ilkay Gundogan has joined Manchester City from [oldClub]!"</p>
<h2> 1.2 Update the destructor of the <code>Footba llPlayer</code> class to include an additional parameter called <code>newClub</code>.</h2>
<p>Inside the destructor, display a message indicating that Gundogan has left Manchester City and joined Barcelona. Use the <code>newClub</code> parameter to complete the message. For example: "Ilkay Gundogan has left Manchester City and joined [newClub]!"</p>
<h2> 1.3 In the <code>main()</code> function, create an instance of the <code>FootballPlayer</code> class for Ilkay Gundogan. Provide the name of Gundogan's old club as the second argument.</h2>
<p>After creating the instance, display a message stating that the transfer has been completed successfully.</p>
<h2> 1.4 Compile and run the modified code.</h2>
2. Solution
// Solution
include <iostream>
include <string>
class FootballPlayer {
public:
std::string name;
// Constructor
FootballPlayer(std::string playerName, std::string oldClub) {
name = playerName;
std::cout << name << " has joined Manchester City from " << oldClub << "!\n";
}
// Destructor
~FootballPlayer() {
std::cout << name << " has left Manchester City and joined Barcelona!\n";
}
};
int main() {
{
FootballPlayer player("Ilkay Gundogan", "Borussia Dortmund");
}
std::cout << "Transfer completed successfully!\n";
return 0;
}
Explanation
In this code, we simulate the football transfer of Ilkay Gundogan using C++ constructors and destructors. Here's a breakdown of the modified code:
-
We define the class
FootballPlayer
with a public member variablename
to store the player's name. -
The constructor of
FootballPlayer
is updated to take two parameters:playerName
andoldClub
. TheplayerName
parameter represents the name of the player being transferred (in this case, "Ilkay Gundogan"), and theoldClub
parameter represents the name of Gundogan's old club.Inside the constructor, we use the
name
andoldClub
parameters to display a message indicating that Gundogan has joined Manchester City from his old club. The message is printed usingstd::cout
and includes the player's name and the name of the old club. -
The destructor of
FootballPlayer
remains unchanged. It is responsible for displaying a message indicating that Gundogan has left Manchester City and joined Barcelona. -
In the
main()
function, we create an instance of theFootballPlayer
class for Ilkay Gundogan by calling the constructor. We provide two arguments: "Ilkay Gundogan" as the player's name and "Borussia Dortmund" as the name of his old club.After creating the instance, we display a message stating that the transfer has been completed successfully using
std::cout
. -
Finally, the program returns 0 to indicate successful execution.
When the modified code is compiled and run, it will produce the following output:
Ilkay Gundogan has joined Manchester City from Borussia Dortmund!
Ilkay Gundogan has left Manchester City and joined Barcelona!
Transfer completed successfully!
Top comments (0)