C
|
C++
|
Objective-C
|
VC++
|
Win32
|
MFC
|
Java
|
Php
|
Delphi
|
Visual Basic
|
.Net
|
Networking
|
General
|
Games
|
Jobs
|
Javascript
|
Menu
Project Upload
Project Download
Articles
Programs
Search
Solution
pickSourcecode.com
Login
Register
About us
Contact us
Cpp
>
Programs
cpp map example and tutorials
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Product
{
public:
Product():mName("New Product"), mStockLevel(0), mPrice(0) {}
Product(string newName, double newPrice = 0,
int
newStockLevel = 0):
mName(newName), mPrice(newPrice), mStockLevel(newStockLevel) {}
void
SetName(string newName) {mName = newName; }
void
SetName(
int
newStockLevel) {mStockLevel = newStockLevel;}
void
SetPrice(double newPrice) {mPrice = newPrice; }
string GetName() const {return mName;}
int GetStockLevel() const {return mStockLevel;}
double GetPrice() const {return mPrice; }
friend ostream& operator<<(ostream& os, Product& p)
{
os << "Product: " << p.mName
<< "\tPrice: " << p.mPrice
<< "\tStock on hand: " << p.mStockLevel;
return os;
}
private:
string mName;
int mStockLevel;
double mPrice;
};
int
main()
{
Product Pen("Pen", 5.99, 58);
Product TableLamp("Table Lamp", 28.49, 24);
Product Speaker("Speaker", 24.95, 40);
map<string, Product> productMap;
productMap[Pen.GetName()] = Pen;
productMap[TableLamp.GetName()] = TableLamp;
productMap[Speaker.GetName()] = Speaker;
cout << "Speaker's price is " << productMap["Speaker"].GetPrice() << "\n";
return 0;
}
Privacy Policy
|
About Us