{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

1D tut - Recap HighScores Example Objects and Classes...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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 .
Image of page 1

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

View Full Document Right Arrow Icon
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;
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern