Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

Some concept of C Sharp Programming are Additional Controls, Declaring Arrays, Call-By-Reference Methods, Information Processing Cycle. Main points of this lecture are: Object-Oriented Programming, Class Vs Object, Unified Modeling Language, Defining Classes, Using Classes, Read-Only Properties, Instance, Static, Constructors and Destructors, Program

Typology: Slides

2012/2013

Uploaded on 04/27/2013

farooq
farooq 🇮🇳

4.3

(92)

204 documents

1 / 52

Toggle sidebar

Related documents


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

Object-Oriented Programming

  • C#.Net is an object-oriented language.
  • We have been developing our programs using predefined classes. Class Name: Textbox Properties: .Text, .Font, .TabStop, etc. Methods: .Focus( ), .SelectAll( ), .Show( ), etc.
  • Class definitions combine data and pertinent operations.
  • We create objects based on a class definition.
  • Recall that an object is a variable.
  • Each object has its own name and memory allocation. Objects: txtName, txtAddress, etc.

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.

Object Analogy

  • An actual house is the object.
  • The object has specific values, such as the colors chosen by the homeowner, but the class allows for color selection.
  • The homebuilder may offer various methods of applying the paint, such as roller, brush, or sponge.
  • If they don’t apply the paint, what’s the point in choosing colors.
  • Many houses may be built from the same blueprint. - Each will have its own plot of land. - Most will have different colors. - Some may have the same colors, but they will still be distinct instances of the blueprints.

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 Collection

  • How are the dates collected?
  • Screen or forms are used to collect each one.
  • Most have the same edits.
    • A lot of duplicate code and maintenance.
  • Data can be collected and saved to a file.
  • Data can then retrieved and used:
    • In update screens
    • In reports
    • As selection criteria for reports
  • It might make sense to create a class to handle the collection, validation, and processing of dates.

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 Outline

  • Class Name
  • Properties (variables)
    • Properties are used to record the current state of an object.
    • The values in the variables represent the current state.
  • Operations (methods)
    • Operations are used to modify the values of the members variables.
    • C# uses Property Blocks to set and get values stored in properties (variables).

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…

Date Class Notation

DateMDY

+month

+day

+year

+getDate()

  • Notation:
    • private
    • public

    protected

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( );

Instantiation

  • The process of creating an object of a class.

DateMDY bday = new DateMDY( );

  • It’s call a process because
    • Memory is allocated, and
    • A constructor method is called automatically.
    • Constructors are introduced a few slides later…

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( )

Using DateMDY Class

private void btnProcessBday (…)

{

DateMDY bday = new DateMDY( );

bday.intMonth = 10; bday.intDay = 31; bday.intYear = 1980;

txtDate.Text = bday.getDate( );

}

Multiple Objects

  • You can create many instances/objects of a class in the

same program.

  • Each object will have their own memory allocation.

DateMDY bday = new DateMDY( ); DateMDY dueDate = new DateMDY( );

  • Member variables are also known as

instance variables.

  • Their values are not shared among objects.

Class Abstraction

  • Classes are defined for other programmers to use or for you to use in many programs.
  • A class is created because we want a data type that is self contained.
  • Users of a class should only be concerned with what the class does and not how it does it.
  • If used correctly, the class should work as expected every time.
  • Since we need to ensure that the class will work, we don’t want other programmers changing important values or calling methods that change values in member variables (ie. We need to ensure that a valid date will be processed.)

Property Blocks

  • To keep programmers from referencing variables

that they shouldn’t we can make them private members.

  • We must then provide Property Blocks for private

variables that allow programmers to set and get the values stored in the private variables.

  • Select meaningful names for the property blocks

(Month), because that is how programmers will reference the values stored in the class variables.

  • Programmers will not reference the variable names

such as intMonth directly.

Improved DateMDY Class

DateMDY

  • month (now private)
  • day
  • year

+setMonth

+getMonth

+setDay

+getDay

+setYear

+getYear

+getDate

  • Notation:
    • private
    • public

    protected