Class. 2 Objectives Discuss class basics –fields –methods –access levels Present object creation –new operator.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Lecture 2: Object Oriented Programming I
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Enhancing classes Visibility modifiers and encapsulation revisited
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
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.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
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.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
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.
Introduction to Object-Oriented Programming Lesson 2.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Topics Instance variables, set and get methods Encapsulation
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.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Procedural and Object-Oriented Programming
Classes (Part 1) Lecture 3
Review What is an object? What is a class?
Delegates and Events 14: Delegates and Events
Methods Attributes Method Modifiers ‘static’
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
More Object Oriented Programming
Object Based Programming
METHODS AND BEHAVIORS AKEEL AHMED.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes and Data Abstraction
Encapsulation and Constructors
Classes, Encapsulation, Methods and Constructors (Continued)
Classes and Objects.
Object Oriented Programming in java
CIS 199 Final Review.
Defining Classes and Methods
Corresponds with Chapter 5
Creating and Using Classes
Presentation transcript:

Class

2 Objectives Discuss class basics –fields –methods –access levels Present object creation –new operator

3 Class Class represents concept in application domain –defines a new type –contains data and operations –fundamental to object oriented programming class Stock {... } class CheckingAccount {... } class Rectangle {... } class Aircraft {... } class Rational {... }

4 Instance fields Class instance fields store object state –each instance gets own copy –also called instance variables instance fields class Stock { string name; double price; int shares;... }

5 Object creation Objects created using new operator –memory allocated for all instance fields –object also called instance –object creation also called instantiation or activation name price shares name price shares ibm sun Stock ibm = new Stock(); Stock sun = new Stock(); allocate objects

6 Member access Object members accessed using dot operator Stock ibm = new Stock(); Stock sun = new Stock(); ibm.name = "IBM"; ibm.price = 56.0; ibm.shares = 100; sun.name = "SUN"; sun.price = 10.0; sun.shares = 200; access fields name IBM price 56.0 shares 100 name SUN price 10.0 shares 200 ibm sun

7 Method Class methods implement the behavior of the class –specify return type, name, parameters, and body –use void if no value returned methods class Stock { void Buy(int shares) {... } void Sell(int shares) {... } void SetPrice(double price) {... } double Value() {... }... }

8 Method invocation Method invoked on object –use dot operator –pass parameters Stock ibm = new Stock(); ibm.SetPrice(56.0); ibm.Buy(100); ibm.SetPrice(99.0); ibm.Sell(50); call methods

9 Method implementation Method implementation uses keyword this –handle to object on which method was called –used to access members from inside method implement method class Stock { string name; double price; int shares; void Buy(int s) { this.shares += s; }... } use this Stock ibm = new Stock(); ibm.SetPrice(56.0); ibm.Buy(100); name price 56.0 shares 100 ibm/this

10 Required this Must use this when local name conflicts with member name parameter and field have same name class Stock { string name; double price; int shares; void Buy(int shares) { this.shares += shares; }... } must use this

11 Omitting this Can omit this when there is no ambiguity –class members accessed implicitly class Stock { string name; double price; int shares; void Buy(int s) { shares += s; }... } omit this

12 Member declaration order Members may be declared in any order –no requirement to declare before use –different rule than local variables class Stock { void Buy(int s) { shares += s; } string name; double price; int shares;... } use declare

13 Return value Use return to send data out of method –value sent back to caller return class Stock { double Value() { return price * shares; }... } Stock ibm = new Stock(); ibm.SetPrice(56.0); ibm.Buy(100); double v = ibm.Value();

14 Method overloading Can have several methods with same name –parameters lists must be different –compiler selects version to call based on passed parameters –cannot overload on return type alone overload class Stock { void Buy(int shares) { this.shares += shares; } void Buy(int shares, double price) { this.shares += shares; this.price = price; }... } Stock ibm = new Stock(); ibm.Buy(100, 56.0); call ( int, double ) version of Buy

15 Value parameter Pass by value is default parameter passing mechanism –data copied into method –any changes to parameter inside method affect local copy only value parameter void F(int x) { x = 0; } int y = 9; F(y); y unchanged change local copy

16 Ref parameter ref parameter passes data in and out –use keyword ref in definition and call –must use variable in call –must initialize passed variable before call ref parameter, initially 9 void H(ref int x) { x = x * 2; } int y = 9; H(ref y); y set to 18

17 Out parameter out parameter returns data through parameter –use keyword out in both definition and call –actual parameter must be a variable, cannot pass literal value –must assign to parameter inside method or compiler error out parameter void G(out int x) { x = 11; } int y; G(out y); y set to 11 assign to parameter

18 Member default access Class members default to private –redundant to use keyword private equivalent class Stock { string name; private double price; private int shares; void Buy (int shares) {... } private void Sell(int shares) {... }... }

19 Meaning of private Access to private member limited to code in that class only –compiler error to attempt access from external class access ok in Stock class class Stock { private int shares; void Sell(int shares) { this.shares = shares }... } class Test { static void Main() { Stock ibm = new Stock();... ibm.shares = 9; } error to access in other class private member

20 Public member access Members can be made public –can then be accessed outside the class public class Stock { private string name; private double price; private int shares; public void Buy (int shares) {... } public void Sell (int shares) {... } public void SetPrice(double price ) {... } public double Value () {... } }

21 Encapsulation Access levels used to separate interface from implementation –interface made public –implementation made private Separation allows implementation to be easily changed –without breaking user code –supports object-oriented principle of encapsulation

22 Naming Naming guidelines –class: intercaps with initial capital –method: intercaps with initial capital –method parameter: intercaps with initial lower case –public field: intercaps with initial capital –private field: intercaps with initial lower case follow naming guidelines class SavingsAccount { public void Deposit(double amountOfDeposit) {... } public int GetAccountNumber() {... } public double Balance; private int accountNumber;... }

23 Summary Class is basic unit of C# programming –contains data (fields) and operations (methods) Several parameter passing options available –value, out, ref Several options for protection level of class members –supports encapsulation and information hiding