1 / 23

Chapter 4

Chapter 4. Object Oriented Concepts. Objects. Object is an intuitive concept We think of things when thinking of ‘object’ In OOP there is more to object We will learn that objects in OOP have both attributes and behavior

job
Download Presentation

Chapter 4

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. Chapter 4 Object Oriented Concepts

  2. Objects • Object is an intuitive concept • We think of things when thinking of ‘object’ • In OOP there is more to object • We will learn that objects in OOP have both attributes and behavior • Examples: a box has a shape, size, dimensions (attributes). It can be moved, opened, closed etc (behavior)

  3. OO Concepts • Objects hold state and behavior • State is the value of data attributes • Behavior is set of operations to do • Data and operations are packaged together • Thus in one reference both are covered

  4. Abstraction and Encapsulation • Abstraction expresses important features of entity • Encapsulation is what the user sees, but the details are hidden - groups things together • Encapsulation implements an abstraction • Access to the data is through special functions only • Thus data is protected from corruption • And users are protected from system changes

  5. Classes • A class groups together objects with common behavior, structure • A new instance of an object can be created – called instantiation

  6. Inheritance • Classes pass on their characteristics to derived (subordinate) classes • Example: • Base class Account • Derived classes are Savings and Checking • Savings has balance and interest • Checking has balance and fee • They are special types of Account

  7. Methods • These are the actions the OBJECTS perform • Methods could be called programs too • Methods are “called” by other programs or methods • Sometimes they have “arguments” like: • public void getname (stringinvar) • Here string invar is an argument of getname • Ex: getname(“John Smith”)

  8. Polymorphism • A method has multiple behaviors • The behavior depends on the object calling the method • Example: • A method in each (savings and checking) gets user input. • That input is private to the method, so it is secure

  9. Example from the book pg 108 using System; namespace CritterMenu { class Menu { static void Main(string[] args) { bool keepGoing = true; int choice;

  10. continued while (keepGoing){ choice = showMenu(); // call method showmenu switch (choice){ // showMenu returns a value contained in choice case 0: keepGoing = false; break; case 1: Console.WriteLine("you talk to the critter"); break; case 2: Console.WriteLine ("You have fed the critter"); break; case 3: Console.WriteLine("You have played with the critter"); break; case 4: Console.WriteLine("You have renamed the critter"); break; default: Console.WriteLine("That was not a valid input"); break; } // end switch } // end while loop } // end main

  11. static int showMenu() { int input = 1; Console.WriteLine(); Console.WriteLine("0) Exit"); Console.WriteLine("1) Listen to Critter"); Console.WriteLine("2) Feed Critter"); Console.WriteLine("3) Play with Critter"); Console.WriteLine("4) Rename Critter"); try { input = Convert.ToInt32(Console.ReadLine()); } catch (FormatException) { Console.WriteLine("Incorrect input"); input = 1; } // end try return input; } // end showMenu

  12. Method example using System; class sumfunctions { public static decimal getsum(decimal num1, decimal num2) { decimal total1; total1=num1+num2; return total1; }// end method static void Main () {

  13. Main static void Main () { decimal salary, bonus, net; salary=5000.00; bonus=1000.00; net=getsum(salary,bonus); Console.Write(“Net Sal = ”+net); }// end Main The values for salary and bonus are sent to getsum. The result is returned to Main.

  14. Classes and Methods (more..)

  15. Theory of Classes • You have used them before • When code is written, a class is not created, it is only declared • As in arrays to create the class: • You use new as class Building { public int floors; // number of floors public int area; // total square footage of bldg public int occupants; // number of occupants } Building house = new Building(); • house is a new instance of Building

  16. Every instance of Building creates a new copy of the variables • To access the variables, use the . • Ex: house.floors = 3; • The dot operator is used for both variables and methods • The rest of the topic:

  17. class Building { public int floors; // number of floors public int area; // total square footage of bldg public int occupants; // number of occupants public void areaPerPerson() { double area1 = area / occupants; Console.WriteLine(" "+ area1 +" area per person"); } public void getvalues(string txt, string txt2, string txt3, out int var1,out int var2,out int var3) { string entt; Console.Write(txt); entt=Console.ReadLine(); var1=Convert.ToInt32(entt); Console.Write(txt2); entt=Console.ReadLine(); var2=Convert.ToInt32(entt); Console.Write(txt3); entt=Console.ReadLine(); var3=Convert.ToInt32(entt); }

  18. class Buildingrunner { public static void Main() { Building house = new Building(); Building office = new Building(); // assign values to fields in house house.occupants = 4; house.area = 2500; house.floors = 2; Console.WriteLine("house has:\n " + house.floors + "floors\n "+ house.occupants + "occupants\n "+ house.area + " total area"); house.areaPerPerson(); Console.WriteLine();

  19. //assign values to fields in office office.getvalues ("Enter # of occpnts", "Enter area", "Enter # floors", out office.occupants, out office.area, out office.floors); Console.WriteLine("office has:\n " + office.floors + "floors\n "+ office.occupants + "occupants\n "+ office.area + " total area"); office.areaPerPerson();

  20. Calls

  21. Call by Value • You have called objects by passing parameters from Main. • EX: sums (salary, tax) • The VALUE of salary and tax are sent to Main • The object perhaps returns a value to Main

  22. Calls by Reference • This makes it possible to return more than one value to Main • A keyword ref is used to signal call by reference • The memory address of the variable is passed • Let’s examine an example

  23. Call by Reference public static void get_tax(double v1, ref double out1, ref double out2) { if ( v1>15000.00) out1 = v1 * .05; else out1 = v1 * .01; out2 = v1 – out1; } In Main… Net =get_tax(salary, ref tax, ref net);

More Related