Docsity

Object-Oriented Programming - C Sharp Programming - Lecture Slides, Slides of C programming

Partial preview of the text

Download Object-Oriented Programming - C Sharp Programming - Lecture Slides and more Slides C programming in PDF only on Docsity!

Object-Oriented Programming

07_classes.ppt

Overview of Topics

• Object-Oriented Programming (OOP)

• Class vs Object

• Unified Modeling Language (UML)

• Defining Classes

• Using Classes

• Read-Only Properties

• Instance vs. Static (Shared)

• Constructors and Destructors

Class and Objects

• A class is a definition of a data type.

• A class definition includes members

variables as well as associated operations.

This is referred to as encapsulation.

• An object is a variable declared using a

class definition as the data type.

• A class is the definition, and an object is an

instance of the class.

Class Analogy

• A class is like a blueprint of a house.

• Each house being built allows for the

selection of tile, carpet, and wall colors.

• So on the order form their may be a blank

line to enter the color chosen, much like a

variable.

• Each house built based on the blueprint is

an instance of the house.

• We can not move into a blueprint;

a house must be built.

OOP Process

  • OO Programming involves using existing classes

(Buttons, String), and sometimes we define new

classes.

  • Define class (clsOrder)
    • Define variables: strDescription, intQty, decPrice
    • Define methods: set and get Description, Qty, Price
  • Program 1:
    • Declare an object of the type clsOrder
  • Program 2:
    • Declare an object of the type clsOrder
  • Both include the same properties and methods.
  • The objects based on clsOrder will be standardized

across the programs that use the clsOrder class.

Student Information System

  • Consider what date fields are recorded in a

Student Information System.

  • Birth Date
  • Enrollment Date
  • Payment Date
  • Withdrawal Date
  • Completion Date
  • Course Start Date
  • Course End Date
  • Graduation Date
  • Many more…

Date Class Designed

• We can create a class to handle dates and

the class may support:

  • Data validation for the month, day, year.
  • Date displayed in different formats.

• Designs of Object-Oriented Programs can

be represented using Unified Modeling

Language (UML).

• Class designs are displayed using UML Class

Notation.

UML – Class Notation

• Notation:

  • private

+ public

# protected

Class name

Properties (variables)

Operations (methods)

Class Notation

• 3 sections in the drawing

  • Class Name
  • Properties (variables)
  • Operations (methods)

• Properties and operations can be either

  • public, private, or protected
    • protected –same as private but used in inheritance
  • The variables and methods are considered

members.

Public vs Private Members

  • Public variables can be referenced and altered in methods that declare an object using the class definition.
  • Public methods can be called directly in methods that declare an object using the class definition.
  • Private variables can only be referenced and altered by methods defined inside of the class.
  • Private methods can only be called by methods defined inside of the class.
  • Private is the default if not specified.
  • Protected – applies to inheritance.
    • Is basically the same as private.
    • Can be accessed by class members and subclass members only.
    • We’ll cover inheritance a little later…

DateMDY Definition

public class DateMDY { public int intMonth, intDay, intYear;

public string getDate( ) { return (intMonth.ToString(“N0”) + “/” + intDay.ToString(“N0”) + “/” + intYear.ToString(“N0”) ); } }

//This class cannot be executed.

//It is merely a definition for other programs to use.

Declaring an Object

• An object is a variable declared using a class as

the data type.

• Declaring a primitive variable.

dataType variableName;

decimal decPrice;

• Declaring an object.

ClassName objectName = new ClassName( );

DateMDY bday = new DateMDY( );

Referencing Object Members

  • DateMDY bday = new DateMDY( );
  • After declaring an object use the dot operator between object name and members.

objectName.variable and/or objectName.method( )

bday.intMonth bday.getDate( ) bday.intDay bday.intYear

  • Only public members can be referenced directly by methods that use the class to declare an object.

Textbox Class Review

• A textbox is a class that has properties (.Text).

txtName.Text = strEmpName;

• A textbox is a class that has methods (.Focus).

txtName.Focus( )