Object Orientated C++ Question

Hi Team,

I'm not new to programming and can program (I think) quite efficiently in vb.net and also SQL server. Because I am mainly self taught I was never taught how to use classes effectively, I have always created a work around (the long way probably) or managed the data itself in SQL and used VB to display the data.

In the spirit of trying things that are difficult, I am trying to teach myself C++ (well Microsofts latest implementation of it) to complete a project at work. The project is to create a portfolio tool  (very easy in vb.net), this portfolio will hold a number of instruments and I would create a instrument class and initiate it when I want to add a new instrument (like a stock) to the portfolio. This class will encapsulate a lot of the valuation methodologies each instrument may have (bonds are valued differently to stocks)

Here is my question, how do I 'hold' the group of instruments? In SQL I would have a series of rows and manage it in the data set. Would I create a array of instances of the instrument class? of course I dont know at design time the number of instances I will need of each class.

As I say I'm entering this into the C++ section as I want to code the solution in VC++ but I would have thought there is a generic solution to this problem

Any thoughts?!
ChrisAllisonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrjoltcolaCommented:
Consider using the C++ standard approach, the Standard Template Library, aka STL

You can use a list or hashmap type to store multiple items.

Containers are listed here:  http://www.cplusplus.com/reference/stl/
0
mrjoltcolaCommented:
Wrote up a quick STL list sample. It creates Widgets dynamically and stores them in a list container. Then it uses an iterator to print the list.

Also this is another site with really good STL samples and tutorials.

http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html
#include <vector>
#include <list>
#include <iostream>
 
using namespace std;
 
 
class Widget {
  public:
     string name;
     int val;
 
     Widget(string n) { name = n; }
 
};
 
list<Widget *> widgets;
 
int main() {
 
   Widget *w = new Widget("xyz");
   widgets.push_back(w);
   w = new Widget("abc");
   widgets.push_back(w);
 
   list<Widget *>::iterator iter;
   for(iter = widgets.begin(); iter != widgets.end(); iter++) {
      Widget * p = *iter;
      cout << "Widget " << p->name << endl;
   }
}

Open in new window

0
Deepu AbrahamR & D Engineering ManagerCommented:
You can have a look at the following if you are using VC++ MFC based application.
http://www.functionx.com/visualc/
have a look at the collection classes
class Vehicle
{
private:
//member variable
//member functions
public:
//member variables
//member functions
};
 
class Car:public Vehicle
{
 
};
 
class Bus:publis Vehicle
{
 
};
 
 
class Collection:public CArray
{
  
};

Open in new window

0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

evilrixSenior Software Engineer (Avast)Commented:
>> You can use a list or hashmap type to store multiple items.
STL doesn't implement a hashmap; however, Google have a couple of nice ones you can use.
http://code.google.com/p/google-sparsehash/

For random access and assuming you only want to add and delete from the back a vector is by far the most efficient STL container (internally it is a contiguous blob of memory), which is just a fancy name for a dynamically sizing array. Note that a vector is less efficient if you need to add and insert in the middle and for this a list is better; however, a list doesn't provide random access (access is sequential so accessing elements that are in the middle is slower than a vector) and it uses more memory (it has to maintain pointers to the previous and next item in the list). When in doubt use a vector until you know for sure a different container is more suitable, it's the most efficient general purpose container.

If you want key/values STL does implement a map and if you want to hold unique items it implements a set.

This link provides a list of all the STL containers, links to detailed descriptions
 of each and a table that shows the time/space complexities of each.
http://www.cplusplus.com/reference/stl/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
peprCommented:
I would also recommend to read the Bruce Eckel's "Thinking in C++, 2nd edition". You can download the electronic version for free (http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html, http://www.mindview.net/Books/DownloadSites). You probably should read it from start.

For the shortcut, focus on "Volume 2: Practical Programming", Part 2: The Standard C++ Library, chapter 7: Generic containers. Besides the above mentioned containers, you may be also interested in "vector" container, which resembles the classical array. You should also learn what the iterators are as they are closely related concept to containers. Shortly, you use them in for loops to write what in other languages is called for_each loop.
0
peprCommented:
Sorry to evirix -- I did not noticed that he mentioned the vector...
0
evilrixSenior Software Engineer (Avast)Commented:
>> Sorry to evirix -- I did not noticed that he mentioned the vector...
No worries pepr :)

>> Shortly, you use them in for loops to write what in other languages is called for_each loop.
<OT>
You might be interested to know that Boost provides a nice foreach construct for C++ (miles better than std::for_each)
http://www.boost.org/doc/libs/1_35_0/doc/html/foreach.html
</OT>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.