Classes CMPS 2143. Overview Terms Class vs instance Class definitions in various languags Access modifiers Methods Constants Separating definition and.

Slides:



Advertisements
Similar presentations
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Advertisements

Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Road Map Introduction to object oriented programming. Classes
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Inheritance and Polymorphism CS351 – Programming Paradigms.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
UML Basics & Access Modifier
CSSE501 Object-Oriented Development. Chapter 5: Messages, Instances and Initialization  This chapter examines OOP run-time features: Object Creation.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Messages, Instances, and Initialization (Methods) CMPS 2143.
The Java Programming Language
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
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 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.
Agenda Object Oriented Programming Reading: Chapter 14.
Lecture 4 : Data Abstraction with C++ : class. Data Abstraction Creating classes with appropriate functionality, which hide the unnecessary details Data.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
C++ for Java Programmers
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Lecture 4 : Data Abstraction with C++ : class
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Abstract Data Types and Encapsulation Concepts
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Introduction to Classes
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Lecture 9 Concepts of Programming Languages
Introduction to Classes
Interface.
Abstract Data Types and Encapsulation Concepts
Packages and Interfaces
Interfaces.
Chapter 14 Abstract Classes and Interfaces
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Lecture 8 Object Oriented Programming (OOP)
CSG2H3 Object Oriented Programming
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Classes CMPS 2143

Overview Terms Class vs instance Class definitions in various languags Access modifiers Methods Constants Separating definition and implementation Properties Class data Inner classes 2

Terms Instance – representative or example of a class Instance variable – internal variable owned by a class ▫ Aka data field or data member State – described by the instance variables and their values Behavior – characterized by methods Glossary at the end of the book 3

Class Definitions We’ll start with the playing card abstraction – ▫ Container for two data values  Rank: Ace – King represented by 1-13  Suit: enum type We’ll see this in several different languages 4

C++ (class declaration/definition) class PlayingCard { public: enum Suits {Spade, Diamond, Club, Heart}; Suits getSuit () {return suitValue}; int getRank () {return rankValue} private: Suits suitValue; int rankValue; }; 5

Java SE6+ /C# (class declaration/definition) public class PlayingCard { public enum Suits {Spade, Diamond, Club, Heart}; public Suits getSuit () {return suitValue}; public int getRank () {return rankValue} private Suits suitValue; private int rankValue; } 6

C# (class declaration/definition) public class PlayingCard { public enum Suits {Spade, Diamond, Club, Heart}; public Suits getSuit () {return suitValue}; public int getRank () {return rankValue} private Suits suitValue; private int rankValue; } 7

Superficial differences C++Java / C# Semicolon Visibility modifiers mark an entire block aCard.suit() = PlayingCard::Heart; enum No semicolon Modifiers placed on EVERY declaration aCard.suit() = PlayingCard.Suits.Heart; enums in Java 6+ and C# ▫ In earlier Java, declare final static variables  final means they are constant  static means they are shared by all instances of class  Note: Ask for a drawing on board 8

Apple Object Pascal and Delphi Pascal Based on Pascal, but extended Both have enumerated types Object Pascal uses keyword object to declare a class (object types) and Delphi Pascal uses class Delphi Pascal requires EVERY class to inherit from some existing class, hence it has a Tobject Delphi, all classes start with T by convention Delphi uses visibility modifiers, Object Pascal des not Delphi requires a constructor 9

Smalltalk Doesn’t have a textual representation of a class Classes are described using an interactive interface (like a IDE) ▫ Using this, programmer defines a new class ▫ Uses a message sent to parent class Object ▫ Like Delphi, requires class to name a specific parent class Do not have visibility modifiers - all data fields are private by default 10

Other languages Clos (Lisp-like) ( defclass PlayingCard () (rank suit)) Eiffel – see book Objective-C – see book Python – uses indentation, not beginning/ending tokens class PlayingCard: “A playing card class” def __init__ (self, s, r); self.suit = s self.rank = r 11

C++ (Revise PlayingCard) Add a constructor Add a method to return the face color of the card (red or black) Add a data field to maintain whether the card is faceup or facedown, and methods to test the state of this value and to flip the card. Add a second enumerated type to represent the colors 12

class PlayingCard { public: enum Suits {Spade, Diamond, Club, Heart}; enum Colors {Red, Black}; PlayingCard (Suits s, int r) { suitValue = s; rankValue = r;} Suits getSuit () {return suitValue}; int getRank () {return rankValue}; void setFaceUp (bool up) {faceUp = up;} void flip () {setFaceUp (!faceUp);} 13

Colors getColor () { if ((suit() == Suits.Diamond) || (suit () == Suits.Heart)) return Color.Red; else return Color.Black; } private: Suits suitValue; int rankValue; bool faceUp; }; 14

Comments on C++ revised PlayingCard Most OOP guidelines indicate member data always private or protected Constructor is a special method with same name as class Methods that return values are termed accessors and often use get in the method name ( getRank ) Methods that return boolean values are special accessors known as predicates ( isFaceUp ) ▫ Why not make the member data public? Methods that set values are termed mutators or setters ( setFaceUp ) 15

Comments on C++ revised PlayingCard Method flip? Not an accessor, text says not a mutator  just a method Method getColor? Not getting and returning a value of a data member  just a method But I – me, personally ▫ think flip is a mutator as it changes state ▫ getColor is accessing a data field to return a value based on it 16

Order of methods in class declaration Style guidelines say ▫ Important features listed earlier ▫ Constructors are most important, so appear near top ▫ Methods grouped – alphabetically or by purpose ▫ Private data fields and private methods are for the developer and should be near the end of the class 17

Constants or immutable data fields A data field that once sets, cannot subsequently be changed As this restriction protects the data field, no need to make it private C++ const int MAXSIZE = 100; Java public final int MAXSIZE = 100; If not set to a value, MUST be set in constructor(s) 18

Separating definition and implementation Java and C# place body of method directly in class declaration Many languages separate these two aspects C++ you have a choice ▫ Small methods can be defined in the class ▫ Larger methods defined outside (e.g. getColor would be a good choice) 19

class PlayingCard {//in header file public: : Colors color (); : } Colors PlayingCard::getColor ( ) //in.cpp file { if ((suit() == Suits.Diamond) || (suit () == Suits.Heart)) return Color.Red; else return Color.Black; } 20

Two reasons to put method body outside class definition 1.Too long and the method body obscures other features in class definition, so we do it for readability ▫ Eye of the beholder 2.When methods bodies are in-line, the C++ compiler is permitted to expand invocations of the method directly in the client code without creating a function call ▫ Executed much faster as a result 21

Other languages Objective-C separates class definition from class imlementation Object Pascal and Delphi separate them into an interface section and an implementation section, but keep them in same file In C++, you can separate them and keep them in same file also 22

Other languages: CLOS (defclass PlayingCard () ((rank :accessor getRank) (suit :accessor getSuit) ) 23

Other languages: Python class PlayingCard: “A playing card class” def __inti__ (self, s, r): self.suit =s self.rank = r def rank (self) return self.rank def color (self) if self.suit == 1 or self.suit == 2 return 1 return 0 24

Interfaces – Save for Chapter 8 An interface defines the protocol for certain behavior, but does not provide an implementation Java is one such language public interface IStoring { void writeOut (Stream s); void readFrom(Stream s); }; 25

Interfaces Like a class, an interface defines a new type IStoring storableValue; A class can implement the protocols defined by an interface – doing something “inherit-like” public class BitImage implements Istoring { public void writeOut (stream s) { code … } : } storableValue = new BitImage( ); 26

Properties Smalltalk, Delphi, Visual Basic, C# and others have properties A property is manipulated syntactically like a data field, but really operates like a method ▫ Delphi uses keyword property ▫ Smalltalk – has accessor methods with same name as data field ▫ C# has special methods with same name as data field …the convention is to capitalize their name 27

C# Properties public class PlayingCard { public int RankValue { get {return rankValue;} //MUST return a value set {rankValue = value;} //value is a pseudo-var } : private int rankValue; : } Property without a get is a write-only property Property without a set is a read-only property 28

Forward Definitions Two or more classes may need access to one another (RARE) Mutual recursion Example: Horse and Buggy Java easy – it is a multi-pass compiler C++ - must use a forward definition 29

C++ Forward Definition class Horse; //forward def class Buggy { : Horse * myHorse; }; class Horse { : Buggy * myBuggy; }; 30

Inner or Nested Classes Java and C++ allow nested classes ▫ Java – inner class C++ - nested class Similar in appearance, but different semantically Java ▫ inner class is linked to a specific instance and has access to data fields and methods in the object ▫ Can have an inner class with no name, too C++ - simply a naming device (restricts visibility with the inner class. ▫ If you need the nested class outside the class ▫ List::Link:: similar to the enum type Package::Shipping:: 31

C++ example – Linked list with Nodes class List { class Node { int value; Node * nextNode; //uses default constructor } public: // put List Methods here : private: Node * head; Node * cursor; int count; } 32

Class Data Fields A common data field that is shared by all instances of a class create a paradox – who initializes it? ▫ Every instance (reinitializes over and over) X ▫ No instances do (leaves it uninitialized) X Move outside the simple class/method/instance paradigm Another mechanism needed ▫ Memory manager initializes it to a default value ▫ Every instance can test for this and see if they are first and then initialize it to value desired 33

C++, Java, C# similar Use static modifier class Student { public: Student () { count++; studentID = count; } : private: static int count = ; int studentID; } 34

Classes as Objects Save for Chapter 25 35

Package Example Visual Studio 2010 C++ Package class Java Package class Eclipse Project Package All in Y:/drive folder in lab 36