{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

9.ClassExample

9.ClassExample - AnExampleonclass Example class TimeRecord...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
An Example on class Example class TimeRecord { public: int hour; int minute; int second; }; 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
After defining TimeRecord, you can declare objects of this class #include <iostream.h> using namespace std; class TimeRecord { public: int hour; int minute; dinnerTime hour minute int second; }; int main() { TimeRecord dinnerTime; second return 0; } 2
Background image of page 2
More on declaring variable of type Time class TimeRecord { public: int hour; int minute; int second; }; int main() { TimeRecord DinnerTime, TimeArray[20]; TimeRecord * TimePtr; return 0; } 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Access the members of a class y The dot operator (.) y Example y To access the hour member of the variable DinnerTime: cout << DinnerTime.hour; y Assign a new time: DinnerTime hour = 18; DinnerTime.hour = 18; 18 hour 0 0 dinnerTime minute second 4
Background image of page 4
Example class TimeRecord {public: int hour; int minute; int second;}; int main() { TimeRecord dinnerTime, TimeArray[20]; TimeReord *timePtr = &dinnerTime 12111 timeRef TimeReord timePtr = &dinnerTime, dinnerTime.hour=1; dinnerTime.minute=0; 1 0 dinner 12111 (*timePtr).second= 12; TimeArray[4].hour = 4; 12 rTime return 0; } 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Example* class TimeRecord { public: 12111 timeRef int hour, minute, second; }; 1 0 dinner 12111 void main() {TimeRecord dinnerTime, *timeRef = &dinnerTime; 0 rTime timeRef >hour = 1; // equiv. to (*timeRef).hour = 1; cout << dinnerTime.hour << endl; } 6
Background image of page 6
Example* class TimeRecord {public: int hour; int minute; int second;}; int main() { TimeRecord *TimeArray; int size; cin >> size; TimeArray = new TimeRecord[size]; TimeArray[4].hour = 7; TimeArray[4].minute=0; TimeArray[4] second=0; TimeArray[4].second=0; return 0; } 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Passing TimeRecord argument* class TimeRecord{ public: int hour, minute, second;}; void printTime(TimeRecord & t) { t t h t i t t d } { cout << t.hour << t.minute << t.second; } int main() { TimeRecord dinnerTime; dinnerTime.hour=8;
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}