Class & Object 蔡柏灃.

Slides:



Advertisements
Similar presentations
Web Application Development Slides Credit Umair Javed LUMS.
Advertisements

CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look 1 Xiang Lian The University of Texas – Pan American Edinburg,
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Written by: Dr. JJ Shepherd
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Road Map Introduction to object oriented programming. Classes
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
CS-212 Intro to C++. Abstract Data Type Abstraction of a real world object as a mathematical entity Implementation independent model of the entity Emphasis.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
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.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 9 Classes: A Deeper Look, Part I Part II.
在職教師教育網路應用系統程式 分享與實作研習 ---C# Tutorial(Code C) 行政網路組 傅志雄.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Object-Oriented Programming Chapter Chapter
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
1 CSC241: Object Oriented Programming Lecture No 05.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Creating and Using Objects, Exceptions, Strings
Classes (Part 1) Lecture 3
Classes C++ representation of an object
Encapsulation, Data Hiding and Static Data Members
Encapsulation and Constructors
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Can perform actions and provide communication
CIS16 Application Development and Programming using Visual Basic.net
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Classes C++ representation of an object
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Class & Object 蔡柏灃

Object Oriented Programming Basic concepts Everything is an object Blueprint and its realization Ex1: A building Ex2: A dog Encapsulation Inheritance Polymorphism

Encapsulation Information of objects should be encapsulated Data and corresponding accessing methods Check and constrain accessing (access-modifiers) Safety and reusability

Class & Object Class Object 1 class  lots of objects ! An user-defined type Data (Properties) & Behaviors (Methods) The Main() Object An entity of specific characteristics/functions existing in the memory – Normally, you are using objects not classes ! The new keyword The dot ( . ) operator 1 class  lots of objects !

Access modifiers It is strongly recommended to put the modifiers EXPLICITLY !!

Examples Example 4_1 Example 4_1_modified (access modifiers) We build a time class and provide a method to display the time message Example 4_1_modified (access modifiers) Not a good example (public on variables) Getter and setter Property (introduce later)

Property Property is used to access the private variables Getter: only provide get You have to return Setter: only provide set Keyword value Example 4_1_modified_2

using System; namespace example4_1 { public class Time // private variables private int Year = 2008; private int Month = 1; private int Date = 12; private int Hour = 8; private int Minute = 0; private int Second = 0; // public methods public void DisplayCurrentTime( ) Console.WriteLine("Time --> {0}/{1}/{2} AM {3}:{4}:{5}", Year, Month, Date, Hour, Minute, Second); } // properties public int YEAR get { return Year; } set { Year = value; } public int MONTH get { return Month; } set { Month = value; } public class Tester static void Main( ) Time t = new Time( ); t.YEAR = 2007; t.MONTH = 11; t.DisplayCurrentTime( );

Exercise 10 A simple personal telephone book Please build a class that records your friends’ names and mobile phone numbers Provide a display function that displays these messages

Constructors It is used to initialize an object when it is firstly constructed by CLR Every class has at least a constructor If you don’t provide one, an empty constructor is provided automatically and implicitly If you provide one, this default one will no longer exist Should always has the same name as the class Public No return (not even void) Example 10_modified Keyword this

Exercise 11 A simple calculator Please try to write a simple calculator that provides some basic arithmetic operations for two integer inputs Max Min Mean Static methods

A system time displayer (example 4_3) System.DatTime class Provided by C# Used to store time System.DateTime.Now is a property to get the current time Don’t you don’t have to new anything? Keyword static You can access the methods or properties by ClassName.xxx static to static

static members Once you put the static keyword on you class member, it will be kept in the memory You don’t have to new any object in order to use it There is only one static member for all of you objects No copies

Telephone book counter How do you keep a counter that records the number of your friends recorded in the phone book ? Example 10_modified_2

Exercise 12 Cats and dogs adoption system Please write a system that keeps the numbers of cats and dogs and provides functions that modifies these numbers when any cat or dog is adopted or newly coming catAdopted(int number) catNew(int number) Property CatNumber …