C++ programming task. main.cc file: #include #include #include #include "plane.h" int main() {   std::vector weights{3.2, 4.7, 2.1, 5.5, 9.8, 7.4, 1.6, 9.3};   std::cout << "Printing out all the weights: " << std::endl;   // ======= YOUR CODE HERE ========   // 1. Using an iterator, print out all the elements in   //    the weights vector on one line, separated by spaces.   //    Hint: see the README for the for loop syntax using   //    an iterator, .begin(), and .end()   // ==========================   std::cout << std::endl;   std::map abbrevs{{"AL", "Alabama"},                                              {"CA", "California"},                                              {"GA", "Georgia"},                                              {"TX", "Texas"}};   std::map populations{       {"CA", 39.2}, {"GA", 10.8}, {"AL", 5.1}, {"TX", 29.5}};   std::cout << "\nPrinting out the state populations: " << std::endl;   // ========= YOUR CODE HERE =========   // 2. Using an iterator, print out each state's population   //    on a new line, in the format:   //      "Population of Alabama: 5.1 million"   //      "Population of California: 39.2 million"   //      ... and so on   //   //    * abbrevs maps from the state abbreviation to the   //      full state name.   //    * populations maps from the state abbreviation to   //      to the population (in millions)   //    Use the abbrevs map to retrieve the full state name   //    to print out, while iterating over the populations   //    map.   // =======================   std::cout << std::endl;   // 3. Implement the constructors of the Plane class, in   //    "plane.h". Refer to the README or the comments in   //    "plane.h" for instructions.   // ======= YOUR CODE HERE ========   // 4. Create an empty vector of Plane objects called `fleet`.   // ==========================   // =========== YOUR CODE HERE =======   // 5. Create a Plane `p1` instantiated with the default   //       constructor. Add `p1` to the `fleet` vector.   // =========================   // ======= YOUR CODE HERE ====   // 6. Use the constructor overload to create a Plane `p2`   //       with 150 seats, 75 passengers, and destination   //       "New York City". Add `p2` to the `fleet` vector.   // ==================   // Uncomment these lines of code after completing #3-6.   // Plane p3(220, 220, "Atlanta");   // Plane p4(75, 75, "Guatemala City");   // Plane p5(125, 94, "Medellin");   // ====== YOUR CODE HERE ====   // 7. Add `p3`, `p4`, and `p5` to the `fleet` vector.   // ==========================   // ======= YOUR CODE HERE =======   // 8. Using an iterator, print out all the flights in   //    the `fleet` vector, only if the flights are full.   //    Hint: see the README for the expected format.   // ========================= plane.cc file   #include "plane.h" // ============ YOUR CODE HERE ========== // This implementation file (plane.cc) is where you should implement // the member functions declared in the header (plane.h), only // if you didn't implement them inline within plane.h. // // Remember to specify the name of the class with :: in this format: //     MyClassName::MyFunction() { //        ... //     } // to tell the compiler that each function belongs to the Plane class. // =============================================   plane.h file   #include class Plane {  public:   // ========== YOUR CODE HERE ==========   // Define the following constructors using member initializer   //  list syntax:   //   //   1. A default constructor, which initializes seat count   //      to 121, passenger count to 121, and the flight   //      destination to "Fullerton".   //   2. A constructor overload which accepts the seat count,   //      passenger count, and flight destination and initializes   //      each corresponding member variable appropriately.   // ===========================   int GetPassengerCount() const {     return passenger_count_;   }   void SetPassengerCount(int passengers) {     passenger_count_ = passengers;   }   int GetSeatCount() const {     return seat_count_;   }   void SetSeatCount(int seats) {     seat_count_ = seats;   }   const std::string &GetDestination() const {     return destination_;   }   void SetDestination(const std::string &dest) {     destination_ = dest;   }  private:   int seat_count_;   int passenger_count_;   std::string destination_; }; Expected Output: 121 passengers flying to Fullerton 220 passengers flying to Atlanta 75 passengers flying to Guatemala City

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter1: A First Program Using C#
Section: Chapter Questions
Problem 12RQ
icon
Related questions
Question

C++ programming task.


main.cc file:


#include <iostream>
#include <map>
#include <vector>

#include "plane.h"

int main() {
  std::vector<double> weights{3.2, 4.7, 2.1, 5.5, 9.8, 7.4, 1.6, 9.3};
  std::cout << "Printing out all the weights: " << std::endl;
  // ======= YOUR CODE HERE ========
  // 1. Using an iterator, print out all the elements in
  //    the weights vector on one line, separated by spaces.
  //    Hint: see the README for the for loop syntax using
  //    an iterator, .begin(), and .end()
  // ==========================

  std::cout << std::endl;
  std::map<std::string, std::string> abbrevs{{"AL", "Alabama"},
                                             {"CA", "California"},
                                             {"GA", "Georgia"},
                                             {"TX", "Texas"}};
  std::map<std::string, double> populations{
      {"CA", 39.2}, {"GA", 10.8}, {"AL", 5.1}, {"TX", 29.5}};
  std::cout << "\nPrinting out the state populations: " << std::endl;
  // ========= YOUR CODE HERE =========
  // 2. Using an iterator, print out each state's population
  //    on a new line, in the format:
  //      "Population of Alabama: 5.1 million"
  //      "Population of California: 39.2 million"
  //      ... and so on
  //
  //    * abbrevs maps from the state abbreviation to the
  //      full state name.
  //    * populations maps from the state abbreviation to
  //      to the population (in millions)
  //    Use the abbrevs map to retrieve the full state name
  //    to print out, while iterating over the populations
  //    map.
  // =======================

  std::cout << std::endl;
  // 3. Implement the constructors of the Plane class, in
  //    "plane.h". Refer to the README or the comments in
  //    "plane.h" for instructions.

  // ======= YOUR CODE HERE ========
  // 4. Create an empty vector of Plane objects called `fleet`.
  // ==========================

  // =========== YOUR CODE HERE =======
  // 5. Create a Plane `p1` instantiated with the default
  //       constructor. Add `p1` to the `fleet` vector.
  // =========================

  // ======= YOUR CODE HERE ====
  // 6. Use the constructor overload to create a Plane `p2`
  //       with 150 seats, 75 passengers, and destination
  //       "New York City". Add `p2` to the `fleet` vector.
  // ==================

  // Uncomment these lines of code after completing #3-6.
  // Plane p3(220, 220, "Atlanta");
  // Plane p4(75, 75, "Guatemala City");
  // Plane p5(125, 94, "Medellin");

  // ====== YOUR CODE HERE ====
  // 7. Add `p3`, `p4`, and `p5` to the `fleet` vector.
  // ==========================

  // ======= YOUR CODE HERE =======
  // 8. Using an iterator, print out all the flights in
  //    the `fleet` vector, only if the flights are full.
  //    Hint: see the README for the expected format.
  // =========================


plane.cc file

 

#include "plane.h"

// ============ YOUR CODE HERE ==========
// This implementation file (plane.cc) is where you should implement
// the member functions declared in the header (plane.h), only
// if you didn't implement them inline within plane.h.
//
// Remember to specify the name of the class with :: in this format:
//     <return type> MyClassName::MyFunction() {
//        ...
//     }
// to tell the compiler that each function belongs to the Plane class.
// =============================================

 

plane.h file

 

#include <string>

class Plane {
 public:
  // ========== YOUR CODE HERE ==========
  // Define the following constructors using member initializer
  //  list syntax:
  //
  //   1. A default constructor, which initializes seat count
  //      to 121, passenger count to 121, and the flight
  //      destination to "Fullerton".
  //   2. A constructor overload which accepts the seat count,
  //      passenger count, and flight destination and initializes
  //      each corresponding member variable appropriately.
  // ===========================
  int GetPassengerCount() const {
    return passenger_count_;
  }
  void SetPassengerCount(int passengers) {
    passenger_count_ = passengers;
  }
  int GetSeatCount() const {
    return seat_count_;
  }
  void SetSeatCount(int seats) {
    seat_count_ = seats;
  }
  const std::string &GetDestination() const {
    return destination_;
  }
  void SetDestination(const std::string &dest) {
    destination_ = dest;
  }

 private:
  int seat_count_;
  int passenger_count_;
  std::string destination_;
};

Expected Output:

121 passengers flying to Fullerton
220 passengers flying to Atlanta
75 passengers flying to Guatemala City

Iterators
In this week's lab, you will practice creating and using iterators. We'll review what they are, and common use cases for them.
Overview of Iterators
What is an iterator?
Iterators in C++ are objects that allow you to access each element of a container (e.g., std::vector, std::map). No matter the underlying
implementation of the container, we can use the same iterator operations. This idea of using iterators to hide away (aka "abstract away") the
underlying structure of a container and the specific implementation details needed to iterate over its elements is called abstraction, and is one
of the core pillars of object-oriented programming.
Obtaining Iterators
Iterators are effectively pointers to specific elements of a container. Containers in the C++ standard library often have the following functions
defined to obtain an iterator for that container:
1. begin(), which returns an iterator pointing to the first element of a container, and
2. end(), which returns an iterator pointing to the element after the last element of a container.
Say we define a vector of strings:
std::vector<std::string> lyrics
{"I", "got", "my", "peaches", "out", "in", "Georgia"};
We can obtain an iterator that points to the first element, the string "I":
std::vector<std::string>::iterator it = lyrics.begin();
To move the iterator along to other elements in the vector, we can apply iterator operators.
Iterator Operators
1. operator++: the ++ operator, or increment, moves the iterator to point to the next element.
it++;
std::cout << *it; // Prints "got"
Similarly, we can use the operator to move the iterator by a specified amount of elements.
it += 5; // Moves iterator forward by 5 elements.
std::cout << *it; // Prints "Georgia"
2. operator==: the operator, or equality operator, is applied on two iterators to check if they are "equal". Two iterators are "equal" if they
are pointing to the same element in the container.
Transcribed Image Text:Iterators In this week's lab, you will practice creating and using iterators. We'll review what they are, and common use cases for them. Overview of Iterators What is an iterator? Iterators in C++ are objects that allow you to access each element of a container (e.g., std::vector, std::map). No matter the underlying implementation of the container, we can use the same iterator operations. This idea of using iterators to hide away (aka "abstract away") the underlying structure of a container and the specific implementation details needed to iterate over its elements is called abstraction, and is one of the core pillars of object-oriented programming. Obtaining Iterators Iterators are effectively pointers to specific elements of a container. Containers in the C++ standard library often have the following functions defined to obtain an iterator for that container: 1. begin(), which returns an iterator pointing to the first element of a container, and 2. end(), which returns an iterator pointing to the element after the last element of a container. Say we define a vector of strings: std::vector<std::string> lyrics {"I", "got", "my", "peaches", "out", "in", "Georgia"}; We can obtain an iterator that points to the first element, the string "I": std::vector<std::string>::iterator it = lyrics.begin(); To move the iterator along to other elements in the vector, we can apply iterator operators. Iterator Operators 1. operator++: the ++ operator, or increment, moves the iterator to point to the next element. it++; std::cout << *it; // Prints "got" Similarly, we can use the operator to move the iterator by a specified amount of elements. it += 5; // Moves iterator forward by 5 elements. std::cout << *it; // Prints "Georgia" 2. operator==: the operator, or equality operator, is applied on two iterators to check if they are "equal". Two iterators are "equal" if they are pointing to the same element in the container.
We most frequently use an equality check as a condition in a for loop:
for (std::vector<std::string>::iterator
it = lyrics.begin();
it != lyrics.end();
it++) {
}
Let's break down each part of this for loop. The initialization
std::vector<std::string>::iterator it = lyrics.begin();
declares it, and sets it to point to the first element initially.
The condition it != lyrics.end() stops the for loop when the iterator has moved past the last element in the vector,
Finally, the update it++ moves the iterator forward by 1 element at every iteration.
3. operator*: the operator, or the deference operator, dereferences (i.e. accesses) the element that the iterator is currently pointing to.
What is returned when you dereference an iterator depends on what type of element is stored in that container. In the case of our lyrics
vector above, dereferencing its iterator would give us a string, as each element is a std::string. We can complete the for loop below:
for (std::vector<std::string>::iterator
it = lyrics.begin();
it != lyrics.end();
it++) {
std::cout << *it << " ";
}
Which, after all iterations are run, prints out "I got my peaches out in Georgia".
Implementing the Plane Class
We've partially defined the Plane class for you in "plane.h" and provided the accessor and mutator functions for you, for each of the member
variables in Plane: seat count, passenger count, and destination.
Define two constructors:
1. A default constructor which sets the default values of each of the member variables. seat_count_, and passenger_count_ should be
initialized to default values 121, and destination_ set to "Fullerton"
2. A non-default constructor overload which accepts 3 parameters: an int for the seat count, int for the passenger count, and a const
reference to a std::string for the destination.
In main.cc, you will be asked to create a fleet of Plane objects and iterate over that fleet. As you iterate over each Plane in the fleet, you
must print out the flights and their destinations, only if that flight is fully booked. A flight is fully booked if every seat in the Plane is occupied
(i.e. the passenger count is equal to the seat count). Assume that the passenger count is always less than or equal to the seat count. See
expected output below:
Transcribed Image Text:We most frequently use an equality check as a condition in a for loop: for (std::vector<std::string>::iterator it = lyrics.begin(); it != lyrics.end(); it++) { } Let's break down each part of this for loop. The initialization std::vector<std::string>::iterator it = lyrics.begin(); declares it, and sets it to point to the first element initially. The condition it != lyrics.end() stops the for loop when the iterator has moved past the last element in the vector, Finally, the update it++ moves the iterator forward by 1 element at every iteration. 3. operator*: the operator, or the deference operator, dereferences (i.e. accesses) the element that the iterator is currently pointing to. What is returned when you dereference an iterator depends on what type of element is stored in that container. In the case of our lyrics vector above, dereferencing its iterator would give us a string, as each element is a std::string. We can complete the for loop below: for (std::vector<std::string>::iterator it = lyrics.begin(); it != lyrics.end(); it++) { std::cout << *it << " "; } Which, after all iterations are run, prints out "I got my peaches out in Georgia". Implementing the Plane Class We've partially defined the Plane class for you in "plane.h" and provided the accessor and mutator functions for you, for each of the member variables in Plane: seat count, passenger count, and destination. Define two constructors: 1. A default constructor which sets the default values of each of the member variables. seat_count_, and passenger_count_ should be initialized to default values 121, and destination_ set to "Fullerton" 2. A non-default constructor overload which accepts 3 parameters: an int for the seat count, int for the passenger count, and a const reference to a std::string for the destination. In main.cc, you will be asked to create a fleet of Plane objects and iterate over that fleet. As you iterate over each Plane in the fleet, you must print out the flights and their destinations, only if that flight is fully booked. A flight is fully booked if every seat in the Plane is occupied (i.e. the passenger count is equal to the seat count). Assume that the passenger count is always less than or equal to the seat count. See expected output below:
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 6 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,