Docsity

Defining Classes in Object-Oriented Programming: A C# Example with Time1 Class, Slides of C programming

Toggle sidebar
bg1
1
CS 112 Introduction to
Programming
Lecture #16:
Defining Classes
http://flint.cs.yale.edu/cs112/
2
Outline
rAdmin. and review
rClasses and objects
rDefining classes
mBasic syntax
mExamples
3
PrintMonth GetMonthDays
GetMonthDays();
PrintMonth();
Main
Summary: Method Control Flow
4
Summary
rDesign
•Top-down; stepwise refinement
rImplementation and testing
•Write the simple functions first
•Write a piece and test a piece
–Test a method
»using some dummy method calls, e.g.
Call the method in Main() with some typical test cases
»using Visual Studio Immediate window
5
Outline
rAdmin. and review
ØClasses and objects
rDefining a class
mBasic syntax
mExamples
6
Object-Oriented Design
rThe focus of methods is on doing things;
roughly speaking, we can say that methods
focus on the verbs.
rIn object-oriented design, we focus on the
nouns
mIn object-oriented design, we tend to group
methods together according to the nouns
mImportant for large, complex programs
pf3
pf4
pf5

Partial preview of the text

Download Defining Classes in Object-Oriented Programming: A C# Example with Time1 Class and more Slides C programming in PDF only on Docsity!

CS 112 Introduction to

Programming

Lecture #16:

Defining Classes

http://flint.cs.yale.edu/cs112/

Outline

r Admin. and review

r Classes and objects

r Defining classes

m Basic syntax

m Examples

PrintMonth GetMonthDays

GetMonthDays();

PrintMonth();

Main

Summary: Method Control Flow

Summary

r Design

• Top-down; stepwise refinement

r Implementation and testing

• Write the simple functions first

• Write a piece and test a piece

– Test a method

» using some dummy method calls, e.g.

Call the method in Main() with some typical test cases

» using Visual Studio Immediate window

Outline

r Admin. and review

ÿ Classes and objects

r Defining a class

m Basic syntax

m Examples

Object-Oriented Design

r The focus of methods is on doing things;

roughly speaking, we can say that methods

focus on the verbs.

r In object-oriented design, we focus on the

nouns

m In object-oriented design, we tend to group

methods together according to the nouns

m Important for large, complex programs

C# Classes

r A C# class plays dual roles:

m Program module: containing a list of (static)

method declarations and (static) data fields

m Blueprint for generating objects

• It is the model or pattern from which objects are created

• Supports two techniques which are essence of object-

oriented programming

– “ data encapsulation ” (for abstraction)

– “ inheritance ” (for code reuse)

User-Defined Class

r A user-defined class is also called a user-defined

type

m class written by a programmer

r A class encapsulates (wrap together) data and

methods:

  • data members (member variables or instance variables)
  • methods that manipulate data members

Objects

r An object has:

m state - descriptive characteristics

m behaviors - what it can do (or be done to it)

r For example, consider a coin in a computer game

m The state of the coin is its current face (head or tail)

m The behavior of the coin is that it can be flipped

r Note the interactions between state and behaviors

m the behavior of an object might change its state

m the behavior of an object might depend on its state

Outline

r Admin. and review

r Classes and objects

ÿ Defining a class

ÿ Basic syntax

m Examples

public int x, y;

private char ch;

class MyClass

Defining Classes

r Use Project < Add Class to add a new class to your project

r A class contains data declarations and method

declarations

Data declarationsData declarations

Method declarationsMethod declarations

Member (data/method) Access Modifiers

public : member is accessible outside the class

private : member is accessible only inside the

class definition

Data Declarations

r You can define two types of variables in a class but not in any

method (called class variables )

m static class variables

m nonstatic variables are called instance variables (fields)

because each instance (object) of the class has its own copy

m class variables can be accessed in all methods of the class

r Comparison: Local variables

  • Variables declared within a method or within a block statement
  • Variables declared as local variables can only be accessed in

the method or the block where they are declared

Outline

TimeTest1.cs

Program Output

34 output += "\n\nAfter attempting invalid settings: " + 35 "\nUniversal time: " + time.ToUniversalString() + 36 "\nStandard time: " + time.ToStandardString(); 37 38 MessageBox.Show( output, "Testing Class Time1" ); 39 40 } // end method Main 41 42 } // end class TimeTest

Class View and Object Browser

r Class View and Object Browser are features

of Visual Studio that facilitate the design of

object-oriented applications

r Class View

m Displays variables and methods for all classes in a

project

m Displays as treeview hierarchical structure

m + at nodes allows nodes to be expanded

m - at nodes allows nodes to be collapsed

m Can be seen by selecting View < Class View

Class View: Example

Fig. 8.20 Class View of class Time1 and class TimeTest1.

Object Browser

r Object Browser

m Lists all classes in a library

m Helps developers learn about the functionality of a

specific class

m To view the Object Browser select any .NET FCL

method, right click, and select Go To Definition

Object Browser: Example

Object Browser when user selects MessageBox from TimesTest1.cs.

Example 2: The Coin Class

r We define a Coin class to model a coin in a game

r In our Coin class we could define the following data:

m face, an integer that represents the current face

m HEADS and TAILS, integer constants that represent the two

possible states

r We might also define the following methods:

m a Coin constructor, to set up the object

m a Flip method, to flip the coin

m a GetFace method, to return the current face

m a StateToString method, to return a string description of the

current state

r See Coin.cs, CountFlips.cs, FlipRace.cs

m To compile CountFlips: “csc CountFlips.cs Coin.cs”

m To compile FlipRace: “csc FlipRace.cs Coin.cs”

Instance Data: The Two Coins in FlipRace

int face;

class Coin

coin1: Coin

face = 0

coin2: Coin

face = 1

Backup Slides

Summary: Data Scope

r Variables declared in a larger scope can be

accessed in an enclosed scope, e.g.

m Variables declared in a class can be accessed by

the methods in the class

m Variables declared in a method can only be

accessed in the method

m Variables declared in a block can only be accessed

in the block