473,383 Members | 1,978 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,383 software developers and data experts.

Stuctures in classes

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
5 1739
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
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
I guess you misunderstood the first post.

Apr 14 '06 #4
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: steflhermitte | last post by:
Dear cpp-ians, I am working with a structure: struct meta_segment { long double id; long double num; long double mean; bool done;
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
18
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
3
by: =?Utf-8?B?SXNsYXkgUm9kcmlndWV6IEpyLg==?= | last post by:
How do you share glogal data stuctures between VB.net and C++ or C# ? Thanks, Islay -- Islay
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.