Intro Week 9

esse quam videri
Revision as of 03:20, 9 February 2016 by Jeff (talk | contribs) (Text replacement - "syntaxhighlight lang="csharp" line="1" " to "syntaxhighlight lang="csharp"")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

OOP Classes and Objects

Classes are sections of code that describe real world objects. Classes define what properties and methods an object contains. Properties

define data members of classes. Nouns

Abstraction

Humans are always abstracting and naming objects. It is how we identify things. Objects are identified by two things: properties(what the have) and methods (what they do).

Classes are used to define objects.

The hardest part about object oriented programming is choosing how to build and implement these abstractions.

Methods

define what classes can do. Verbs

Dog Class UML

Objects are instances of classes. For instance a Dog class may describe what dogs are like.

All dogs have specific properties like height, weight, color. Dog also have methods like bark. My dog rover is a specific instance of the dog class.

Dog Class

Fields

Microsoft has started call ing public variables fields

  • string Name
  • int Age
  • int Weight
  • string BarkSound

Methods

  • Bark
  • Eat

Dog Class UML

DogSimpleUML.png

//Dog simple class definition
public class Dog
{
 	public string Name;		// the dog's name
	public int Age;			// the dog's age
	public int Weight;	// the dog's weight
	public string BarkSound;	// the sound of the dog's bark
	

	public string Bark() { 
	   return BarkSound;
	}
	public void Eat() {
		//put eat code here 
	}
}

Now lets make some instances of the dog class

// Instantiate Dog objects
// Declare an instance of the Dog class
Dog firstDog;

// Allocate memory for the dog object
firstDog = new Dog();

// Declare and Allocate in one like
Dog secondDog = new Dog();

The new keyword call the classes constructor which allocated memory on the stack for the object. </syntaxhighlight>

In Class

  Make and compile a Cat class with a test class.
  *Draw UML for the Cat Class
  *Write test class
  *Compile and test test class
  *Make Dog class
  *Test Dog class with test class