1 / 47

Introduction to Classes and Objects

Introduction to Classes and Objects. Ch 4. Object-Oriented Programming. Object Abstraction of a read-world item (e.g., car) Has attributes (e.g., size, shape, color) and behaviors (e.g., accelerates, brakes, turns) Class Blueprint for instantiating (creating) objects

mwendy
Download Presentation

Introduction to Classes and Objects

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. Introduction to Classes and Objects Ch 4

  2. Object-Oriented Programming • Object • Abstraction of a read-world item (e.g., car) • Has attributes (e.g., size, shape, color) and behaviors (e.g., accelerates, brakes, turns) • Class • Blueprint for instantiating (creating) objects • Many objects may belong to the same class • User-defined, non-built-in type

  3. The OOP Trilogy • Three important OO principles • Encapsulation • Inheritance • Polymorphism

  4. Encapsulation • Attributes & behaviors encapsulated (wrapped) into objects • Information hiding • Implementation details hidden within objects • You can drive a car without knowing details of how engine, transmission really work • Modularization!

  5. Method • Implements a behavior of object • E.g., car accelerates, brakes, turns • Describes the mechanisms that actually perform its tasks • Hides from its user the complex tasks that it performs • Method call by its user tells method to perform its task

  6. Instance Variable & Property • Together, represent an attribute of object • E.g., size, shape, color of a car • Instance variable • Actually stores an attribute • May not be directly accessible (information hiding) • Property • Provides indirect, controlled (get and set) access to an attribute

  7. Account Class

  8. Class Declaration • Instance variable • Each object has its own copy • Stores a value • E.g., name • Instance method • Can be called on an object to perform a task • voidindicates method does not return a value • E.g., SetName, GetName

  9. Access modifier • Private • Considered hidden in the class (information hiding) • Accessible in the class only • Cannot be accessed outside the class • Public • Can be accessed outside the class by a user • Available to the public

  10. UML Class Diagram • Three compartments of each class • Name, Attributes (Properties), Behaviors (Methods) • Plus sign indicates public

  11. Test the Account Class

  12. Result

  13. Instantiating an Object Account myAccount = new Account(); • Instantiating an object referred to as myAccountof class Account • myAccountholds a reference (memory address) of the object

  14. Calling Method on an Object myAccount.SetName(theName); • Calling the SetName method of object myAccount • Telling object to set the name variable

  15. In the Memory name object (anonymous) Jane Green myAccount reference

  16. Class with a Property

  17. Property • Provides indirect, controlled access to an attribute, e.g., Name for name • Can be used just like a variable • get accessor method • return an attribute value • set accessor method • Set (update) an attribute with a new value • value is implicitly declared input parameter

  18. UML Class Diagram

  19. Test the Class with Property

  20. Result

  21. Auto-Implemented Properties • Simpler syntax for defining a private instance variable and a public property accessing the instance variable • Cannot be used if the get or set accessor is not trivial

  22. Constructor • Initializes an object of a class • Automatically called when object is instantiated (keyword new) • Has the same name as the class • Has no return type and is not void

  23. Auto-implemented Property and Constructor

  24. UML Class Diagram

  25. Test the Class with Constructor and Auto-implemented Property

  26. Account Class with Variable, Property, Method

  27. UML Class Diagram

  28. Test the Account Class

  29. Result

  30. Access Modifiers • private variable, property, method • Only accessible in the class • Considered hidden (information hiding) • public variable, property, method • Accessible both inside and outside the class • The interface presented to the outside world

  31. Encapsulation • Information hiding • Implementation details hidden within objects • Instance variables made private • Indirect access to variables through public properties

  32. Advantages of Information Hiding • Create a more robust class • Less likely to malfunction, easier to maintain • Prevent data contamination • Data validation in set accessor of property • E.g., account balance cannot be negative • Freedom of changing internal design • As long as public properties are preserved • E.g., storing account balance in an integer (in number of cents)

  33. Illustrating Information Hiding(Example by Instructor) classAccount { // instance variable changed from decimal to int privateintbalanceInCents; // Balance property is still decimal publicdecimal Balance { get { return (decimal)balanceInCents/100; } privateset { if (value > 0.0m) { balanceInCents = (int)(value * 100); } } }

  34. Value Type vs. Reference Type • Value Type • Simple type • Variable contains a value of that type • bool,char, int, double, decimal, etc • Reference Type • Non-simple type • Class for instantiating objects • Variable contains reference (address) of an object, which must be instantiated with new • Otherwise, default value is null

  35. Value/Reference Type Examples

  36. GradeBook Class publicclassGradeBook { // property to get and set the course name publicstringCourseName {get; set;} // display a welcome message to the GradeBook user publicvoidDisplayMessage() { // use property CourseName to get the // name of the course that this GradeBook represents Console.WriteLine( "Welcome to the grade book for\n{0}!", CourseName ); // display property CourseName } // end method DisplayMessage } // end class GradeBook

  37. Test Value/Reference Types(Example by Instructor) int courseNumber1, courseNumber2, courseNumber3; GradeBook book1, book2, book3; book1 = newGradeBook(); book2 = book1; book3 = newGradeBook(); courseNumber1 = 101; courseNumber2 = 102; courseNumber3 = 103; book1.CourseName = "CS101 Instruction to C#"; book2.CourseName = "CS102 Data Structures in C#"; book3.CourseName = "CS103 Algorithms in C#"; Console.WriteLine($"Course number 1: {courseNumber1}"); Console.WriteLine($"Course number 2: {courseNumber2}"); Console.WriteLine($"Course number 3: {courseNumber3}"); book1.DisplayMessage(); book2.DisplayMessage(); book3.DisplayMessage();

  38. Result Course number 1: 101 Course number 2: 102 Course number 3: 103 Welcome to the grade book for CS102 Data Structures in C#! Welcome to the grade book for CS102 Data Structures in C#! Welcome to the grade book for CS103 Algorithms in C#!

  39. In the Memory (1) int courseNumber1, courseNumber2, courseNumber3; GradeBook book1, book2, book3; null book3 null 3 references book2 null book1 0 courseNumber3 3 values 0 courseNumber2 0 courseNumber1

  40. In the Memory (2) book1 = new GradeBook(); book2 = book1; book3 = new GradeBook(); CourseName (anonymous) null 2 objects CourseName (anonymous) null book3 3 references book2 book1 0 courseNumber3 3 values 0 courseNumber2 0 courseNumber1

  41. In the Memory (3) courseNumber1 = 101; courseNumber2 = 102; courseNumber3 = 103; book1.CourseName = "CS101 Instruction to C#"; book2.CourseName = "CS102 Data Structures in C#"; book3.CourseName = "CS103 Algorithms in C#"; CourseName (anonymous) CS102 2 objects CourseName (anonymous) CS103 book3 3 references book2 book1 103 courseNumber3 3 values 102 courseNumber2 101 courseNumber1

  42. Summary • Class is template for instantiating objects • Methods • Instance variables and properties • Access modifiers • private, public • Encapsulation • Information hiding • Value type vs. reference type

More Related