Exercise: Simulating Ilkay Gundogan's Football Transfer Using constructors and destructors in C++
Tags: programming, cpp, tutorial, beginners
Published: 2023-07-01 18:57:13


  


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

  <p>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.</p>

  <h2> 1. Your task is to modify the given code to include additional functionality and output. Follow the instructions below:</h2>

  
    
      <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>
    
  <h1>2. Solution</h1>

  <pre><code class="cpp">
// Solution

#include &lt;iostream&gt;
#include &lt;string&gt;

class FootballPlayer {
public:
  std::string name;

  // Constructor
  FootballPlayer(std::string playerName, std::string oldClub) {
    name = playerName;
    std::cout &lt;&lt; name &lt;&lt; " has joined Manchester City from " &lt;&lt; oldClub &lt;&lt; "!\n";
  }

  // Destructor
  ~FootballPlayer() {
    std::cout &lt;&lt; name &lt;&lt; " has left Manchester City and joined Barcelona!\n";
  }
};

int main() {
  {
    FootballPlayer player("Ilkay Gundogan", "Borussia Dortmund");
  }
  std::cout &lt;&lt; "Transfer completed successfully!\n";

  return 0;
}
  </code></pre>

  <h2>Explanation</h2>

  <p>In this code, we simulate the football transfer of Ilkay Gundogan using C++ constructors and destructors. Here's a breakdown of the modified code:</p>

  <ol>
    <li>
      <p>We define the class <code>FootballPlayer</code> with a public member variable <code>name</code> to store the player's name.</p>
    </li>
    <li>
      <p>The constructor of <code>FootballPlayer</code> is updated to take two parameters: <code>playerName</code> and <code>oldClub</code>. The <code>playerName</code> parameter represents the name of the player being transferred (in this case, "Ilkay Gundogan"), and the <code>oldClub</code> parameter represents the name of Gundogan's old club.</p>
      <p>Inside the constructor, we use the <code>name</code> and <code>oldClub</code> parameters to display a message indicating that Gundogan has joined Manchester City from his old club. The message is printed using <code>std::cout</code> and includes the player's name and the name of the old club.</p>
    </li>
    <li>
      <p>The destructor of <code>FootballPlayer</code> remains unchanged. It is responsible for displaying a message indicating that Gundogan has left Manchester City and joined Barcelona.</p>
    </li>
    <li>
      <p>In the <code>main()</code> function, we create an instance of the <code>FootballPlayer</code> 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.</p>
      <p>After creating the instance, we display a message stating that the transfer has been completed successfully using <code>std::cout</code>.</p>
    </li>
    <li>
      <p>Finally, the program returns 0 to indicate successful execution.</p>
    </li>
  </ol>

  <p>When the modified code is compiled and run, it will produce the following output:</p>

  <pre><code>
Ilkay Gundogan has joined Manchester City from Borussia Dortmund!
Ilkay Gundogan has left Manchester City and joined Barcelona!
Transfer completed successfully!
  </code></pre>