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 Docsity.com 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 Docsity.com 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. Docsity.com 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. Docsity.com 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. Docsity.com 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. Docsity.com UML – Class Notation • Notation: - private + public # protected Class name Properties (variables) - + # Operations (methods) - + # Docsity.com 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). Docsity.com Date Class Notation DateMDY +month +day +year +getDate() • Notation: - private + public # protected Docsity.com 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. Docsity.com 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( ); Docsity.com 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( ) Docsity.com Using DateMDY Class private void btnProcessBday (…) { DateMDY bday = new DateMDY( ); bday.intMonth = 10; bday.intDay = 31; bday.intYear = 1980; txtDate.Text = bday.getDate( ); } Docsity.com 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. Docsity.com Improved DateMDY Class DateMDY -month (now private) -day -year +setMonth +getMonth +setDay +getDay +setYear +getYear +getDate • Notation: - private + public # protected Docsity.com 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”) ); } } Docsity.com Using Set Methods DateMDY bday = new DateMDY( ); bday.Month = 10; //use property block names to assign 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. Docsity.com 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… Docsity.com • Let’s take a look at the example, CS8ex. Docsity.com Read-Only Properties • This is used for variables that are maintained by the class, but may need to provide the value to the program at some time. • This could be used on running totals, counts, or constants used in the class. • The getDate method could be converted to a Read-Only Property. • To create a Read-Only property, only code the get method of the Property Block. Docsity.com Instance vs. Static (Shared) Variables • Variables defined in a class are normally considered Instance variables. • Each object created would have separate memory allocations for their own variables. • A static variable is a single variable for all objects. • It can be used to keep a running total, count, or it could be a constant. • Static members can be accessed without instantiating an object of the class. • Static members must be referenced using the ClassName even when an instance of the class has been created (ClassName.member) • Methods that reference a static variable should also be declared as static so they can referenced without creating an instance. • Methods that are declared as static, must only reference class variables that are static and/or local variables Docsity.com Defining Static public class DateMDY { //The current date is needed for all instances of DateMDY //We would need to assign the system date to these variables private static intCurrMonth, intCurrDay, intCurrYear; public static string CurrentDate { get { return (intCurrMonth.ToString(“N0”) + “/” + intCurrDay.ToString(“N0”) + “/” + intCurrYear.ToString(“N0”) ); } //No Set because it is read only. } } Docsity.com Using Static Variables DateMDY bday = new DateMDY; DateMDY dueDate = new DateMDY; //Static properties and methods are called using class name. lblCurrDate.Text = DateMDY.CurrentDate //There would be one CurrentDate for both bday. and dueDate. //and both have the same value Docsity.com Default Constructor • Default constructor has no parameters. • Always define a default constructor, even if it will not be doing any initialization. • If one is not defined, the compiler will generate one. • Usually used to assign a default value. public DateMDY ( ) { intMonth = 1; //assign default date intDay = 1; intYear = 1900; } Docsity.com Overloaded Constructors • Constructors can be overloaded. – Same method name but a different number or type of parameters. • Initial values can be passed at declaration if there is a parameterized constructor defined. public DateMDY (int m, int d, int y) { intMonth = m; intDay = d; intYear = y; } Docsity.com Property Blocks and Constructors • Use property blocks by using the property name, because any validation would be performed in the set method. public DateMDY (int m, int d, int y) { Month = m; //calls set procedure Day = d; Year = y; } Docsity.com Using Default Constructors public DateMDY ( ) { intMonth = 1; //assign default date intDay = 1; intYear = 1900; } • In the declaration below determine why is the default constructor called and what is returned by Date property. DateMDY bday = new DateMDY( ); txtDate.Text = bday.Date; Docsity.com Using Overloaded Constructors public DateMDY(int m, int d, int y) { Month = m; //calls set method Day = d; Year = y; } • In the declaration below determine why the overloaded constructor is called and what is returned by Date property. DateMDY payDate = new DateMDY(2, 26, 2002); txtDate.Text = payDate.Date; Docsity.com Destructor • A method that is automatically called when object goes out of scope. • Must be public. • The name of method is the class name preceded with a tilde (~) ~DateMDY( ) • Can NOT return a value. • Can be used to “cleanup” dynamic variables. • It is recommended that we do not declare one unless it is required, because the .Net Framework performs it own garbage collection (releases memory). Docsity.com Data Validation Definition public string Description { get { return mstrDescription; } set { if (value ! = “”) mstrDescription = value; else throw new System.Exception(“Description is missing.”); } } Docsity.com Data Validation Usage private void btnProcess(…) { try { objOrder = new clsOrder; objOrder.Description = txtDescription.Text; //… additional processing here } catch ex As Exception { MessageBox.Show("Error: " + ex.Message); } } Docsity.com Summary • 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 Docsity.com