pickzy.com

C  |  C++  |  Objective-C  |  VC++  |  Win32  |  MFC  |  Java  |  Php  |  Delphi  |  Visual Basic  |  .Net  |  Networking  |  General  |  Games  |  Jobs  |  Javascript  |  




Menu

pickSourcecode.com


        

 




 

Cpp > Programs

 

cpp example Product, price , Stock and sample program

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


class Product

{

public:

   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);

Product productArray[3] = {Pen, TableLamp, Speaker};


cout << "Price list:\n";

for (int i = 0; i < 3; ++i) cout << productArray[i] << "\n";

for (int j = 0; j < 3; ++j)
{

if (productArray[j].GetName() == "Speaker")
cout << "\nSpeaker's price is " << productArray[j].GetPrice() << "\n";

}


return 0;

}


 
Privacy Policy | About Us