classes

25
Classes CMPS 2143

Upload: morag

Post on 24-Feb-2016

39 views

Category:

Documents


0 download

DESCRIPTION

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. Terms. Instance – representative or example of a class - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Classes

Classes

CMPS 2143

Page 2: Classes

2

Overview (Chapter 4)•Terms•Class vs instance•Class definitions in various languages•Access modifiers•Methods•Constants•Separating definition and implementation•Properties•Class data• Inner classes

Page 3: Classes

3

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

Page 4: Classes

4

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

Page 5: Classes

5

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;

};

Page 6: Classes

6

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;}

Page 7: Classes

8

Superficial differencesC++ 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

Page 8: Classes

12

C++ (Revise PlayingCard)•Add a constructor (Ch 3)

•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

Page 9: Classes

13

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);}

Page 10: Classes

14

Colors getColor () { if ((suit() == Suits.Diamond) || (suit () == Suits.Heart))

return Color.Red; else return Color.Black; }

private:Suits suitValue;int rankValue;

bool faceUp;};

Page 11: Classes

15

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)

Page 12: Classes

16

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

Page 13: Classes

17

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

Page 14: Classes

18

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)

Page 15: Classes

19

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)

Page 16: Classes

20

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;}

Page 17: Classes

21

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 beholder2. 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

Page 18: Classes

22

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

Page 19: Classes

29

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

Page 20: Classes

30

C++ Forward Definitionclass Horse; //forward def

class Buggy { : Horse * myHorse;};

class Horse { : Buggy * myBuggy;};

Page 21: Classes

31

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::

Page 22: Classes

32

C++ example – Linked list with Nodesclass List { class Node { int value; Node * nextNode; //uses default constructor }

public: // put List Methods here :

private: Node * head; Node * cursor; int count;}

Page 23: Classes

33

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

Page 24: Classes

34

C++, Java, C# similar•Use static modifier

class Student { public: Student () { count++; studentID = count; } :

private: static int count = 20200000; int studentID;}

Page 25: Classes

36

Package Example on Y:/drive

•Visual Studio 2013 C++ Package class• Java Package class•Eclipse Project Package

•All in Y:/drive folder in lab