Static Members

Lab C++ Intermediate 20 points

Write class Order with a static int nextId giving each new order an id starting at 1, and a static int count(). Read items and print #id ITEM for each, then the count.

Input

n, then n item names.

Output

n + 1 lines.

Example

Input

3
tea
cake
juice

Output

#1 tea
#2 cake
#3 juice
3
Sample input available Sample output available
Editor
C++

Hints

A static data member is defined once outside the class: int Order::nextId = 1;

Solution explanation

Static members belong to the class, shared by all objects.

Show solution
#include <iostream>
#include <string>
using namespace std;

class Order {
public:
    explicit Order(string item) : id_(nextId++), item_(item) {}
    int id() const { return id_; }
    const string &item() const { return item_; }
    static int count() { return nextId - 1; }

private:
    static int nextId;
    int id_;
    string item_;
};

int Order::nextId = 1;

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        string item;
        cin >> item;
        Order o(item);
        cout << "#" << o.id() << " " << o.item() << "\n";
    }
    cout << Order::count() << "\n";
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD