Chapter 4 - PowerPoint PPT Presentation

Chapter 4
1 / 23

  • Uploaded on
  • Presentation posted in: General

Chapter 4. Writing Classes. Figures from Lewis, “C# Software Solutions”, Addison Wesley. Topics. Adding a Class to a Project Defining a Class Attributes Methods Using a Class Visibility. Multiple Classes in a Project. You’ve seen projects with many classes

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Presentation

Chapter 4

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Chapter 4

Chapter 4

Writing Classes

Figures from Lewis, “C# Software Solutions”, Addison Wesley



  • Adding a Class to a Project

  • Defining a Class

    • Attributes

    • Methods

  • Using a Class

  • Visibility

Multiple classes in a project

Multiple Classes in a Project

  • You’ve seen projects with many classes

    • XNA projects can (and often do) have multiple classes… one for each game element

  • Can add classes to our project

    • Keep them all in one file (~bad choice)

    • One class per file (better choice)

Adding to a project

Addingto aProject

Right-click project

Select Add | Add New Item…

Class data methods

Class = Data + Methods

Using the class

Using the Class

  • Before we define the Die class, let’s use it

  • Remember that OOP allows us to ignore the “guts” and just presume it works

  • There are 3+ people involved

    • The writer of the class (knows what the “guts” are)

    • The user of the class, writing the main program (knows his code, but doesn’t know the class “guts”)

    • The user of the program – sees the interface, doesn’t see program code at all

The die

The Die

  • At this point, what do you know about Die from the code?

    • You can “Roll()” it

    • It has “FaceValue”

    • Can call “SetFaceValue()”

    • Can call “GetFaceValue()”

  • Now let’s look at the guts…

The die class

The Die Class

Die class cont

Die Class (cont)

Die class cont1

Die Class (cont)

Die class cont2

Die Class (cont)

The user of a class

The “User” of a Class

  • Recall we don’t want to have to expose the “guts”

  • But we do need to tell a user of our class what it can do and what they have access to

    • Define the API of the class

The die class api

The Die Class API

Unified modeling language uml

Unified Modeling Language (UML)



  • OO motivation: protection/security

  • We need a way of selectively “publishing” parts of a class and “hiding” other parts of the class

  • Public & private

Visibility example

Visibility Example

class BMW_Z4 {

private int ModelYear;

public string LicensePlate;

private bool TopUp;

public void Drive()


Console.WriteLine("Roadin’ and Rockin’");


public void OpenTop()


TopUp = false;



Note the visibility (attributes will usually be private)

Method aretypically


Object method attribute visibility

Object Method & Attribute Visibility

BMW_Z4 myCar;

myCar = new BMW_Z4();

myCar.LicensePlate = "BMR4ME";

myCar.ModelYear = 2004;




Illegal b/c private

Interacting with objects

Interacting with Objects

  • Keep private/public visibility as needed



  • Login