Object-Oriented Programming Part 1 07_classes.ppt

Slides:



Advertisements
Similar presentations
C++ Classes & Data Abstraction
Advertisements

CIS162AD Inheritance 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Constructors & Inheritance.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
ASP.NET Programming with C# and SQL Server First Edition
CIS162AD - C# Methods, Menus, and Dialog Boxes 05_methods_menus.ppt.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
110-I 1 Object Terminology Review Object - like a noun, a thing –Buttons, Text Boxes, Labels Properties - like an adjective, characteristics of object.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CIS162AD Inheritance Part 3 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Abstract Classes and.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
CIS162AD – C# Call-by-Reference Methods 06_methods_by_ref.ppt.
CIS162AD Constructors Part 2 08_constructors.ppt.
Class Fundamentals BCIS 3680 Enterprise Programming.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Introduction to Object-oriented Programming
Classes C++ representation of an object
Programming Logic and Design Seventh Edition
3 Introduction to Classes and Objects.
Classes and OOP.
Topics Procedural and Object-Oriented Programming Classes
Microsoft Visual Basic 2005: Reloaded Second Edition
About the Presentations
Introduction to Classes
Classes and Objects 2nd Lecture
Procedural Abstraction Object-Oriented Code
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
IFS410: Advanced Analysis and Design
Lecture 22 Inheritance Richard Gesick.
Chapter 9 Objects and Classes
Learning Objectives Classes Constructors Principles of OOP
Defining Classes and Methods
Classes, Objects, Methods and Strings
Object-Oriented Programming: Classes and Objects
Introduction of Programming
Outline Anatomy of a Class Encapsulation Anatomy of a Method
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Java Programming Language
Defining Classes and Methods
Classes C++ representation of an object
Creating and Using Classes
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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