1
2
3
4
5
6
| struct DateStruct{ int nMonth; int nDay; int nYear;}; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| // Declare a DateStruct variableDateStruct sToday;// Initialize it manuallysToday.nMonth = 10;sToday.nDay = 14;sToday.nYear = 2020;// Here is a function to initialize a datevoid SetDate(DateStruct &sDate, int nMonth, int nDay, int Year){ sDate.nMonth = nMonth; sDate.nDay = nDay; sDate.nYear = nYear;}// Init our date to the same date using the functionSetDate(sToday, 10, 14, 2020); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| struct DateStruct{ int nMonth; int nDay; int nYear;};class Date{public: int m_nMonth; int m_nDay; int m_nYear;}; |
1
2
3
4
5
6
| Date cToday; // declare a variable of class Date// Assign values to our members using the member selector operator (.)cToday.m_nMonth = 10;cToday.m_nDay = 14;cToday.m_nYear = 2020; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| class Date{public: int m_nMonth; int m_nDay; int m_nYear; void SetDate(int nMonth, int nDay, int nYear)// Member { m_nMonth = nMonth; m_nDay = nDay; m_nYear = nYear; }}; |
1
2
| Date cToday;cToday.SetDate(10, 14, 2020); // call SetDate() on cToday |
m_nDay is actually referring to cToday.m_nDay. If we called cTomorrow.SetDate(), m_nDay inside of SetDate() would refer tocTomorrow.m_nDay.
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
| #include <iostream>class Employee{public: char m_strName[25]; int m_nID; double m_dWage; // Set the employee information void SetInfo(char *strName, int nID, double dWage) { strncpy(m_strName, strName, 25); m_nID = nID; m_dWage = dWage; } // Print employee information to the screen void Print() { using namespace std; cout << "Name: " << m_strName << " Id: " << m_nID << " Wage: $" << m_dWage << endl; }};int main(){ // Declare two employees Employee cAlex; cAlex.SetInfo("Alex", 1, 25.00); Employee cJoe; cJoe.SetInfo("Joe", 2, 22.25); // Print out the employee information cAlex.Print(); cJoe.Print(); return 0;} |
0 comments: