lecture15slides

lecture15slides - Monday December 3rd, 2001 Agenda 1. Fill...

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

View Full Document Right Arrow Icon
Monday December 3 rd , 2001 Agenda 1. Fill out class evaluation forms 2. Project #7 questions 3. friends continued: friend classes 4. The “this” pointer and classes 5. Static member functions 6. Simple dynamic memory allocation: “the heap” 7. Dynamic memory allocation+classes 8. Review for the final
Background image of page 1

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

View Full DocumentRight Arrow Icon
Friend classes class CPerson { public: CPerson(int age) { m_age = age; } void HowOldAreYou(void) { cout << "None of your business!\n"; } private: int m_age; void take_shirt_off() { cout << “OK.”; } }; main() { CPerson carey(30); carey.HowOldAreYou(); cout << carey.m_age; // error! carey.take_shirt_off(); // error! } When should you use friend classes?
Background image of page 2
class CPerson { public: CPerson(int age) { m_age = age; } void HowOldAreYou(void) { cout << "None of your business!\n"; } private: friend class CDoctor; // our friend int m_age; void take_shirt_off() { cout << “OK.”; } }; class Cdoctor { public: { if (pers.m_age > 120) cout << "You're probably sick!\n"; pers.take_shirt_off(); cout << "No boils. You look OK,\n"; } }; main() { CPerson carey(30); CDoctor doc; doc.GiveExam(carey); }
Background image of page 3

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

View Full DocumentRight Arrow Icon
The this pointer Question: how can an instance of a class determine its own address? Class SomeClass { public: void printmyaddress() { // what do I write here? } private: int m_var1; float m_var2; ... }; main() { SomeClass a, b; // we can do this… cout << &a cout << &b // but what about this? a.printmyaddress(); b.printmyaddress(); }
Background image of page 4
class Point { public: Point(int x, int y) { m_x = x; m_y = y; } int GetX() const { return m_x; }; int GetY() const { return m_y; }; void PrintMyAddress(void) { cout << "I am at: " << this << endl; cout << "My x value is: " << this->m_x << endl; cout << "My y value is: " << (*this).m_y << endl; } private: int m_x, m_y; }; main() { Point a(5,6); a.PrintMyAddress(); } Questions: 1. What does “this” point to? 2.
Background image of page 5

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

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

This note was uploaded on 10/08/2011 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Page1 / 21

lecture15slides - Monday December 3rd, 2001 Agenda 1. Fill...

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

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