Advertisement

07.12.2005 at 11:09AM PDT, ID: 21488856
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

C++ Basics: Dynamic arrays

Zone: C++ Builder
Tags: array, dynamic
Hey all,

I'm pretty new to C++ so forgive how simple this question probably will be.

I'm creating a class which performs operations on instances of another class which it stores in memory. My problem is that I don't know how many instances there will be.

Basically, I have a class called 'employee' which handles the details of each record in my program. I'm creating a class called 'directory' which will support adding/deleting/searching/sorting the list of employees. As this is for a Uni assignment, I'm not storing this in any form of a datasource, but am just keeping it in memory during runtime.

I was thinking of using some sort of a dynamic pointer array, but I'm not quite sure how to implement this. I know I can create a pointer array using the new keyword and can specify the size of the array during runtime. In this case, how would I resize the array to add a new record or to delete an old one? I've come across the malloc and realloc functions from C, but I want to use C++ only code.

I can create a function the creates a new dynamic array of a size 1 greater that the existing array, copy the contents of the old array, add the new record, and delete the old array, but this seems like a round about way to go about this.

Sorry about the poor explanation, I'm still getting used to the terminology etc.

Regards,
Esulin
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: Esulin
Solution Provided By: Axter
Participating Experts: 3
Solution Grade: A
Views: 191
Translate:
Loading Advertisement...
07.12.2005 at 11:13AM PDT, ID: 14424118

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.12.2005 at 11:14AM PDT, ID: 14424132

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.12.2005 at 11:17AM PDT, ID: 14424156

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.12.2005 at 12:02PM PDT, ID: 14424570

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.12.2005 at 07:46PM PDT, ID: 14427831

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.13.2005 at 03:17AM PDT, ID: 14429493

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.13.2005 at 08:18AM PDT, ID: 14432001

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
07.12.2005 at 11:13AM PDT, ID: 14424118
>>I know I can create a pointer array using the new keyword and can specify the size of the array during runtime. In this
>>case, how would I resize the array to add a new record or to delete an old one?

In C++ the preferred method for dynamic array is std::vector.

You should use std::vector<yourobject> and then add to the vector as you get objects to put into that vector.

You can access a vector like you would a C style array.

std::vector<int> vInt;
vInt.push_back(123);
vInt.push_back(456);

cout << vInt[0] << endl;
cout << vInt[1] << endl;

A vector will clean up the memory automatically, so you don't have to worry about calling new or delete.
Accepted Solution
 
07.12.2005 at 11:14AM PDT, ID: 14424132
To get the size of your vector, call the size() method

cout << "Qty items in array = " << vInt.size() << endl;
 
07.12.2005 at 11:17AM PDT, ID: 14424156
To create an array using a pointer, you use the following method:
int Qty = 99;
int *pArrayInt = new int[Qty];

Then you must make sure to use delete[] to clean up above variable:
delete [] pArrayInt;

There is not easy method to reallocate memory created via new[] operator.
That's why it's better to use a vector instead, which you can easily increase or decrease it's size.
 
07.12.2005 at 12:02PM PDT, ID: 14424570
Ok... The following is my directory.h header file.
--------------------------------------------------------------------------------
#ifndef _directory_H
#define _directory_H

using namespace::std;

class directory
{
private:
    std::vector<employee> dir;
       
public:

directory();
directory(employee);
~directory();
void addEmployee(employee);
void delEmployee(string);
int countEmployees();
void displayDir();
void sortDir();
int searchDir();

};
#endif
--------------------------------------------------------------------------------

I got two questiosn...

1) I get this error "using-declaration for non-member at class scope" when I try to use the std::vector. What am I missing.
2) I have a class called employee that these functions have to deal with. How do I link the employee class to this class so that employee is recognised as a data type?

- Esulin
 
07.12.2005 at 07:46PM PDT, ID: 14427831
1)

using namespace::std;

This says you want to use the name "std" in the namespace named "namespace".  You probably want to use all the names in the "std" namespace.  The statement for that is:

using namespace std;

(No colons.)

2)  Put the declaration of the employee class in a file named "employee.h".  In the directory.h file, include the line:

#include "employee.h"

Put this line somewhere before the file tries to do anything with the employee class.
Assisted Solution
 
07.13.2005 at 03:17AM PDT, ID: 14429493
>1) I get this error "using-declaration for non-member at class scope" when I try to use the std::vector. What am I missing.

you are missing

#include <vector>
Assisted Solution
 
07.13.2005 at 08:18AM PDT, ID: 14432001
great answer... all of them. Many thanks.

Esulin
 
 
20080236-EE-VQP-29