By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

Stuctures in classes

P: n/a
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
..

Apr 14 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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::operator[](const int index)
+ {
+ if(index >=0 && index < 50) return Points[index];
+ return Points[0]; /* out of range, return the first point */
+ }

Apr 14 '06 #2

P: n/a
gmelcer wrote:
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.

Apr 14 '06 #3

P: n/a
I guess you misunderstood the first post.

Apr 14 '06 #4

P: n/a
dan2online wrote:
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.

Apr 14 '06 #5

P: n/a
"gmelcer" wrote:
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?
Apr 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.