473,320 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

dynamic object arrays

here's the assignment for clarity purposes.

Write two classes. The first one is 'Point' that has three private data
members (integers) that are x, y, z. The second one is 'Path' that has
two private data members that are 'numOfPoints' (integer) and 'points'
whose data type is Point *. The relationship between 'Point' and 'Path'
is that 'Path' has an array of 'Point' objects. Every time a 'Path'
object is created, its constructor is called and within that
constructor 'numOfPoints' is assigned a value and the memory is
dynamically allocated for points. ....

and here's the code so far:

#include <iostream>
using namespace std;

class Point
{
friend class Path;
private:
int x;
int y;
int z;
};

class Path
{
public:
Path(int nPoints)
{
setPoints(nPoints);
for (int i = 0; i < nPoints; i++)
points[i] = new Point;
}
void setPoints(const int nPoints)
{
numOfPoints = nPoints;
}
void getPoints()
{
for (int i = 0; i < numOfPoints; i++)
{
int x = 0, y = 0, z = 0;
cout<<"Point "<<i + 1<<")"<<endl<<" x: ";
cin>>x; points[i]->x = x;
cout<<" y: ";
cin>>y; points[i]->y = y;
cout<<" z: ";
cin>>z; points[i]->z = z;
}
}
~Path()
{
delete [] points;
}
private:
int numOfPoints;
Point *points;
};

int main()
{
Path newPath(5);

newPath.getPoints();

system("PAUSE");
return 0;
}

It all works great when I change Point *points; in

private:
int numOfPoints;
Point *points;

to Point *points[3]; ... but I need the integer 3 to be the value of
'numOfPoints' ... how would I go about doing this. I know I'm
forgetting a rule or something, just cant think of it.

Oct 4 '05 #1
2 1985
whiteboy wrote:
here's the assignment for clarity purposes.

Write two classes. The first one is 'Point' that has three private
data members (integers) that are x, y, z. The second one is 'Path'
that has two private data members that are 'numOfPoints' (integer)
and 'points' whose data type is Point *. The relationship between
'Point' and 'Path' is that 'Path' has an array of 'Point' objects.
Every time a 'Path' object is created, its constructor is called and
within that constructor 'numOfPoints' is assigned a value and the
memory is dynamically allocated for points. ....

and here's the code so far:

#include <iostream>
using namespace std;

class Point
{
friend class Path;
private:
int x;
int y;
int z;
};

class Path
{
public:
Path(int nPoints)
{
setPoints(nPoints);
Don't do this. Just initialise it in the c-tor initilaiser list.
for (int i = 0; i < nPoints; i++)
points[i] = new Point;
Where do you get 'points'? It's a pointer, right? What value does
it have? Why can you dereference it (by means of indexing it)? Do
you not need to allocate some memory for the 'points' itself?
}
void setPoints(const int nPoints)
{
numOfPoints = nPoints;
}
void getPoints()
{
for (int i = 0; i < numOfPoints; i++)
{
int x = 0, y = 0, z = 0;
cout<<"Point "<<i + 1<<")"<<endl<<" x: ";
Unmatche parenthesis in the user output...
cin>>x; points[i]->x = x;
cout<<" y: ";
cin>>y; points[i]->y = y;
cout<<" z: ";
cin>>z; points[i]->z = z;
Couldn't you do without 'x', 'y', or 'z'?
}
}
~Path()
{
delete [] points;
Why are you deleting a pointer you never allocated?
}
private:
int numOfPoints;
Point *points;
};

int main()
{
Path newPath(5);

newPath.getPoints();

system("PAUSE");
return 0;
}

It all works great when I change Point *points; in

private:
int numOfPoints;
Point *points;

to Point *points[3]; ... but I need the integer 3 to be the value of
'numOfPoints' ... how would I go about doing this. I know I'm
forgetting a rule or something, just cant think of it.


No, you're doing it slightly wrong. You're not supposed to allocate
every element of the 'points' "array" using 'new Point'. You're
supposed to allocate them all at once, as an array. Think 'new[]'.

V
Oct 4 '05 #2
whiteboy wrote:
here's the assignment for clarity purposes.

Write two classes. The first one is 'Point' that has three private
data members (integers) that are x, y, z. The second one is 'Path'
that has two private data members that are 'numOfPoints' (integer)
and 'points' whose data type is Point *. The relationship between
'Point' and 'Path' is that 'Path' has an array of 'Point' objects.
Every time a 'Path' object is created, its constructor is called and
within that constructor 'numOfPoints' is assigned a value and the
memory is dynamically allocated for points. ....

and here's the code so far:

#include <iostream>
using namespace std;

class Point
{
friend class Path;
private:
int x;
int y;
int z;
};

class Path
{
public:
Path(int nPoints)
{
setPoints(nPoints);
for (int i = 0; i < nPoints; i++)
points[i] = new Point;
This is likely to lead to a disastrous crash. Your 'points' member could be
pointing anywhere.
}
void setPoints(const int nPoints)
{
numOfPoints = nPoints;
}
void getPoints()
{
for (int i = 0; i < numOfPoints; i++)
{
int x = 0, y = 0, z = 0;
cout<<"Point "<<i + 1<<")"<<endl<<" x: ";
cin>>x; points[i]->x = x;
cout<<" y: ";
cin>>y; points[i]->y = y;
cout<<" z: ";
cin>>z; points[i]->z = z;
}
}
~Path()
{
delete [] points;
}
private:
int numOfPoints;
Point *points;
};

int main()
{
Path newPath(5);

newPath.getPoints();

system("PAUSE");
return 0;
}

It all works great when I change Point *points; in

private:
int numOfPoints;
Point *points;

to Point *points[3]; ... but I need the integer 3 to be the value of
'numOfPoints' ... how would I go about doing this. I know I'm
forgetting a rule or something, just cant think of it.


I don't know what you mean by "I need the integer 3 to be the value of
'numOfPoints'". According to the problem description, numOfPoints should be
whatever value is passed to the Path constructor (5 in your case). Your real
problem is the allocation of the Point objects. Leave 'points' as Point *
and allocate the Points like this:
points = new Points[nPoints];
Note that since you don't have a default constructor for Point, the x, y and
z values of each Point will be undefined until you call getPoints().

DW
Oct 4 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: meyousikmann | last post by:
The following code just sets up and fills a dynamic array of integers. #include <cstdlib> int main() { int* intArray = NULL; int count; count = 20;
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
15
by: Ronny Mandal | last post by:
Assume that you want to store n records in an array, and that the array can hold all n elements, that is, it is declared int a. And suddenlny in run-time, I need to store n+m items. Why cannot...
3
by: genc ymeri | last post by:
Hi, What can I use in C# for dynamic arrays ???? I have some records (struts in ..Net) and want to store them in a dynamic "arrays" or object list. I noticed the in C# arrays' length can't be...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
4
by: learnfpga | last post by:
Here is a little code I wrote to add the numbers input by the user.....I was wondering if its possible to have the same functionality without using dynamic arrays.....just curious..... ...
4
by: Zark3 | last post by:
Hi all, I was wondering if anybody could enlighten me on the possibility of dynamic casting. Or, well, whether or not I'm actually trying to do this the right way. What I have is a base class...
2
by: headware | last post by:
Do dynamic arrays declared using ReDim have to be freed? I assume that if it's an array of dynamically created objects (e.g. Scripting.Dictionary), each one of those objects will have to be set to...
4
by: Sunny | last post by:
Hi, Is there a way in javascript to create Dynamic arrays or arrays on fly. Something Like: var "ptsgN"+sd = new Array(); Here sd is incrementing by 1. I have lots of data that I am...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.