lecture12slides

lecture12slides - Monday November 19th 1 Questions about...

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

View Full Document Right Arrow Icon
Monday, November 19 th 1. Questions about project #6 2. Struct quiz! 3. Finish up last weeks lecture on classes 4. More, and more, and more on classes Remember: place class declarations in a .H (header) file Place actual class member function implementations/logic in a .CPP file. Include .H files in other .CPP files where you want to use your class. Add your class .CPP files to your project
Background image of page 1

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

View Full DocumentRight Arrow Icon
What happens and why? struct foo { int geek; char nerd; float goober; }; void func(foo achoo) { cout << achoo.geek << endl; achoo.nerd = ‘C’; } void main(void) { foo a = {10,’A’,3.14159}; func(a); cout << a.nerd << endl; }
Background image of page 2
Passing structures or classes by POINTER! struct foo { int geek; char nerd; float goober; }; void func(foo *achoo) { cout << (*achoo).goober << endl; cout << achoo->geek << endl; achoo->nerd = 'A'; (*achoo).geek = 20; } void main(void) { foo a = {10,'A',3.14159}; func(&a); cout << a.geek << endl; cout << a.nerd << endl; }
Background image of page 3

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

View Full DocumentRight Arrow Icon
Accessing member variables, Pointers and classes class cow { public: int GetHooves(void); int m_nHooves; }; int cow::GetHooves(void) { return(m_nHooves); } void main(void) { cow a; a.m_nHooves = 5; cout << a.GetHooves() << endl; cow *b; b = &a; b->m_nHooves = 10; cout << b->GetHooves() << endl; cout << a.GetHooves() << endl; }
Background image of page 4
What’s wrong with the following? class cow { public: int GetHooves(void); int m_nHooves; }; int cow::GetHooves(void) { return(m_nHooves); } void main(void) { cow *a; a->m_nHooves = 5; cout << a.GetHooves() << endl; }
Background image of page 5

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

View Full DocumentRight Arrow Icon
class Circ // class declaration { public: void Init(float x,float y,float r); float GetArea(void); float GetCircum(void); private: float m_x, m_y, m_rad; }; void Circ::Init(float x,float y,float r) { m_x = x; m_y = y; m_rad = r; } float Circ::GetArea(void) { return(3.14159*m_rad*m_rad); } float Circ::GetCircum(void) { return(2*3.14159*m_rad); } main() { Circ a,b; // definitions/instances a.Init(0,0,10); b.Init(3,4,1); cout << a.GetCircum(); cout << b.GetArea(); ...
Background image of page 6
Another way of writing the same class: You can embed your member functions inside your class declaration. Or place some inside and some outside. .. class Circ { public: void Init(float x, float y, float r) { m_x = x; m_y = y; m_rad = r; } float GetArea(void) { return(3.14159*m_rad*m_rad); } float GetCircum(void); private: float m_x, m_y, m_rad; }; float Circ::GetCircum(void) { return(2*3.14159*m_rad); } NOTE: Placing an implementation inside the class decl. makes the function
Background image of page 7

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

View Full DocumentRight Arrow Icon
inline by default, and exposes the implementation . Classes: CONST and default parameters
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.

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 / 28

lecture12slides - Monday November 19th 1 Questions about...

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

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