Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 INF160 IS Development Environments AUBG, COS dept Lecture 13 Title: Classes: Introduction & Review (Extract from Syllabus)

Similar presentations


Presentation on theme: "1 INF160 IS Development Environments AUBG, COS dept Lecture 13 Title: Classes: Introduction & Review (Extract from Syllabus)"— Presentation transcript:

1 1 INF160 IS Development Environments AUBG, COS dept Lecture 13 Title: Classes: Introduction & Review (Extract from Syllabus)

2 2 Lecture Contents: §OOP: Classes, Objects, Data members, Methods §Classes in VBasic §Classes in C++ §Classes in C# §Classes in Java §Applying Classes to build primitive IS

3 3 Demo Programs: Group name:Test160dClass §Test160dClassVB (Module1.vb) §Test160dClassCPP §Test160dClassC#(Program.cs) §Test160dClassJava

4 4 Formal introduction to concept of classes/objects §Transition §From Structured Programming to OOP §From Think in Functions to Think in Objects §From Algorithms + Data Structures = Programs To Classes + Objects = Programs

5 5 Formal introduction to concept of classes/objects §OOP (Object Oriented Programming): §Data Encapsulation & Data Hiding §Inheritance §Polymorphism

6 6 The 3 main OOP characteristics §Data Encapsulation/Data Hiding l Data and its functions are said to be encapsulated into a single entity. l Data is concealed within a class, so that it cannot be accessed mistakenly by functions outside the class. §Inheritance §Polymorphism

7 7 The 3 main OOP characteristics §Data Encapsulation and Data Hiding §Inheritance l The process of creating new classes, called derived classes from existing classes or base classes §Polymorphism

8 8 The 3 main OOP characteristics §Data Encapsulation and Data Hiding §Inheritance §Polymorphism l Generally, the ability to appear in many forms l More specifically, in OOP it is the ability to redefine methods for derived classes l Ability to process objects differently depending on their data type or classdata typeclass l Giving different meanings to the same thing

9 9 Informal introduction to concept of classes/objects §Programs = Code + Data

10 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 11 Informal introduction to concept of classes/objects §Programs = Code + Data §The Data concept §Scalar data §Data collections/Data containers §Evolution of the data concept

12 12 Informal introduction to concept of classes/objects §Evolution of the data concept §Scalar data §Simple data type or Data type used to store a single value. §Scalar variable stores a single value.

13 13 Informal introduction to concept of classes/objects §Evolution of the data concept §Data collections/Data containers §Data container: composite of related data items stored in memory under the same name. l Arrays l Structures (also named Records) l Classes

14 14 Informal introduction to concept of classes/objects §Evolution of the data concept §Data collections/Data containers: Arrays l Array: a homogeneous collection of data items of the same type.

15 15 Informal introduction to concept of classes/objects §Evolution of the data concept  Data collections/Data containers : S tructures l Structure/Record/: A heterogeneous collection of simple variables l 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 16 Informal introduction to concept of classes/objects §Evolution of the data concept §Data collections/Data containers l Classes = Structures + Routines

17 17 Informal introduction to concept of classes/objects §Evolution of the data concept §Data collections/Data containers l Classes = Structures + Routines l Classes = Data Items + Member Functions

18 18 Informal introduction to concept of classes/objects §Evolution of the data concept §Data collections/Data containers l Classes = Structures + Routines l Classes = Data Items + Member Functions l Classes = Data Fields + Methods

19 19 Informal introduction to concept of classes/objects §Classes = Abstract Data Types §Classes = User Defined Data Types

20 20 Informal introduction to concept of classes/objects §The relation basic type - variable l int age; l float score; §The relation class – object, class - instance l Person mike, marry; l SmallObj s; l SparePart pieces[10];

21 21 Informal introduction to concept of classes/objects §Access to data fields and methods is regulated through access modifiers: l Private l Protected l Public §Usually data fields are private §Usually methods are public

22 22 Informal introduction to concept of classes/objects §Specific methods: §Constructors §Destructor

23 23 Informal introduction to concept of classes/objects §Specific methods: §Accessor / Getter methods §Mutator / Setter methods

24 24 Informal introduction to concept of classes/objects §Properties: §Getter and Setter methods interpreted as data fields

25 25 Classes in VBasic §D§D

26 26 The class is essentially an extension of the structure. The class is = structure + Sub/Function procedures. Class = S tructure + procedure(s) Sub/Function procedures are member procedures. Sub/Function procedures are also called methods.

27 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 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 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 30 Dim a As Counter a = New Counter a.GetData() a.IncCount() : a.IncCount() : a.IncCount() a.ShowData()

31 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 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 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 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 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 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 Student Pupil = New Student()‘create instance of pupil ‘OR Dim Pupil As New Student()

37 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 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 Property block Example: External identifier of property – may be accessed

39 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 40 Example class Counter with property block Dim d As New Counter d.Count = 880 Console.WriteLine _ ("Counter data displayed through property = {0}", d.Count) Dim var As Integer var = d.Count Console.WriteLine _ ("Counter data displayed through assignment = {0}", var)

41 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 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 43 ‘ Constructors with indirect access to data member ‘ m_count using 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 Public Sub New() Count = 0 End Sub Public Sub New(ByVal val As Integer) Count = val End Sub End Class

44 44 Dim b, c As Counter b = New Counter b.ShowData() c = New Counter(20) c.ShowData()

45 45 Classes in VB §Open 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 46 Classes in C++ §D§D

47 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 48 Classes in C++ class Book { private: string m_Title; public: Book() { m_Title = " ";} Book(string pb) { m_Title = pb; } // method accessor string getTitle() { return m_Title; } // methods mutators void setTitle(string pb) { m_Title = pb; } void printBook() { cout << endl; cout << m_Title; cout << endl; } };

49 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 50 Classes in C# §D§D

51 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 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_Title public 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 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 54 Classes in Java §D§D

55 55 Classes in Java §Open 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 56 Classes in Java 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_Title // NO PROPERTIES in JAVA public void printBook() { System.out.println(); System.out.println(" "+ m_Title); System.out.println(); } } // end of class Book

57 57 Classes in Java public 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 58 Applying Classes to build primitive IS §Examples of elementary IS: l Books l Journals l Family members l Students in INF160 class §Approaches to develop simple IS: l Using many scalar variables l Using parallel arrays l Using Class template and array of Classes

59 59 Using many scalar variables §E§E

60 60 Using parallel arrays §E§E

61 61 Using Class template and array of Classes §E§E

62 62 §To introduce UML before BlueJ

63 63 Class SmallObj class SmallObj { private: int somedata; public: void SetData(int d) { somedata = d; } void ShowData() { cout << "\nData is =" << somedata; } };

64 64 SmallObj – UML class diagram SmallObj somedata -somedata SetData(int) +SetData(int) ShowData() +ShowData()

65 65 SmallObj – UML class diagram after G.Booch cloud form SmallObj SmallObj somedata -somedata SetData(int) +SetData(int) ShowData() +ShowData()

66 66 Thank You For Your Attention!


Download ppt "1 INF160 IS Development Environments AUBG, COS dept Lecture 13 Title: Classes: Introduction & Review (Extract from Syllabus)"

Similar presentations


Ads by Google