Difference between revisions of "OOP Class6"

esse quam videri
Jump to: navigation, search
(READ)
m (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >")
Line 20: Line 20:
 
Static - A type of member modifier that indicates that the member applies to the type rather than an instance of the type
 
Static - A type of member modifier that indicates that the member applies to the type rather than an instance of the type
  
<csharp>public class Dog
+
<syntaxhighlight lang="csharp" line="1" >public class Dog
 
{
 
{
 
//some code
 
//some code
Line 54: Line 54:
 
Simple Bark Method
 
Simple Bark Method
  
<csharp>
+
<syntaxhighlight lang="csharp" line="1" >
 
public class Dog
 
public class Dog
 
{
 
{
Line 80: Line 80:
 
overloaded method that makes a dog bark more than once.
 
overloaded method that makes a dog bark more than once.
  
<csharp>
+
<syntaxhighlight lang="csharp" line="1" >
 
public class Dog
 
public class Dog
 
{
 
{
Line 119: Line 119:
 
here is a simple dog contructor
 
here is a simple dog contructor
  
<csharp>
+
<syntaxhighlight lang="csharp" line="1" >
 
public class Dog
 
public class Dog
 
{
 
{
Line 135: Line 135:
 
we can modify this so that we can pass in the sound of the dogs bark when the object is created
 
we can modify this so that we can pass in the sound of the dogs bark when the object is created
  
<csharp>
+
<syntaxhighlight lang="csharp" line="1" >
 
public class Dog
 
public class Dog
 
{
 
{
Line 152: Line 152:
  
  
<csharp>
+
<syntaxhighlight lang="csharp" line="1" >
 
public class Dog
 
public class Dog
 
{
 
{
Line 180: Line 180:
 
     * Operator Overloading in C# - HowToDoThings.com
 
     * Operator Overloading in C# - HowToDoThings.com
  
<csharp>//Overloading unary operators
+
<syntaxhighlight lang="csharp" line="1" >//Overloading unary operators
 
public static return_type operator op (Type t)
 
public static return_type operator op (Type t)
 
{
 
{
Line 204: Line 204:
 
===Operator Overloading Fraction Class===
 
===Operator Overloading Fraction Class===
  
<csharp>public static ClassType operator + ( object lhs, object rhs )
+
<syntaxhighlight lang="csharp" line="1" >public static ClassType operator + ( object lhs, object rhs )
 
{
 
{
 
     ClassType c
 
     ClassType c
Line 249: Line 249:
 
A dog uses a hydrant to relieve them self.
 
A dog uses a hydrant to relieve them self.
  
<csharp>//Dog simple class definition
+
<syntaxhighlight lang="csharp" line="1" >//Dog simple class definition
 
public class Dog
 
public class Dog
 
{
 
{
Line 291: Line 291:
  
  
<csharp>Dog fido = new Dog("fido");
+
<syntaxhighlight lang="csharp" line="1" >Dog fido = new Dog("fido");
 
fido.Owner = new Person("Sue");</csharp>
 
fido.Owner = new Person("Sue");</csharp>
  
<csharp>//Dog simple class definition
+
<syntaxhighlight lang="csharp" line="1" >//Dog simple class definition
 
public class Dog
 
public class Dog
 
{
 
{
Line 336: Line 336:
 
  and the this keyword
 
  and the this keyword
  
<csharp>//Accessor for private color allow color to be set and return color or 'dirty' + color
+
<syntaxhighlight lang="csharp" line="1" >//Accessor for private color allow color to be set and return color or 'dirty' + color
 
     public string Color
 
     public string Color
 
     {
 
     {
Line 362: Line 362:
 
Dog private members, Overloaded with method class definition
 
Dog private members, Overloaded with method class definition
  
<csharp>//Dog private members, Overloaded  with method class definition
+
<syntaxhighlight lang="csharp" line="1" >//Dog private members, Overloaded  with method class definition
 
public class Dog
 
public class Dog
 
{
 
{
Line 485: Line 485:
 
A dog uses a hydrant to relieve them self.
 
A dog uses a hydrant to relieve them self.
  
<csharp>//Dog simple class definition
+
<syntaxhighlight lang="csharp" line="1" >//Dog simple class definition
 
public class Dog
 
public class Dog
 
{
 
{
Line 527: Line 527:
  
  
<csharp>Dog fido = new Dog("fido");
+
<syntaxhighlight lang="csharp" line="1" >Dog fido = new Dog("fido");
 
fido.Owner = new Person("Sue");</csharp>
 
fido.Owner = new Person("Sue");</csharp>
  
<csharp>//Dog simple class definition
+
<syntaxhighlight lang="csharp" line="1" >//Dog simple class definition
 
public class Dog
 
public class Dog
 
{
 
{

Revision as of 18:26, 25 January 2016


Review

Accessibility
restrict the visibility of a class and it's members
Classes
Abstraction that has properties and methods. properties and the nouns and methods are the verbsClasses are used to define objects.
Objects
Instance of a class
Abstraction 
Factor out details to work on fewer concepts at a time
Encapsulation
Allows us to use objects with out completely understanding how everything inside the object works. Encapsulation also allows the internal state of objects to be changed easily without affecting other objects or interfaces. Real barking dogs...DogBark an example of how you don't need to know how all of the classes work just how to use them
Polymorphism
many forms that have the same attributes and abilities
UML 
Unified Modeling Language.

TODO

Virtual Functions
Functions that can be overridden.
Static
Members that are associayed with a class not an instance of the class

Shared Members - static members

aka Shared Properties

Static - A type of member modifier that indicates that the member applies to the type rather than an instance of the type

<syntaxhighlight lang="csharp" line="1" >public class Dog { //some code

static private int dogCount; // total number of dogs

public Dog() { barkSound = "Woof!!!";

		//Add a dog to the total dog count

dogCount++; } public Dog(string newName) { name = newName; barkSound = "Woof!!!"; //Add a dog to the total dog count dogCount++; }</csharp> }

Static dougCount Example
dogStatic.cs - source



OverLoading

You can overload a method to make it more flexible.

Simple Bark Method

<syntaxhighlight lang="csharp" line="1" > public class Dog {

	public string Name;		// the dog's name

public string BarkSound; // the sound of the dog's bark

public Dog() { BarkSound = "Woof!!!"; }

public string Bark() { string strBark = this.BarkSound;

               barkCount ++;
               return strBark;

}

public void Eat() { //put eat code here } } </csharp>

overloaded method that makes a dog bark more than once.

<syntaxhighlight lang="csharp" line="1" > public class Dog {

	public string Name;		// the dog's name

public string BarkSound; // the sound of the dog's bark

public Dog() { BarkSound = "Woof!!!"; }

public string Bark() { string strBark = this.BarkSound;

               barkCount ++;
               return strBark;

}

public string Bark(int numBarks) { string strBark = "";

               for(int i =0; i < numBarks; i++)  //bark many times
               {
                  strBark += this.BarkSound;
                  barkCount ++;
               }
               return strBark;

} } </csharp>

You can overload a method as much as you want as long as each overload has a unique argument signature


Overload Constructor

Overloading the constructor of you class can make it easier to use.

here is a simple dog contructor

<syntaxhighlight lang="csharp" line="1" > public class Dog {

public string BarkSound; // the sound of the dog's bark

public Dog() { BarkSound = "Woof!!!"; }

} </csharp>

we can modify this so that we can pass in the sound of the dogs bark when the object is created

<syntaxhighlight lang="csharp" line="1" > public class Dog {

public string BarkSound; // the sound of the dog's bark

public Dog(string newBarkSound) { this.BarkSound = newBarkSound; }

} </csharp>

or the name of the dog and the barksound


<syntaxhighlight lang="csharp" line="1" > public class Dog {

public string BarkSound; // the sound of the dog's bark

       public string Name;

public Dog(string newBarkSound, string NewName) { this.BarkSound = newBarkSound;

               this.Name = newName;

}

} </csharp>

you can have as many overoads as you want as long as each overload has a unique argument signature




Association - 'Uses A'

A dog uses a hydrant to relieve them self.

<syntaxhighlight lang="csharp" line="1" >//Dog simple class definition public class Dog {

   //some dog code...
   
   public void Relieve(Hydrant h)
   {
    h.Clean = false;
   }

}

public class Hydrant {

 public string Color; //the color of the hydrant
 public bool Clean;   //if the hydrant is clean or dirty
 
 public Hydrant()
 {
   Color = "red";
   Clean = true;
 }
 
 public override string ToString ()
 {
   if (this.Clean == true)
   {
    return ("The " + this.Color + " hydrant is clean.");
   }
   else
   {
    return ("The " + this.Color + " hydrant is not clean.");
   }
 }

}</csharp>

Dog Association example DogHydrant.cs - source

Containment - 'Has A'

A dog has an owner


<syntaxhighlight lang="csharp" line="1" >Dog fido = new Dog("fido"); fido.Owner = new Person("Sue");</csharp>

<syntaxhighlight lang="csharp" line="1" >//Dog simple class definition public class Dog {

   public string Name;        // the dog's name
   public string BarkSound;    // the sound of the dog's bark
   public Person Owner;    // the dogs owner
   
   public Dog(string dogName)
   {
       Name = dogName;
       BarkSound = "Woof!!!";
   }
   public string Bark() { 
       string s;
       s = this.Name + " says " + this.BarkSound;
       //Make sure the the dog has an owner
       if (!(this.Owner == null))
          s += "\n" + Owner.Name + " says be quiet.";
       return s;
   }

}

public class Person

 {
   public string Name;      //the color of the hydrant
   
   public Person(string newName)
   {
     Name = newName;
   }
 }</csharp>


Containment example
DogOwner.cs -source

Homework

  • Create A Cat Containment and Association Project based off of the followong UML

CatConainmentAssociation.PNG

The Program should have output similar to

Hello I am a CatContainmentAssociation.Cat I'm 10 years old and I weigh 0 My Meo
w sounds like Meow!. punkin doesn't have a toy to play with
Hello I am a CatContainmentAssociation.Cat I'm 10 years old and I weigh 0 My Meo
w sounds like Meow!. punkin plays with SparkleyBall
The Basement LitterBox is clean
The Basement LitterBox is dirty
The Basement LitterBox is clean


  • Create a fourth class the uses or is used by one of your other classes
    • Association
  • Create a fifth class the contains or is contained by one of your other classes
    • Containment
    • Create a UML diagram that show the relationships of your classes post export the diagram as a png (I'll show this in class) post diagram to demo fourth and fifth class
  • Broken toaster