1 / 66

INF160 IS Development Environments AUBG, COS dept

INF160 IS Development Environments AUBG, COS dept. Lecture 13 Title: Classes: Introduction & Review (Extract from Syllabus). Lecture Contents:. OOP: Classes, Objects, Data members, Methods Classes in VBasic Classes in C++ Classes in C# Classes in Java

ogden
Download Presentation

INF160 IS Development Environments AUBG, COS dept

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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. Demo Programs: Group name: Test160dClass • Test160dClassVB (Module1.vb) • Test160dClassCPP • Test160dClassC#(Program.cs) • Test160dClassJava

  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. Formal introduction to concept of classes/objects • OOP (Object Oriented Programming): • Data Encapsulation & Data Hiding • Inheritance • Polymorphism

  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. • Inheritance • Polymorphism

  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 Hiding • Inheritance • Polymorphism • 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 data • Data collections/Data containers • Evolution of the data concept

  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. 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. • 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 Interface Methods

  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 membervariable. • 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 Student Pupil = 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 External identifier of property – may be accessed Example: 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

  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 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. 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_countusing 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. Dim b, c As Counter b = New Counter b.ShowData() c = New Counter(20) c.ShowData()

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

More Related