Stuctures in classes

G

gmelcer

Hi, I need to use the data type "struct" in a class. Could someone show
me a simple example of how to use stuct in a class and access the data
type which is declared in the private section of the class. Here is an
exmaple of what I am trying to do. If someone could quickly write a
module to acces the structure I would highly appreciate it

#include <iostream>

#ifndef POLYLINE
#define POLYLINE
struct PointSet{
float x;
float y;
};
typedef PointSet Point;

class PolyLine
{
public:

float length();
void insert(int x, int y, int placement);
void remove(int placement);
void store();

private:
int counter;
Point Points[50];
};

#endif
..
 
D

dan2online

FYI,
you can overload [] to access the Points

class PolyLine
{
public:

float length();
void insert(int x, int y, int placement);
void remove(int placement);
void store();
+ Point &operator[](const int placement) const;
private:
int counter;
Point Points[50];

};

+ inline Point &PolyLine::eek:perator[](const int index)
+ {
+ if(index >=0 && index < 50) return Points[index];
+ return Points[0]; /* out of range, return the first point */
+ }
 
R

Rolf Magnus

gmelcer said:
Hi, I need to use the data type "struct" in a class.

struct is not a data type. It's a tool to create your own data types. Note
also that struct and class are almost exactly the same thing.
Could someone show me a simple example of how to use stuct in a class and
access the data type which is declared in the private section of the
class.

You access members of a struct just the same way you access class members.
Here is an exmaple of what I am trying to do. If someone could
quickly write a module to acces the structure I would highly appreciate
it

#include <iostream>

#ifndef POLYLINE
#define POLYLINE
struct PointSet{
float x;
float y;
};
typedef PointSet Point;

class PolyLine
{
public:

float length();
void insert(int x, int y, int placement);
void remove(int placement);
void store();

private:
int counter;
Point Points[50];
};

#endif
.

Try something like:

Points[10].x = 3;

That would set the x member of the element at index 10 to 3.
 
R

Rolf Magnus

dan2online said:
I guess you misunderstood the first post.

I guess you haven't explained it clearly enough then. You asked how to
access your Point elements, and I showed you. If you thought I'd do your
homework by writing an implementation of your class, then you're mistaken.
 
O

osmium

gmelcer said:
Hi, I need to use the data type "struct" in a class. Could someone show
me a simple example of how to use stuct in a class and access the data
type which is declared in the private section of the class. Here is an
exmaple of what I am trying to do. If someone could quickly write a
module to acces the structure I would highly appreciate it

#include <iostream>

#ifndef POLYLINE
#define POLYLINE
struct PointSet{
float x;
float y;
};
typedef PointSet Point;

Why didn't you simply make it struct Point { } in the first place? This is
C++, not C.
class PolyLine
{
public:

float length();

I don't see how that can do anything worthwhile. There are 50 pairs of
points and you don't describe a path, so the aggregate length can't be
computed. Don't you want to talk about line segments? ISTM the signature
should be

float length( int start, int end);
or something similar to that.

void insert(int x, int y, int placement);
void remove(int placement);
void store();

private:
int counter;
Point Points[50];
};

#endif

I assume that is flotsam or jetsam.

Can you revise your question to remove the warts? Or else correct my
misunderstandings?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top