1D4 - Recap - Classes & Objects Class declaration...

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

View Full Document Right Arrow Icon
1 Objects and Classes Engineering 1D04, Teaching Session 10 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 1 Recap - Classes & Objects class hsRecord { public string name; public int score; } Class declaration defines a complex data structure hsRecord myRef; myRef = new hsRecord(); hsRecord[] highScore = new hsRecord[3]; name score Instantiation of hsRecord is an object name score name score name score 0 1 2 void addScore(string newName, int newScore) { \\uses highScore etc } © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 2 General Classes & Objects We have seen how classes can encapsulate data structures. This lets us group differently typed variables into a single record. Well - it’s even better than that Classes can encapsulate data structures and methods that work with those data structures, into a single entity. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 3 General Classes & Objects class hsRecord { public string name; public int score; } hsRecord[] highScore = new hsRecord[3]; public void addScore(string newName, int newScore) { } public void show() { } Class HighScore private data public methods
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 4 General Classes & Objects class hsRecord { public string name; public int score; } hsRecord[] highScore = new hsRecord[3]; public void addScore(string newName, int newScore) { } public void show() { } Class HighScore private data public methods cannot access from outside the class only way to access internal data structures from outside the class © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 5 General Classes & Objects class hsRecord { public string name; public int score; } hsRecord[] highScore = new hsRecord[3]; public void addScore(string newName, int newScore) { } public void show() { } Class HighScore So, what do users of the class see? © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 6 General Classes & Objects public void addScore(string newName, int newScore) public void show() Class HighScore This! © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 7 General Classes & Objects Before we see how we can construct such a class, let’s discuss why it could be useful. So - why?
Background image of page 2
3 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 8 General Classes & Objects Before we see how we can construct such a class, let’s discuss why it could be useful. Encapsulation - protecting data structures from being accessed from outside the class is crucial. Why? © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 9 General Classes & Objects Before we see how we can construct such a class, let’s discuss why it could be useful. Encapsulation
Background image of page 3

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

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

Page1 / 13

1D4 - Recap - Classes & Objects Class declaration...

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

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