Difference between revisions of "OOP Class6"

esse quam videri
Jump to: navigation, search
m (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >")
m (Text replacement - "</csharp>" to "</syntaxhighlight>")
Line 38: Line 38:
 
//Add a dog to the total dog count
 
//Add a dog to the total dog count
 
dogCount++;
 
dogCount++;
}</csharp>
+
}</syntaxhighlight>
 
}
 
}
  
Line 76: Line 76:
 
}
 
}
 
}
 
}
</csharp>
+
</syntaxhighlight>
  
 
overloaded method that makes a dog bark more than once.
 
overloaded method that makes a dog bark more than once.
Line 108: Line 108:
 
}
 
}
 
}
 
}
</csharp>
+
</syntaxhighlight>
  
 
You can overload a method as much as you want as long as each overload has a '''unique argument signature'''
 
You can overload a method as much as you want as long as each overload has a '''unique argument signature'''
Line 131: Line 131:
 
   
 
   
 
}
 
}
</csharp>
+
</syntaxhighlight>
  
 
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
Line 147: Line 147:
 
   
 
   
 
}
 
}
</csharp>
+
</syntaxhighlight>
  
 
or the name of the dog and the barksound
 
or the name of the dog and the barksound
Line 167: Line 167:
 
   
 
   
 
}
 
}
</csharp>
+
</syntaxhighlight>
  
 
you can have as many overoads as you want as long as each overload has a unique '''argument signature'''
 
you can have as many overoads as you want as long as each overload has a unique '''argument signature'''
Line 192: Line 192:
 
//code to implement class addition
 
//code to implement class addition
 
return c;
 
return c;
}</csharp>
+
}</syntaxhighlight>
  
 
dog addition? extra credit
 
dog addition? extra credit
Line 209: Line 209:
 
     //code to implement class addition
 
     //code to implement class addition
 
     return c;
 
     return c;
}</csharp>
+
}</syntaxhighlight>
  
 
Operator also refer to implicit and explicit conversions. Fractions make a good example for opertor overloading.
 
Operator also refer to implicit and explicit conversions. Fractions make a good example for opertor overloading.
Line 282: Line 282:
 
     }
 
     }
 
   }
 
   }
}</csharp>
+
}</syntaxhighlight>
  
 
Dog Association example
 
Dog Association example
Line 292: Line 292:
  
 
<syntaxhighlight lang="csharp" line="1" >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");</syntaxhighlight>
  
 
<syntaxhighlight lang="csharp" line="1" >//Dog simple class definition
 
<syntaxhighlight lang="csharp" line="1" >//Dog simple class definition
Line 325: Line 325:
 
       Name = newName;
 
       Name = newName;
 
     }
 
     }
   }</csharp>
+
   }</syntaxhighlight>
  
  
Line 355: Line 355:
 
           color = value;
 
           color = value;
 
         }
 
         }
     }</csharp>
+
     }</syntaxhighlight>
  
 
console /infod/jeff/classSource/class4/dogAccessor.cs - source
 
console /infod/jeff/classSource/class4/dogAccessor.cs - source
Line 463: Line 463:
 
//put eat code here  
 
//put eat code here  
 
}
 
}
}</csharp>
+
}</syntaxhighlight>
  
 
console
 
console
Line 518: Line 518:
 
     }
 
     }
 
   }
 
   }
}</csharp>
+
}</syntaxhighlight>
  
 
Dog Association example
 
Dog Association example
Line 528: Line 528:
  
 
<syntaxhighlight lang="csharp" line="1" >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");</syntaxhighlight>
  
 
<syntaxhighlight lang="csharp" line="1" >//Dog simple class definition
 
<syntaxhighlight lang="csharp" line="1" >//Dog simple class definition
Line 561: Line 561:
 
       Name = newName;
 
       Name = newName;
 
     }
 
     }
   }</csharp>
+
   }</syntaxhighlight>
  
  

Revision as of 18:29, 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

 1 public class Dog
 2 {
 3 	//some code
 4 
 5 	static private int dogCount;	// total number of dogs
 6 
 7 	public Dog()
 8 	{
 9 		barkSound = "Woof!!!";
10  		//Add a dog to the total dog count
11 		dogCount++;
12 	}
13 	public Dog(string newName)
14 	{
15 		name = newName;
16 		barkSound = "Woof!!!";
17 		//Add a dog to the total dog count
18 		dogCount++;
19 	}

}

Static dougCount Example
dogStatic.cs - source



OverLoading

You can overload a method to make it more flexible.

Simple Bark Method

 1 public class Dog
 2 {
 3  	public string Name;		// the dog's name
 4 	
 5 	public string BarkSound;	// the sound of the dog's bark
 6 	
 7 	public Dog()
 8 	{
 9 		BarkSound = "Woof!!!";
10 	}
11  
12 	public string Bark() { 
13 		string strBark = this.BarkSound;
14                 barkCount ++;
15                 return strBark;
16 	}
17     
18 	public void Eat() {
19 		//put eat code here 
20 	}
21 }

overloaded method that makes a dog bark more than once.

 1 public class Dog
 2 {
 3  	public string Name;		// the dog's name
 4 	
 5 	public string BarkSound;	// the sound of the dog's bark
 6 	
 7 	public Dog()
 8 	{
 9 		BarkSound = "Woof!!!";
10 	}
11  
12 	public string Bark() { 
13 		string strBark = this.BarkSound;
14                 barkCount ++;
15                 return strBark;
16 	}
17     
18 	public string Bark(int numBarks) { 
19 		string strBark = "";
20                 for(int i =0; i < numBarks; i++)  //bark many times
21                 {
22                    strBark += this.BarkSound;
23                    barkCount ++;
24                 }
25                 return strBark;
26 	}
27 }

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

 1 public class Dog
 2 {
 3  	
 4 	public string BarkSound;	// the sound of the dog's bark
 5 	
 6 	public Dog()
 7 	{
 8 		BarkSound = "Woof!!!";
 9 	}
10  
11 }

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

 1 public class Dog
 2 {
 3  	
 4 	public string BarkSound;	// the sound of the dog's bark
 5 	
 6 	public Dog(string newBarkSound)
 7 	{
 8 		this.BarkSound = newBarkSound;
 9 	}
10  
11 }

or the name of the dog and the barksound


 1 public class Dog
 2 {
 3  	
 4 	public string BarkSound;	// the sound of the dog's bark
 5         public string Name;
 6 	
 7 	public Dog(string newBarkSound, string NewName)
 8 	{
 9 		this.BarkSound = newBarkSound;
10                 this.Name = newName;
11 
12 	}
13  
14 }

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.

 1 //Dog simple class definition
 2 public class Dog
 3 {
 4     //some dog code...
 5     
 6     public void Relieve(Hydrant h)
 7     {
 8      h.Clean = false;
 9     }
10 }    
11 
12 public class Hydrant
13 {
14   public string Color; //the color of the hydrant
15   public bool Clean;   //if the hydrant is clean or dirty
16   
17   public Hydrant()
18   {
19     Color = "red";
20     Clean = true;
21   }
22   
23   public override string ToString ()
24   {
25     if (this.Clean == true)
26     {
27      return ("The " + this.Color + " hydrant is clean.");
28     }
29     else
30     {
31      return ("The " + this.Color + " hydrant is not clean.");
32     }
33   }
34 }

Dog Association example DogHydrant.cs - source

Containment - 'Has A'

A dog has an owner


1 Dog fido = new Dog("fido");
2 fido.Owner = new Person("Sue");
 1 //Dog simple class definition
 2 public class Dog
 3 {
 4     public string Name;        // the dog's name
 5     public string BarkSound;    // the sound of the dog's bark
 6     public Person Owner;    // the dogs owner
 7     
 8     public Dog(string dogName)
 9     {
10         Name = dogName;
11         BarkSound = "Woof!!!";
12     }
13 
14     public string Bark() { 
15         string s;
16         s = this.Name + " says " + this.BarkSound;
17         //Make sure the the dog has an owner
18         if (!(this.Owner == null))
19            s += "\n" + Owner.Name + " says be quiet.";
20         return s;
21     }
22 }    
23 
24 public class Person
25   {
26     public string Name;      //the color of the hydrant
27     
28     public Person(string newName)
29     {
30       Name = newName;
31     }
32   }


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