Solved

Object Orientated C++ Question

Posted on 2009-07-13
7
235 Views
Last Modified: 2013-11-05
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?!
0
Comment
Question by:ChrisAllison
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24844271
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
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 100 total points
ID: 24844371
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
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 24846600
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Accepted Solution

by:
evilrix earned 200 total points
ID: 24846753
>> 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
 
LVL 29

Assisted Solution

by:pepr
pepr earned 200 total points
ID: 24846858
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
 
LVL 29

Expert Comment

by:pepr
ID: 24846864
Sorry to evirix -- I did not noticed that he mentioned the vector...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24846877
>> 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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Find Visual Studio Tools 2 134
C++ help/ Toy problem 19 69
why doesn't assert terminate here? 2 18
using interface in TLB 3 27
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question