Solved

Object Orientated C++ Question

Posted on 2009-07-13
7
230 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 28

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 28

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ question 3 62
FMX TCameraComponent Problem 2 53
c++ using a LPCOLESTR * and adding a string varaible to the Pointer 2 49
designing in object programming 12 42
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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now