Solved

Object Orientated C++ Question

Posted on 2009-07-13
7
229 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 40

Accepted Solution

by:
evilrix earned 200 total points
Comment Utility
>> 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
Comment Utility
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
Comment Utility
Sorry to evirix -- I did not noticed that he mentioned the vector...
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
>> 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.

744 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

9 Experts available now in Live!

Get 1:1 Help Now