-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstructs.cpp
65 lines (56 loc) · 1.5 KB
/
structs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* Structs.cpp
*
* Created on: Jan 4, 2012
* Author: behnam
*/
#include <iostream>
using namespace std;
/*
* In C++ the only difference between a class and a struct is that class-members
*are private by default, while struct-members default to public. So structures
*can have constructors, ref:
*http://www.yolinux.com/TUTORIALS/LinuxTutorialC++Structures.html
* */
struct DataElement {
string SVal;
int iVal;
bool hasData;
DataElement() // Example of a constructor used in a structure.
{
iVal = -1;
hasData = 0;
}
};
struct Employee {
int nID;
int nAge;
float fWage;
// structs can have function as well
int add(int a, int b) { return a + b; }
};
int Structsmain()
// int main()
{
// initializer list. This allows you to initialize some or all the members
// of a struct at declaration time.
Employee Joe = {1, 42, 60000.0f};
Employee sJoe;
sJoe.nID = 14;
sJoe.nAge = 32;
sJoe.fWage = 24.15;
cout << sJoe.add(12, 12) << endl;
cout << "sum up the bytes of two integers and one float=" << sizeof(sJoe)
<< endl;
// Example of a constructor used in a structure.
DataElement myDataElement;
cout << myDataElement.iVal << endl;
cout << myDataElement.hasData << endl;
// example of "->" operator, when we have pointer to struct or class, for
// accessing member we have to use -> operator
DataElement RealData;
DataElement *PointerToRealData = &RealData;
PointerToRealData->iVal = 1234;
cout << PointerToRealData->iVal << endl;
return 0;
}