1D tut - 1 Objects and Classes Continued Engineering 1D04,...

Info iconThis preview shows pages 1–3. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Objects and Classes Continued Engineering 1D04, Teaching Session 11 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 1 Recap: HighScores Example txtName1 txtName2 txtScore2 txtScore1 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 2 recap: HighScores Example public class HighScore { private class hsRecord { public string name; public int score; } private const int maxElements = 10; private hsRecord hsArray = new hsRecord[maxElements]; private int length; public bool add(string newName, int newScore) { . . . } public void show() { . . . } } no room to show this here no room to show this here © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 3 recap: HighScores What about including a title for the name of the game at the time of instantiation? In general it is useful to be able to include initial processing for an object at the time of its instantiation. This is done through a constructor . 2 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 4 Constructors A constructor is simply a method that is executed automatically when the class is instantiated in an object. The name of the constructor method is exactly the same as the name of the class. The constructor method may also include parameters. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 5 A Constructor for HighScores Consider our example. We want the constructor to include a string parameter for the title of the game. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 6 Constructor Example public class HighScore { private class hsRecord { public string name; public int score; } const int maxElements = 10; private hsRecord hsArray = new hsRecord[maxElements]; private int length = 0; private string result; private string hsTitle; public HighScore(string title) { hsTitle = title; } . . . constructor © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 7 Constructor Example How do we use the constructor?...
View Full Document

This note was uploaded on 02/07/2010 for the course SDF sdf taught by Professor Sdf during the Spring '10 term at Manchester IN.

Page1 / 7

1D tut - 1 Objects and Classes Continued Engineering 1D04,...

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

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