[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Object Orientated C++ Question

Posted on 2009-07-13
7
Medium Priority
?
238 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 400 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:Deepu Abraham
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
Industry Leaders: 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 800 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 800 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

650 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