9.ClassExample

9.ClassExample - An Example on class Example class...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: An Example on class Example class TimeRecord { ublic: public: int hour; int minute; int second; }; 1 After defining TimeRecord, you can declare objects of this class #include <iostream.h> using namespace std; lass imeRecord class TimeRecord { public: int hour; int minute; innerTime hour minute int second; }; int main() { TimeRecord dinnerTime; dinnerTime second { ; return 0; } 2 ore on declaring variable of type 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 Access the members of a class y The dot ‐ operator (.) y Example o access the hour member of the variable innerTime y To access the hour member of the variable DinnerTime: cout << DinnerTime.hour; y Assign a new time: innerTime hour 18; DinnerTime.hour = 18; 18 hour dinnerTime minute second 4 Example ass imeRecord ublic: t our; t inute; t cond;}; class TimeRecord {public: int hour; int minute; int second;}; int main() { TimeRecord dinnerTime, TimeArray[20]; imeReord * mePtr & innerTime 12111 timeRef TimeReord timePtr = &dinnerTime, dinnerTime.hour=1; dinnerTime.minute=0; 1 d in n e r 12111 (*timePtr).second= 12; TimeArray[4].hour = 4; 12 r T im e return 0; } 5 Example* class TimeRecord { ublic 12111 timeRef public: int hour, minute, second; }; 1 d in n e r 12111 void main() {TimeRecord dinnerTime, *timeRef = &dinnerTime; r T im e timeRef ‐ >hour = 1; // equiv. to (*timeRef).hour = 1; cout << dinnerTime.hour << endl; } 6 Example* ass imeRecord ublic: t our; t inute; t cond;}; 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; imeArray ] second=0; TimeArray[4].second=0; return 0; } 7 Passing...
View Full Document

This note was uploaded on 01/26/2011 for the course BENG 1002 taught by Professor Dr.x during the Winter '10 term at HKU.

Page1 / 20

9.ClassExample - An Example on class Example class...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online