Chapter 4 Object Oriented Concepts Objects Object is
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 • Examples: a box has a shape, size, dimensions (attributes). It can be moved, opened, closed etc (behavior)
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
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
Classes • A class groups together objects with common behavior, structure • A new instance of an object can be created – called instantiation
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
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 (string invar) – Here string invar is an argument of getname – Ex: getname(“John Smith”)
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
Example from the book pg 108 using System; namespace Critter. Menu { class Menu { static void Main(string[] args) { bool keep. Going = true; int choice;
continued while (keep. Going){ choice = show. Menu(); // call method showmenu switch (choice){ // show. Menu returns a value contained in choice case 0: keep. Going = false; break; case 1: Console. Write. Line("you talk to the critter"); break; case 2: Console. Write. Line ("You have fed the critter"); break; case 3: Console. Write. Line("You have played with the critter"); break; case 4: Console. Write. Line("You have renamed the critter"); break; default: Console. Write. Line("That was not a valid input"); break; } // end switch } // end while loop } // end main
static int show. Menu() { int input = 1; Console. Write. Line(); Console. Write. Line("0) Exit"); Console. Write. Line("1) Listen to Critter"); Console. Write. Line("2) Feed Critter"); Console. Write. Line("3) Play with Critter"); Console. Write. Line("4) Rename Critter"); try { input = Convert. To. Int 32(Console. Read. Line()); } catch (Format. Exception) { Console. Write. Line("Incorrect input"); input = 1; } // end try return input; } // end show. Menu
Method example using System; class sumfunctions { public static decimal getsum(decimal num 1, decimal num 2) { decimal total 1; total 1=num 1+num 2; return total 1; }// end method static void Main () {
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.
Classes and Methods (more. . )
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
• 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:
class Building { public int floors; // number of floors public int area; // total square footage of bldg public int occupants; // number of occupants public void area. Person() { double area 1 = area / occupants; Console. Write. Line(" "+ area 1 +" area person"); } public void getvalues(string txt, string txt 2, string txt 3, out int var 1, out int var 2, out int var 3) { string entt; Console. Write(txt); entt=Console. Read. Line(); var 1=Convert. To. Int 32(entt); Console. Write(txt 2); entt=Console. Read. Line(); var 2=Convert. To. Int 32(entt); Console. Write(txt 3); entt=Console. Read. Line(); var 3=Convert. To. Int 32(entt); }
class Buildingrunner { public static void Main() { Building house = new Building(); Building office = new Building(); // assign values to fields in house. occupants = 4; house. area = 2500; house. floors = 2; Console. Write. Line("house has: n " + house. floors + "floorsn "+ house. occupants + "occupantsn "+ house. area + " total area"); house. area. Person(); Console. Write. Line();
//assign values to fields in office. getvalues ("Enter # of occpnts", "Enter area", "Enter # floors", out office. occupants, out office. area, out office. floors); Console. Write. Line("office has: n " + office. floors + "floorsn "+ office. occupants + "occupantsn "+ office. area + " total area"); office. area. Person();
Calls
Call by Value n You have called objects by passing parameters from Main. n EX: sums (salary, tax) The VALUE of salary and tax are sent to Main n The object perhaps returns a value to Main n
Calls by Reference This makes it possible to return more than one value to Main n A keyword ref is used to signal call by reference n The memory address of the variable is passed n Let’s examine an example n
Call by Reference public static void get_tax(double v 1, ref double out 2) { if ( v 1>15000. 00) out 1 = v 1 *. 05; else out 1 = v 1 *. 01; out 2 = v 1 – out 1; } In Main… Net =get_tax(salary, ref tax, ref net);
- Slides: 23