#includeclass Item { private: int id; public: Item(int itemId) { id = itemId; } int getID() { return id; } }; int main() { Item item1(1001); std::cout << "Item ID: " << item1.getID() << std::endl; return 0; }
#includeThis example creates an Item object with an ID of 1001 and a name of "Hammer" using a namespace to encapsulate the class. The getID function is then called to retrieve the ID and print it to the console. The package library for this code example is likely to be a custom library specific to the project.#include namespace inventory { class Item { private: int id; std::string itemName; public: Item(int itemId, std::string name) { id = itemId; itemName = name; } int getID() { return id; } std::string getName() { return itemName; } }; } int main() { inventory::Item item1(1001, "Hammer"); std::cout << "Item ID: " << item1.getID() << std::endl; std::cout << "Item Name: " << item1.getName() << std::endl; return 0; }