Solved

# UNDERSTANDING THE CODE

Posted on 2005-05-11
205 Views
I am not understanding what is going on in this part of the program except I understand they declared a structure.  Could someone help me understand what this part of the code is doing?
#include <iostream>
#include <iomanip>
#include <math.h>
#include <string>
#include <vector>
using namespace std;

enum Market {  INITIAL = -1, NYSE, AMEX, OTC, NASDAQ, NUM_MARKETS };

struct Share
{
Market    market;
int       shares;
double    price;

Share() : market(INITIAL), shares(0), price(0.0) {}
Share(Market m, int s, double p) : market(m), shares(s), price(p) {}
};

const char*  StockMarkets[NUM_MARKETS]   = { "NYSE", "AMEX", "OTC", "NASDAQ" };
double       CommissionFees[NUM_MARKETS] = { 0.02, 0.025, 0.03, 0.05 };

int main()
{
vector<Share>  ShareArr;
double         MarketValues[NUM_MARKETS] = { 0.00 };
0
Question by:mmorse0971

LVL 55

Expert Comment

Well, share, as you can see, is your main data type

Share has 2 constructor functions:
- A default constructor, first one, doesn't require arguments, set member with default values
- Another constructor which receives values from creator of the object

there are 2 global arrays also: StockMarkets (array of strings) and CommissionFees (array of double floating point numbers)

Inside main:
vector<Share> ShareArr;  declares an empty dynamic array (called 'vector' instead of array) using STL library's vector template
also is declared another array of doubles (local): MarketValues
0

LVL 4

Accepted Solution

hi

#include <iostream>
#include <iomanip>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
************ Above code is include files and setting namespace **************

enum Market {  INITIAL = -1, NYSE, AMEX, OTC, NASDAQ, NUM_MARKETS };
in above statement initial= -1 , nyse=0, amex=1, otc=2, nasdaq=3,num_markets=4
**********************************

struct Share
{
Market    market;
int       shares;
double    price;

Share() : market(INITIAL), shares(0), price(0.0) {}
Share(Market m, int s, double p) : market(m), shares(s), price(p) {}
};
*********
** above setting up the structure with two constructors
first contructors takes no arguments and
setsmarket=-1
sharres=0 and price =0.0

second constructor takes three arguments

const char*  StockMarkets[NUM_MARKETS]   = { "NYSE", "AMEX", "OTC", "NASDAQ" };
*** above sets up a string initalsed with
StockMarket[0]="NYSE"
StockMarket[1]="AMEX"
....
StockMarket[3]="NASDAQ";

double       CommissionFees[NUM_MARKETS] = { 0.02, 0.025, 0.03, 0.05 };
***** above statement sets an array CommisionFees initialised with values
CommisionFess[0]=0.02
CommisionFess[1]=0.025
CommisionFess[2]=0.03
CommisionFess[4]=0.05

int main()
{
vector<Share>  ShareArr;  sets up a vector  ( kind of a array of type share)

double         MarketValues[NUM_MARKETS] = { 0.00 };
above MarketValues[4]=0.00

cheers
0

## Featured Post

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.