DEV Community

Abssi Franki
Abssi Franki

Posted on

Exercise: Simulating Ilkay Gundogan's Football Transfer Using constructors and destructors in C++

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>
Enter fullscreen mode Exit fullscreen mode

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:

  1. We define the class FootballPlayer with a public member variable name to store the player's name.

  2. The constructor of FootballPlayer is updated to take two parameters: playerName and oldClub. The playerName parameter represents the name of the player being transferred (in this case, "Ilkay Gundogan"), and the oldClub parameter represents the name of Gundogan's old club.

    Inside the constructor, we use the name and oldClub parameters to display a message indicating that Gundogan has joined Manchester City from his old club. The message is printed using std::cout and includes the player's name and the name of the old club.

  3. The destructor of FootballPlayer remains unchanged. It is responsible for displaying a message indicating that Gundogan has left Manchester City and joined Barcelona.

  4. In the main() function, we create an instance of the FootballPlayer 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.

  5. 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)