Use a member initialization list to make the TeamInfo constructor initialize the vector listOfPointsInSeason with a size of 6. Note: Including a vector in an initialization list causes that vector's constructor to be called with the value in the parens. #include #include using namespace std; class TeamInfo { public: TeamInfo(); void PrintGamesInSeason() const; private: vector listOfPointsInSeason; };
Use a member initialization list to make the TeamInfo constructor initialize the
#include <iostream>
#include <vector>
using namespace std;
class TeamInfo {
public:
TeamInfo();
void PrintGamesInSeason() const;
private:
vector<int> listOfPointsInSeason;
};
TeamInfo::TeamInfo() : /* Your code goes here */ {
}
void TeamInfo::PrintGamesInSeason() const {
cout << "There are " << listOfPointsInSeason.size() << " games in a season." << endl;
}
int main() {
TeamInfo myTeam;
myTeam.PrintGamesInSeason();
return 0;
}
![### Learning Objective: Constructor Initializer Lists in C++
In this exercise, you will learn how to use a member initialization list to initialize a vector within a class constructor. Specifically, we will initialize the vector `listOfPointsInSeason` with a size of 6.
#### Code Example
```cpp
#include <iostream>
#include <vector>
using namespace std;
class TeamInfo {
public:
TeamInfo();
void PrintGamesInSeason() const;
private:
vector<int> listOfPointsInSeason;
};
TeamInfo::TeamInfo() : listOfPointsInSeason(6) {
/* Your code goes here */
}
void TeamInfo::PrintGamesInSeason() const {
cout << "There are " << listOfPointsInSeason.size() << " games in a season." << endl;
}
int main() {
TeamInfo myTeam;
myTeam.PrintGamesInSeason();
return 0;
}
```
#### Explanation
- **Member Initialization List**: The constructor of the class `TeamInfo` uses a member initialization list to initialize `listOfPointsInSeason` with a size of 6. This is done by calling the vector's constructor with the size value inside the parentheses.
- **Constructor**: `TeamInfo::TeamInfo() : listOfPointsInSeason(6) {}` initializes `listOfPointsInSeason` to be a vector with 6 default-initialized integers.
- **PrintGamesInSeason Function**: This function prints the size of the vector, which indicates the number of games in a season.
- **`main` Function**: It creates an instance of `TeamInfo` and calls `PrintGamesInSeason` to demonstrate the initialized vector's size.
Explore how initializing class members with an initializer list can streamline your object construction and ensure efficiency in your programs.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fb7324842-f341-42f1-b59c-5e882e20e060%2Fb73f2803-224e-4489-9de2-21bf3c29744b%2Fixazvbn_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)