C++ Code Example: unique item number generation

In this code example, we explore a practical implementation of classes and objects, focusing on generating unique item numbers using a static counter. The Item class encapsulates item-specific details such as name, description, and color, and leverages a static counter to ensure each item has a unique identifier.

This example showcases essential object-oriented programming concepts, including encapsulation, constructors, and member functions. By initializing item numbers with a static counter, each Item instance automatically receives a distinct number upon creation. The main function demonstrates creating multiple Item objects, setting their attributes, and printing their details.

This approach is particularly useful for inventory management systems where unique identification of items is crucial. Through this code, you’ll gain a deeper understanding of managing unique identifiers in object-oriented programming.

#include <iostream>
#include <math.h>
using namespace std;

class Item {
private:
    int itemNr;
    string name, description, color;

    static int &getCount() {
       static int counter = 1000;
       return counter;
    }

public:
    Item() {
        itemNr = getCount()++;
    }

    void setName(string n) {
        name = n;
    }

    void setDescription(string desc) {
        description = desc;
    }

    void setColor(string c) {
        color = c;
    }

    void print() {
        cout << "Item name: " << name << endl;
        cout << "Item description: " << description << endl;
        cout << "Color: " << color << endl;
        cout << "ItemNr: " << itemNr << "\n"
             << endl;
    }
};


int main() {
    Item i1, i2;

    i1.setName("Shirt");
    i1.setDescription("Adidas performance essentials big logo");
    i1.setColor("Black");

    i2.setName("Jeans");
    i2.setDescription("Trendyol Collection");
    i2.setColor("Blue");

    i1.print();
    i2.print();

    return 0;
}

Code Explanation

Defining the Item Class

Private Member Variables

class Item {
private:
    int itemNr;
    string name, description, color;

The Item class is declared with private member variables: itemNr, name, description, and color.

Static Counter Function

    static int &getCount() {
       static int counter = 1000;
       return counter;
    }

The getCount function returns a reference to a static integer counter, initialized to 1000. This counter is used to assign unique item numbers.

Public Member Functions

Constructor
public:
    Item() {
        itemNr = getCount()++;
    }

The constructor initializes itemNr with the current value of counter and then increments counter to ensure unique item numbers for each instance.

Setters
    void setName(string n) {
        name = n;
    }

    void setDescription(string desc) {
        description = desc;
    }

    void setColor(string c) {
        color = c;
    }

These functions set the values of name, description, and color for an Item object.

Print Function
    void print() {
        cout << "Item name: " << name << endl;
        cout << "Item description: " << description << endl;
        cout << "Color: " << color << endl;
        cout << "ItemNr: " << itemNr << "\n" << endl;
    }

The print function outputs the details of the Item object, including name, description, color, and itemNr.

Main Function

Object Creation

int main() {
    Item i1, i2;

Two Item objects, i1 and i2, are instantiated.

Setting Attributes

    i1.setName("Shirt");
    i1.setDescription("Adidas performance essentials big logo");
    i1.setColor("Black");

    i2.setName("Jeans");
    i2.setDescription("Trendyol Collection");
    i2.setColor("Blue");

Attributes name, description, and color are set for i1 and i2 using their respective setter functions.

Printing Item Details

    i1.print();
    i2.print();

The print function is called for both i1 and i2 to display their details on the console.

Expected Output

The code will produce the following output:

Item name: Shirt
Item description: Adidas performance essentials big logo
Color: Black
ItemNr: 1000

Item name: Jeans
Item description: Trendyol Collection
Color: Blue
ItemNr: 1001