Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming Part 1 07_classes.ppt

Similar presentations


Presentation on theme: "Object-Oriented Programming Part 1 07_classes.ppt"— Presentation transcript:

1 Object-Oriented Programming Part 1 07_classes.ppt
CIS162AD Object-Oriented Programming Part 1 07_classes.ppt

2 Overview of Topics Object-Oriented Programming (OOP) Class vs Object
Unified Modeling Language (UML) Defining Classes Using Classes A lot of information is introduced – be patient with yourselves as you absorb it. CIS162AD

3 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. CIS162AD

4 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. CIS162AD

5 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. CIS162AD

6 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. CIS162AD

7 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: Both include the same properties and methods. The objects based on clsOrder will be standardized across the programs that use the clsOrder class. CIS162AD

8 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… CIS162AD

9 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. CIS162AD

10 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. CIS162AD

11 UML – Class Notation Notation: Class name Properties (variables) - + #
Operations (methods) Notation: - private + public # protected CIS162AD

12 Class Outline Class Name Properties (variables) Operations (methods)
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). CIS162AD

13 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. CIS162AD

14 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… CIS162AD

15 Date Class Notation Notation: DateMDY - private + public # protected
+month +day +year +getDate() Notation: - private + public # protected CIS162AD

16 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. CIS162AD

17 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; CIS162AD

18 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… CIS162AD

19 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. CIS162AD

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

21 Using DateMDY Class private void btnProcessBday (…)
{ DateMDY bday = new DateMDY; bday.intMonth = 10; bday.intDay = 31; bday.intYear = 1980; txtDate.Text = bday.getDate( ); } CIS162AD

22 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. CIS162AD

23 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.) CIS162AD

24 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. CIS162AD

25 Improved DateMDY Class
month (now private) day year +setMonth +getMonth +setDay +getDay +setYear +getYear +getDate Notation: - private + public # protected CIS162AD

26 Improved DateMDY Definition
public class DateMDY { private int intMonth, intDay, intYear; public int Month { get { return intMonth; } set { intMonth = value; //value required keyword } //need property procedures for Day and Year public string getDate( ) As String { return (intMonth.ToString(“N0”) + “/” + intDay.ToString(“N0”) + “/” + intYear.ToString(“N0”) ); } CIS162AD

27 Using Set Methods DateMDY bday = new DateMDY;
bday.Month = 10; //use property block names bday.Day = 31; //Automatically calls set method bday.Year = 1980; txtDate.Text = bday.getDate( ) //following is now illegal because intMonth is private. bday.intMonth = 11; //intMonth exists, but cannot be referenced directly. CIS162AD

28 Using Get Methods DateMDY bday = new DateMDY; int intMyMonth;
intMyMonth = bday.Month //Automatically calls Get method //following is now illegal because intMonth is private. intMyMonth = bday.intMonth; CIS162AD

29 Complete Support of Class
Since the variables are private, only member methods can change or get the values. So the class should have methods that allow the manipulation of the data. What if we want to know what the date will be 30, 60, or 90 days from now? What if we want to know the date 30, 60, or 90 days ago? We need methods that allows us to do this (ie): addDays(int numberOfDays) subtractDays(int numberOfDays) CIS162AD

30 Date Considerations Increase days first, then month, then year.
Leap year? Display date in various formats: MM/DD/YYYY Month DD, YYYY Etc. Before taking this example too far, you should know that C# includes a DateTime class… CIS162AD

31 Summary Object-Oriented Programming (OOP) Class vs Object
Unified Modeling Language (UML) Defining Classes Using Classes OOP is a complex topic. Don’t expect to understand it completely the 1st or 2nd time out. Be patient with yourself. It should begin to gel as you work through the next few assignments. CIS162AD


Download ppt "Object-Oriented Programming Part 1 07_classes.ppt"

Similar presentations


Ads by Google