|
From: Daniel G. <sp...@ho...> - 2002-03-23 20:43:16
|
<html><div style='background-color:'><DIV>Hi, just been going over my course notes and I've come across the Struct tag. An example is below.</DIV>
<DIV>Looking at it, how is this different to a class? And why use it?</DIV>
<DIV> </DIV>
<DIV>Also, what's the difference between an array, and a vector, which I understand is its replacement but know little about them.</DIV>
<DIV> </DIV>
<DIV><FONT color=#ff0000>
<P>// A demonstration of the Employee structure</P>
<P>#include <iostream.h></P></FONT><FONT color=#800080>
<P>struct</FONT><FONT color=#ff0000> </FONT><FONT color=#008000>Employee</FONT><FONT color=#ff0000> // declaration of a structure type</P>
<P>{</P>
<P>int EmpID;</P>
<P>char EmpName[30];</P>
<P>char EmpAddr[40];</P>
<P>float Salary;</P>
<P>void GetData();</P>
<P>}EmpOne</FONT><FONT color=#0000ff>;</FONT><FONT color=#ff0000> // Optional declaration of a structure variable</P>
<P>void main()</P>
<P>{</P>
<P>}</P></FONT></DIV></div><br clear=all><hr>Join the worlds largest e-mail service with MSN Hotmail. <a href='http://g.msn.com/1HM305301/15'>Click Here</a><br></html>
|
|
From: Ioannis V. <no...@ho...> - 2002-03-23 22:05:45
|
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Daniel
Glenfield
Sent: Saturday, March 23, 2002 10:43 PM
To: dev...@li...
Subject: [Dev-C++] Struct tag
Hi, just been going over my course notes and I've come across the Struct
tag. An example is below.
Looking at it, how is this different to a class? And why use it?
In C++ a struct is a class with its members public by default.
Also, what's the difference between an array, and a vector, which I
understand is its replacement but know little about them.
A vector is a high level container written with templates with purpose
to provide the same functionality and efficiency while at the same time
providing safety and comfort.
// A demonstration of the Employee structure
#include <iostream.h>
#include <iostream>
struct Employee // declaration of a structure type
{
int EmpID;
char EmpName[30];
char EmpAddr[40];
float Salary;
void GetData();
}EmpOne; // Optional declaration of a structure variable
void main()
int main()
{
}
Ioannis
* Ioannis Vranos
* Programming pages: <http://www.noicys.d2g.com/>
http://www.noicys.d2g.com
* Alternative URL: <http://run.to/noicys> http://run.to/noicys
|
|
From: Jason H. <jas...@bt...> - 2002-03-24 00:33:04
|
The struct tag is a remnant of C carried through and extended in C++. =
Structs in C carry data only and don't allow methods and such to added, =
but a struct in C++ is equivalent to a class but with it's member =
automatically declared public by default, whereas in a class the default =
is private.
The only reason I can think of for using it in C++ is if it lends a =
clearer meaning to the code you are writing and the way it is =
represented, and possibly in compatibility with C, i.e. porting old C =
code to C++. Another difference between the C struct and the C++ struct =
is that in C a struct is a tag for a set of data formatted in a specific =
manner, and in C++ a struct represents a type. Mainly meaning that in C =
each declaration of a struct required you to specify that it was a =
struct, i.e.
struct a_structure data; // creates an a_structure struct in data.
In C++ this is still valid but you could also say:
a_structure data; // same
"a_structure" can be any structure previously defined. A work around you =
will see often in things like Win32 API is the use of a typedef for a =
struct, i.e.:
typedef struct tagPOINT { // pt=20
LONG x;=20
LONG y;=20
} POINT;
Where POINT is a typedef of the tagPOINT struct, and means you can =
declare a POINT directly.
An array is a primitive data representation used in C and C++ as the =
basic structured container available. An array of 1000 bytes directly =
represents 1000 consecutive memory locations in the underlying system. =
AFAIK the underlying code is supported by direct assembly =
representations of arrays (CISC), but don't quote me on that. One of the =
drawbacks of the basic array is that it has no bounds checking built in =
to it, so you could create an array of 1000 bytes and then go on to =
access 2000 bytes (and most likely crash your system). These errors can =
often be hard to find and require an extra investment in checking of =
bounds in your code.
On the other hand the Vector class (std::vector<type>) is a C++ =
container class that mimics much of what an array is, and most likely =
(although not guaranteed) is internally represented as an array or group =
of arrays. On top of that the class supports automatic resizing and =
bounds checking and a whole host of other manipulation methods and =
algorithmic functions. This extra functionality will incur an extra =
overhead on a system, but in modern C++ standard library implementations =
this cost will be negligible, and unlikely to be significant. As a C++ =
coder more often than not you would be recommended to use a vector =
although basic arrays will still have their uses occasionally.
Maybe this is more information that you wanted to know, but it may be =
valuable in future use.
Jason.
----- Original Message -----=20
From: Daniel Glenfield=20
To: dev...@li...=20
Sent: Saturday, March 23, 2002 8:43 PM
Subject: [Dev-C++] Struct tag
Hi, just been going over my course notes and I've come across the =
Struct tag. An example is below.
Looking at it, how is this different to a class? And why use it?
Also, what's the difference between an array, and a vector, which I =
understand is its replacement but know little about them.
// A demonstration of the Employee structure
#include <iostream.h>
struct Employee // declaration of a structure type
{
int EmpID;
char EmpName[30];
char EmpAddr[40];
float Salary;
void GetData();
}EmpOne; // Optional declaration of a structure variable
void main()
{
}
-------------------------------------------------------------------------=
-----
Join the world's largest e-mail service with MSN Hotmail. Click Here
_______________________________________________ Dev-cpp-users mailing =
list Dev...@li... =
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Ioannis V. <no...@ho...> - 2002-03-24 10:06:47
|
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Jason
Hardman
Sent: Sunday, March 24, 2002 2:33 AM
To: Dev-C++; Daniel Glenfield
Subject: Re: [Dev-C++] Struct tag
On the other hand the Vector class (std::vector<type>) is a C++
container class that mimics much of what an array is, and most likely
(although not guaranteed) is internally represented as an array or group
of arrays. On top of that the class supports automatic resizing and
bounds checking and a whole host of other manipulation methods and
algorithmic functions. This extra functionality will incur an extra
overhead on a system, but in modern C++ standard library implementations
this cost will be negligible, and unlikely to be significant. As a C++
coder more often than not you would be recommended to use a vector
although basic arrays will still have their uses occasionally.
Maybe this is more information that you wanted to know, but it may be
valuable in future use.
Jason.
sorrry for sqeezing the mateer so much but... :) a vector doesn't incur
any addtional cost when one knows how to use it. For example:
vector<int>array(1000);
for(vector<int>::size_type i=0; i<array.size(); i++)
// some stuff
Here no range checking occurs, the vector<int>::size() is inlined and
the object clears itself automatically when the end of its scope is
reached or an exception is thrown ("resource aquisition is
initialization" technique. That's why proper C++ doesn't need garbage
collection). Also the code is bullet proof (the probability of our
mistakes is very low) and we can change the initial size of the array by
changing only the value of the constructor.
This should not incur more cost than int *array=new int[1000];
for(size_t i=0; i<1000; i++)
// some stuff
When we want to avoid the cost of continuous new & delete we define a
large size from the beginning. Also it is guaranteed that subsequent
push_back operations do not incur time cost for every one push_back.
In summary if we know what we need and what we are doing everything is
ok. :)
For an example of efficicency of C++ high level containers and low level
C-style equivallents check my
http://users.forthnet.gr/ath/jvr/c++faq.htm
Ioannis
* Ioannis Vranos
* Programming pages: <http://www.noicys.d2g.com/>
http://www.noicys.d2g.com
* Alternative URL: <http://run.to/noicys> http://run.to/noicys
|