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
.. 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 */
+ }
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.
I guess you misunderstood the first post.
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.
"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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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;
|
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...
|
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....
|
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...
|
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
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |