+ All Categories
Home > Documents > 1 INF160 IS Development Environments AUBG, COS dept Lecture 13 Title: Classes: Introduction & Review...

1 INF160 IS Development Environments AUBG, COS dept Lecture 13 Title: Classes: Introduction & Review...

Date post: 27-Dec-2015
Category:
Upload: linette-lang
View: 214 times
Download: 0 times
Share this document with a friend
Popular Tags:
66
1 INF160 IS Development Environments AUBG, COS dept Lecture 13 Title: Classes: Introduction & Review (Extract from Syllabus)
Transcript

1

INF160 IS Development Environments AUBG, COS dept

Lecture 13Title:

Classes: Introduction & Review

(Extract from Syllabus)

2

Lecture Contents:

OOP: Classes, Objects, Data members, Methods

Classes in VBasicClasses in C++Classes in C#Classes in JavaApplying Classes to build primitive IS

3

Demo Programs:

Group name: Test160dClass

Test160dClassVB (Module1.vb)Test160dClassCPPTest160dClassC#(Program.cs)Test160dClassJava

4

Formal introduction to concept of classes/objects

TransitionFrom Structured Programming to OOP

From Think in Functions to Think in Objects

From Algorithms + Data Structures = ProgramsTo Classes + Objects = Programs

5

Formal introduction to concept of classes/objects

OOP (Object Oriented Programming):

Data Encapsulation & Data HidingInheritancePolymorphism

6

The 3 main OOP characteristics

Data Encapsulation/Data Hiding Data and its functions are said to be

encapsulated into a single entity. Data is concealed within a class, so that

it cannot be accessed mistakenly by functions outside the class.

InheritancePolymorphism

7

The 3 main OOP characteristics

Data Encapsulation and Data Hiding

Inheritance The process of creating new classes,

called derived classes from existing classes or base classes

Polymorphism

8

The 3 main OOP characteristics

Data Encapsulation and Data HidingInheritancePolymorphism

Generally, the ability to appear in many forms

More specifically, in OOP it is the ability to redefine methods for derived classes

Ability to process objects differently depending on their data type or class

Giving different meanings to the same thing

9

Informal introduction to concept of classes/objects

Programs = Code + Data

10

Informal introduction to concept of classes/objects

Programs = Code + Data The Code concept

Code includes statements organized in routines

Code (including statements and routines) reads input data, processes data, generates output data

11

Informal introduction to concept of classes/objects

Programs = Code + Data The Data concept

Scalar dataData collections/Data containers

Evolution of the data concept

12

Informal introduction to concept of classes/objects

Evolution of the data concept

Scalar dataSimple data type or Data type used to store

a single value.Scalar variable stores a single value.

13

Informal introduction to concept of classes/objects

Evolution of the data concept

Data collections/Data containersData container: composite of related data

items stored in memory under the same name. Arrays Structures (also named Records) Classes

14

Informal introduction to concept of classes/objects

Evolution of the data concept

Data collections/Data containers: Arrays

Array: a homogeneous collection of data items of the same type.

15

Informal introduction to concept of classes/objects

Evolution of the data concept

Data collections/Data containers: Structures Structure/Record/: A heterogeneous collection of

simple variables A data type for a structurerecord composed of

multiple components. These components may be the same data type or may be not the same data type.

16

Informal introduction to concept of classes/objects

Evolution of the data concept

Data collections/Data containers

Classes = Structures + Routines

17

Informal introduction to concept of classes/objects

Evolution of the data concept

Data collections/Data containers Classes = Structures + Routines

Classes = Data Items + Member Functions

18

Informal introduction to concept of classes/objects

Evolution of the data concept

Data collections/Data containers Classes = Structures + Routines

Classes = Data Items + Member Functions

Classes = Data Fields + Methods

19

Informal introduction to concept of classes/objects

Classes = Abstract Data Types

Classes = User Defined Data Types

20

Informal introduction to concept of classes/objects

The relation basic type - variable int age; float score;

The relation class – object, class - instance Person mike, marry; SmallObj s; SparePart pieces[10];

21

Informal introduction to concept of classes/objects

Access to data fields and methods is regulated through access modifiers: Private Protected Public

Usually data fields are private Usually methods are public

22

Informal introduction to concept of classes/objects

Specific methods:

Constructors

Destructor

23

Informal introduction to concept of classes/objects

Specific methods:

Accessor / Getter methods

Mutator / Setter methods

24

Informal introduction to concept of classes/objects

Properties:

Getter and Setter methods interpreted as data fields

25

Classes in VBasic

D

26

The class is essentially an extension of the structure.

The class is = structure + Sub/Function procedures.

Class = Structure + procedure(s)

Sub/Function procedures are member procedures.

Sub/Function procedures are also called methods.

27

Example

In a professor’s program to assign and display semester grades, a student object might hold a single student’s name, an ID number, midterm mark and final mark.

A CalcSemGrade() method might calculate the student’s semester grade.

28

Example

In a computer program, a Counter object might be used as a GP programming element to count up and count down integer values.

IncCount() and DecCount() methods would tell the object to calculate for example going up and going down iterations.

29

Class Counter Private m_count As Integer ‘ data member Public Sub GetData() ‘ method – member function Console.Write("Enter data:") m_count = CInt(Console.ReadLine()) End Sub

Public Sub ShowData() ‘ method – member function Console.WriteLine("Counter data is = {0}", m_count) End Sub

Public Sub IncCount() ‘ method – member function m_count = m_count + 1 End Sub

Public Sub DecCount() ‘ method – member function m_count = m_count - 1 End Sub

End Class

30

Dim a As Counter a = New Counter

a.GetData()

a.IncCount() : a.IncCount() : a.IncCount()

a.ShowData()

31

Example

In a computer program, a Distance object might be used as a GP programming element to explore English measure system based on feet and inches.

AddDist1 and AddDist2 methods would tell the object to add for example two English style distances.

32

Class Distance

Private m_feet As Integer, m_inches As Single ‘ data members

Public Sub GetData() ‘ method – member function Console.Write("Enter feet:") m_feet = CInt(Console.ReadLine()) Console.Write("Enter inches:") m_inches = CSng(Console.ReadLine()) End Sub

Public Sub ShowData() ‘ method – member function Console.WriteLine("Distance data is feet={0} inches={1}", m_feet, m_inches) End Sub

Public Sub AddDist1(ByVal d1 As Distance, ByVal d2 As Distance) m_feet = d1.m_feet + d2.m_feet m_inches = d1.m_inches + d2.m_inches End Sub

Public Function AddDist2(ByVal d As Distance) As Distance Dim temp As Distance temp = New Distance temp.m_feet = m_feet + d.m_feet temp.m_inches = m_inches + d.m_inches Return temp End Function

End Class

33

Dim aa, bb, cc As Distance aa = New Distance(3, 3.0) bb = New Distance(6, 6.0) cc = New Distance

aa.ShowData() : bb.ShowData() : cc.ShowData()

cc.AddDist1(aa, bb) cc.ShowData()

cc = aa.AddDist2(bb) cc.ShowData()

34

General Template for a Class

Class className

Private member variable declarations

Public Property X ... End Property

Sub A() ... End Sub

End Class

Properties

Methods

Interface

35

Private Data

• The following declaration may be inside a class:

Private m_name As type

• The word Private is used to ensure that the variable cannot be accessed directly from outside the class – data hiding.

• The variable is an internal variable, local to the class.

• m_name is called an instance variable or member variable.

• It is used to hold the value of a property.

36

Example

Class Student Private m_name As String ‘Name Private m_id As String ‘ID Private m_midterm As Double ‘Midterm mark Private m_final As Double ‘Final mark

...End Class

Dim Pupil As Student ‘pupil is of type StudentPupil = New Student() ‘create instance of pupil

‘OR

Dim Pupil As New Student()

37

Property Block

Member variables are only accessed indirectly via a Property block.

Property values are set (i.e. written) by the Set procedure

Property values are got (i.e. read) by the Get procedure

Attention! The Property concept comes to systematically/formally replace/substitute the pair of methods introduced as GetData() & ShowData() as in the Counter/Distance examples of class templates

You are recommended to use Properties instead of GetData()/ShowData()-like class methods

38

Get and Set

Public Property Name() As String

Get

Return m_name ‘ read value of property

End Get

Set(ByVal Value As String)

m_name = Value ‘ write value of property

End Set

End Property

Propertyblock

Example:External identifier of

property – may be accessed

39

Example class Counter with property block

Class Counter

Private m_count As Integer . . .

Public Property Count() As Integer

Get Return m_count ' read value of property End Get

Set(ByVal param As Integer) m_count = param ' write value of property End Set

End Property . . .

End Class

40

Example class Counter with property block

Dim d As New Counterd.Count = 880Console.WriteLine _

("Counter data displayed through property = {0}", d.Count)

Dim var As Integervar = d.CountConsole.WriteLine _

("Counter data displayed through assignment = {0}", var)

41

Object Constructors

• Each class may have one or more special method(s) called constructor(s) that is/are always invoked when the object is instantiated.

• The constructor(s) may take or may not take parameters.

• It is used to perform tasks to initialize the object.

• The first line of no-argument constructor has the form:

Public Sub New()

• The first line of constructor with argument(s) has the form:

Public Sub New(ByVal par1 As dataType, ...)

42

‘ Constructors with direct access to data member‘ m_count

Class Counter

Private m_count As Integer

. . .

Public Sub New()

m_count = 0

End Sub

Public Sub New(ByVal val As Integer)

m_count = val

End Sub

. . .

End Class

43

‘ Constructors with indirect access to data member

‘ m_count using Property blockClass Counter

Private m_count As Integer

Public Property Count() As Integer

Get

Return m_count ' read value of property

End Get

Set(ByVal param As Integer)

m_count = param ' write value of property

End Set

End Property

Public Sub New()

Count = 0

End Sub

Public Sub New(ByVal val As Integer)

Count = val

End Sub

End Class

44

Dim b, c As Counter

b = New Counter

b.ShowData()

c = New Counter(20)

c.ShowData()

45

Classes in VBOpen the source text Module1.vb

Compile and run the VB program

Expand the functionality of the Book class with data fields and methods for the book author, for the book publisher, for the book number of pages and for the price of the book.

46

Classes in C++

D

47

Classes in C++Open the source text TEst160dClassesCPP.cpp

Compile and run the C++ program

Expand the functionality of the Book class with data fields and methods for the book author, for the book publisher, for the book number of pages and for the price of the book.

48

Classes in C++class Book{private: string m_Title;public: Book() { m_Title = " ";}

Book(string pb){

m_Title = pb;}// method accessorstring getTitle() { return m_Title; }// methods mutators

void setTitle(string pb) { m_Title = pb; }

void printBook(){

cout << endl;cout << m_Title;cout << endl;

}};

49

Classes in C++int main(){

Book myBook;myBook.setTitle("Formal Languages and Language Prcessors");cout << endl;cout << myBook.getTitle();cout << endl;

Book linBook("Operating Systems");cout << endl;cout << linBook.getTitle();cout << endl;

myBook.printBook();linBook.printBook();

return 0;}

50

Classes in C#

D

51

Classes in C#Open the source text Program.cs

Compile and run the C# program

Expand the functionality of the Book class with data fields and methods for the book author, for the book publisher, for the book number of pages and for the price of the book.

52

Classes in C# class Book{private string m_Title;public Book() { m_Title = " ";}public Book(string pb)

{m_Title = pb;

}// method accessor

public string getTitle() { return m_Title; }// methods mutators

public void setTitle(string pb) { m_Title = pb; } // property Title for data member m_Titlepublic string Title // property{ get { return m_Title; } set { m_Title = value; }} // end of property

public void printBook(){

Console.WriteLine();Console.WriteLine(" {0}",m_Title);Console.WriteLine();

}} // end of class Book

53

Classes in C# class Program { static void Main(string[] args) { Book myBook = new Book(); myBook.Title = "Formal Languages & Language Processors"; // property // or myBook.setTitle("Formal Languages & Language Processors"); // setter/mutator method //myBook.Publisher() = "TU SOfia"; //myBook.NumPages() = 190; //myBook.Price() = 5.0;

Book linBook = new Book("Operating Systems");

//' printing Book structure Console.WriteLine(); Console.WriteLine("{0}", myBook.Title); // property // or Console.WriteLine("{0}", myBook.getTitle() ); // getter method Console.WriteLine();

myBook.printBook(); linBook.printBook(); }// end of Main }}

54

Classes in Java

D

55

Classes in JavaOpen source text Test160dClassesJava.java

Compile and run the Java program

Expand the functionality of the Book class with data fields and methods for the book author, for the book publisher, for the book number of pages and for the price of the book.

56

Classes in Javaclass Book{private String m_Title;public Book() { m_Title = " ";}public Book(String pb)

{m_Title = pb;

}// method accessor

public String getTitle() { return m_Title; }// methods mutators

public void setTitle(String pb) { m_Title = pb; } // property Title for data member m_Title

// NO PROPERTIES in JAVA

public void printBook(){

System.out.println();System.out.println(" "+ m_Title);System.out.println();

}} // end of class Book

57

Classes in Javapublic static void main(String[] args) { Book myBook = new Book(); //myBook.Author = "lin & Bon"; myBook.setTitle("Formal Languages & Language Processors"); // set method //myBook.Publisher() = "TU SOfia"; //myBook.NumPages() = 190; //myBook.Price() = 5.0;

Book linBook = new Book("Operating Systems");

//' printing Book structure System.out.println(); System.out.println(" " + myBook.getTitle() ); // getter method System.out.println();

myBook.printBook(); linBook.printBook(); }

58

Applying Classes to build primitive IS

Examples of elementary IS: Books Journals Family members Students in INF160 class

Approaches to develop simple IS: Using many scalar variables Using parallel arrays Using Class template and array of Classes

59

Using many scalar variables

E

60

Using parallel arrays

E

61

Using Class template and array of Classes

E

62

To introduce UML before BlueJ

63

Class SmallObj class SmallObj{ private: int somedata;

public: void SetData(int d) {

somedata = d; }

void ShowData() {

cout << "\nData is =" << somedata; }

};

64

SmallObj – UML class diagram

SmallObj SmallObj

somedata -somedata

SetData(int) +SetData(int)

ShowData() +ShowData()

65

SmallObj – UML class diagram after G.Booch cloud form

SmallObj SmallObj somedata -somedata

SetData(int) +SetData(int) ShowData() +ShowData()

66

Thank You For

Your Attention!


Recommended