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 , template using product example
#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, const Product& p)
{
os << "Product: " << p.mName
<< "\tPrice: " << p.mPrice
<< "\tStock on hand: " << p.mStockLevel;
return os;
}
private:
string mName;
int mStockLevel;
double mPrice;
};
// template function to show all elements in a map
template<class T, class A>
void
showMap(const map<T, A>& m);
int
main()
{
Product Pen("Pen", 5.99, 58);
Product Lamp("Lamp", 28.49, 24);
Product Speaker("Speaker", 24.95, 40);
map<string, Product> productMap;
productMap[Pen.getName()] = Pen;
productMap[Lamp.getName()] = Lamp;
productMap[Speaker.getName()] = Speaker;
showMap(productMap);
return 0;
}
//
// Show map elements
//
template<class T, class A>
void
showMap(const map<T, A>& m)
{
cout << "Map elements:\n";
for
(map<T, A>::const_iterator ci = m.begin(); ci != m.end(); ++ci)
cout << ci->first << "\t" << ci->second << "\n";
cout << "\n\n";
}
Privacy Policy
|
About Us