Difference between revisions of "OOP Class5"

esse quam videri
Jump to: navigation, search
Line 10: Line 10:
 
}</csharp>
 
}</csharp>
  
 +
==What is UML==
 +
;UML : Unified Modeling Language.
 +
:"A modeling language is a language whose vocabulary and rules focus on the conceptual and physical representation of a system"
 +
The Unified Modeling Language User Guide, Booch Rumbaugh Jacobson
 +
ACM Press, Addison Wesley 1999 ISBN 0201571684
 +
[http://www.amazon.com/gp/product/0201571684/104-7742183-3467114?v=glance&n=283155 The Unified Modeling Language User Guide]
 +
==Simple Notation==
 +
Class notation in UML
  
 +
The name of the Class should go inside of a box
  
==access-modifiers==
+
{| align="center" cellspacing="0" style="border:2px solid black;"
public +
+
|Class Name
:most permissive access. No restrictions on access
+
|}
private -
 
:least permissive access level. Only available to the class in which it was declared
 
protected #
 
:accessible within its class and by derived classes
 
internal
 
:accessible only within files in the same assembly
 
protected internal
 
:combination of protected and internal
 
Public Dog
 
[[Image:DogSimpleUML.png]]
 
  
Dog with private age
 
[[Image:DogsimpleAgePrivate.png]]
 
  
==Properties Private instance data members - accessors==
+
Then Two lines are added. The first line separates Attributes. The second line separates Operations.
Micorsoft has stared calling private varables with accessors Properties
+
Attributes are the equivalent of fields or properties in c# and Operations are the same as methods.
  
Private class memebers the use get and set keyword to set and retrieve data. Get and set are known as accessor methods private members are helpfull when you want to also do other things when a data member is changed or change the rutern value under certian conditions. C# Programmer's Reference - Accessors http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfaccessorspg.asp. Lastly accessor also make read only and write only variables possible
+
{|  align="center" border = 0
 +
|
  
<csharp>//private string color read/write
+
{|  cellspacing="0" style="border:2px solid black;"
private string color;
+
|Class Name
 +
|-
 +
| style="border-Top:2px solid black;" |&nbsp;
 +
|-
 +
| style="border-Top:2px solid black;" |&nbsp;
 +
|}
  
public string Color
+
|
{
+
 
  get
+
{| align="center" cellspacing="0" Border = 0
  {
+
| <--'''Class Name'''
  return color;
+
|-
  }
+
|  <--'''Attributes'''
  set
+
|-
  {
+
| <--'''Opertaions'''
  color = value;
+
|}
  }
+
 
}
+
|}
 +
 
 +
 
 +
A Rectagle Class might look like this
 +
 
 +
{|  align="center" cellspacing="0" style="border:2px solid black;"
 +
|Rectangle
 +
|-
 +
| style="border-Top:2px solid black;" | height
 +
|-
 +
| width
 +
|-
 +
| style="border-Top:2px solid black;" | area()
 +
|-
 +
| add()
 +
|-
 +
| move()
 +
|-
 +
| copy()
 +
|-
 +
| isEmpty()
 +
|}
  
//private string color read only
+
You can also specify Attribute types with a :
private string color;
 
  
public string Color
+
{|  align="center" cellspacing="0" style="border:2px solid black;"
{
+
|Rectangle
  get
+
|-
  {
+
| style="border-Top:2px solid black;" | height : Float
  return color;
+
|-
  }
+
| width : Float
}</csharp>
+
|-
 +
| style="border-Top:2px solid black;" | area()
 +
|-
 +
| add()
 +
|-
 +
| move()
 +
|-
 +
| copy()
 +
|-
 +
| isEmpty()
 +
|}
  
 +
Default vlaues are specified with =
  
Another property that is a good candidate for a private instance data member is the dogs age
+
{|  align="center" cellspacing="0" style="border:2px solid black;"
 +
|Rectangle
 +
|-
 +
| style="border-Top:2px solid black;" | height : Float = 6.0
 +
|-
 +
| width : Float = 4.0
 +
|-
 +
| style="border-Top:2px solid black;" | area()
 +
|-
 +
| add()
 +
|-
 +
| move()
 +
|-
 +
| copy()
 +
|-
 +
| isEmpty()
 +
|}
  
<csharp>
 
private string age;
 
  
public int Age
+
Operator return type are also specifed with a :. Opertor parameter signatures are specified by ( '''name''' : '''Type''' )
{
 
  //age can only be accessed with get there is no set accessor
 
  //age must be set with HappyBirthday()
 
  get
 
  {
 
  return age;
 
  }
 
}
 
  
public int HappyBirthday()
+
{|  align="center" cellspacing="0" style="border:2px solid black;"
{
+
|Rectangle
  age++;
+
|-
  return age;
+
| style="border-Top:2px solid black;" | height : Float = 6.0
}
+
|-
 +
| width : Float = 4.0
 +
|-
 +
| style="border-Top:2px solid black;" | area() : Float
 +
|-
 +
| add()(r:Rectangle)
 +
|-
 +
| move()
 +
|-
 +
| copy() :Rectangle
 +
|-
 +
| isEmpty() : Boolean = true
 +
|}
  
 +
==UML Resources==
 +
[http://www.gnome.org/projects/dia/ Dia A Drawing Program]
  
</csharp>
+
[http://dia-installer.sourceforge.net/ Dia for windows]
  
===In class===
+
[http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class6/DiagramDog Dog Diagrams with Dia]
  
==In Class Project==
+
==Dog Exmaples==
 +
Simple Dog Class from week 5 [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class6/DiagramDog/DogSimple.cs DogSimple.cs - source]<br />
 +
[[Image:DogSimple.png]]
 +
<csharp>using System;
  
*Build a Dog class with a private variable called age.
+
//Dog simple class definition
*Make a public Accessor for the private age the is read only
+
public class Dog
*Make a method that increments the age of your dog
+
{
*MAke a test class for the new property and accessor
+
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 Dog()
 +
{
 +
BarkSound = "Woof!!!";
 +
}
  
<!--In class build traingle class.
+
public void Bark() {
Properties
+
//put bark code here
 +
}
 +
public void Eat() {
 +
//put eat code here
 +
}
 +
}</csharp>
  
* sideA
 
* sideB
 
* sideC
 
  
Methods
+
Dog UML with Types
  
* Area //A method that reurn the area of the triangle
+
Dog Class with Types from week 4 [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class6/DiagramDog/DogWithTypes.cs DogWithTypes.cs - source]<br /><br />
 +
[[Image:DogWithTypes.png]]
 +
<csharp>using System;
 +
 +
//Dog simple class definition
 +
public class Dog
 +
{
 +
public string Name; // the dog's name
 +
private int Age; // the dog's age
 +
public int Weight; // the dog's weight
 +
public string BarkSound; // the sound of the dog's bark
 +
private int barkCount;    //The number of time the dog has barked
 +
public Dog()
 +
{
 +
BarkSound = "Woof!!!";
 +
}
 +
 +
public string Bark() {
 +
string strBark = this.BarkSound;
 +
        barkCount ++;
 +
        return strBark;
 +
}
 +
   
 +
public void Eat() {
 +
//put eat code here
 +
}
 +
}</csharp>
  
      use Heronian formula which is able to compute the area of a triange by knowing the length of the three sides.
 
      triangle area given a,b,c = sqrt(s(s-a)(s-b)(s-c)) when s = (a+b+c)/2 (Heron's formula)
 
  
<csharp>// Heronian formula
 
double s = (a + b + c) / 2.0;
 
double dArea = Math.Sqrt(s*(s-a)*(s-b)*(s-c));
 
</csharp>
 
  
Once you have built the triangle class build a class to test it.
 
The test class should create a triangle with
 
sideA= 3
 
sideB= 4
 
sideC= 5
 
  
and then display the area as text in the console.
 
  
http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class4/tri_class.cs tri_class.cs]
+
==Inheritance - 'Is A'==
-->
 
  
 +
[http://ia300033.us.archive.org/1/items/arsdigitac04n03/Lecture_03.html ArsDigita University Course 04: Object-oriented Program Design and Software Engineering - Lecture Notes 3]
  
 +
C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from.
  
Here's a class of dogs that return differnt color depending on whether on not they are clean. It uses private private instance data memebrs and the this keyword
+
A Basenji is an African barkless dog.
 +
A Basenji is a dog.
 +
syntax
  
<csharp>//Accessor for private color allow color to be set and return color or 'dirty' + color
+
<csharp>public class Basenji : Dog
     public string Color
+
{
 +
    //override constructor
 +
     public Basenji() : base()
 
     {
 
     {
         get
+
         barkSound = "Basenjis don't bark, but they do howl and growl.";
         {
+
         dogCount++;
            //if current dog isClean the return dogs color
+
    }
            if (this.isClean == true)
+
    public override void Bark()
            {
+
    {
                return color;
+
        Console.WriteLine("Basenjis dont bark.");
            }
+
    }
            //else return 'dirty' and the dogs color
+
            else {
+
// some more code....
                return "dirty " + color;
+
}
            }
+
 
         }
+
public class Dog
         set
+
{
        {
+
// some dog code...
          color = value;
+
//methods need to be marked virtual or protected to be overridden
        }
+
    public virtual void Bark()
    }</csharp>
+
    {  
 +
        //make dog bark
 +
        Console.WriteLine (this.Name + " says " + this.barkSound);
 +
         //add 1 to the number of times the dog has barked
 +
         this.barkCount++ ;
 +
    }
 +
}</csharp>
 +
 
 +
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/Basenji.cs Basenji.cs] -source
 +
 
 +
==Virtual Functions==
  
console /infod/jeff/classSource/class4/dogAccessor.cs - source
+
A class modifier that indicates that a method can be overridden by a derived class.
web /infod/jeff/classSource/class4/dogAccessor.aspx - source
+
We will look at virtual function when we look at abstract classes
  
Dog private members, Overloaded with method class definition
+
==Abstract Classes==
 +
abstract - A class modifier that specifies that the class must be derived from to be instantiated.
  
<csharp>//Dog private members, Overloaded  with method class definition
+
<csharp>abstract public class Mammal
public class Dog
 
 
{
 
{
private string name; // the dog's name
+
private string birth;
private int age; // the dog's age
+
private string skin;
private int weight; // the dog's weight
+
 
private string barkSound; // the sound of the dog's bark
+
public Mammal()
private int barkCount; // how many times the dog has barked
 
 
public Dog()
 
{
 
barkSound = "Woof!!!";
 
}
 
public Dog(string newName)
 
{
 
name = newName;
 
barkSound = "Woof!!!";
 
}
 
public Dog(string newName, string newBarkSound )
 
 
{
 
{
name = newName;
+
birth = " gives birth to live young.";
barkSound = newBarkSound;
+
skin = " has Hair.";
 
}
 
}
  
public string Name
+
public string Birth
 
{
 
{
 
get
 
get
 
{
 
{
return name;
+
return birth;
 
}
 
}
set
+
}
 +
public string Skin
 +
{
 +
get
 
{
 
{
name = value;
+
return skin;
 
}
 
}
 
}
 
}
public int Age
+
}</csharp>
 +
 
 +
Inheritance example /infod/jeff/classSource/class5/DogMammal.cs -source
 +
 
 +
===Polymorphic Types===
 +
[http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 Polymorphism (computer science)]
 +
Different types implementing the same functionality.<br>
 +
[http://www.webopedia.com/TERM/p/polymorphism.html  polymorphism webopedia]
 +
The C# Station Tutorial - [http://www.csharp-station.com/Tutorials/Lesson09.aspx Lesson 9: Polymorphism]
 +
 
 +
<csharp>abstract public class Mammal
 +
{
 +
private string name;
 +
private string birth;
 +
private string skin;
 +
 
 +
public Mammal()
 +
{
 +
name = "none";
 +
birth = " gives birth to live young.";
 +
skin = " has Hair.";
 +
}
 +
 +
public Mammal(string newName)
 +
{
 +
name = newName;
 +
birth = " gives birth to live young.";
 +
skin = " has Hair.";
 +
}
 +
 
 +
public string Birth
 
{
 
{
 
get
 
get
 
{
 
{
return age;
+
return birth;
}
 
set
 
{
 
age = value;
 
 
}
 
}
 
}
 
}
public int Weight
+
public string Skin
 
{
 
{
 
get
 
get
 
{
 
{
return weight;
+
return skin;
}
 
set
 
{
 
weight = value;
 
 
}
 
}
 
}
 
}
public string BarkSound
+
public string Name
 
{
 
{
 
get
 
get
 
{
 
{
return barkSound;
+
return name;
 
}
 
}
 
set
 
set
 
{
 
{
barkSound = value;
+
name = value;
 
}
 
}
 
}
 
}
public int BarkCount //you can't set bark count
+
public virtual void Bark()
//it only increments from the Bark() method
 
 
{
 
{
get
+
Console.WriteLine("Some Mammals Bark.");
{
 
return barkCount;
 
}
 
 
}
 
}
   
+
}
    public string About()
+
 
{  
+
public class Dog : Mammal
//return a string with some information about the dog
+
{
        string about = "";
+
public string BarkSound; // the sound of the dog's bark
//this refers to current object
+
about +=("\nThe dogs name is " + this.name + ".");
+
public Dog(string newName)
about +=("\nIt is " + this.age + " years old.");
+
{
about +=("\nIt weighs " + this.weight + " lb(s).");
+
Name = newName;
about +=("\nIts bark sounds like '" + this.barkSound + "'");
+
BarkSound = "Woof!!!";
about +=("\nIt has barked " + this.barkCount + " time(s)" );
 
        about += about.Replace("\n","
 
");
 
        return about;
 
 
}
 
}
  
public void Bark() {  
+
public override void Bark() {  
//make dog bark
+
base.Bark();
Console.WriteLine (this.Name + " says " + this.barkSound);
+
Console.WriteLine(this.Name + " says " + this.BarkSound);
//add 1 to the number of times the dog has barked
 
this.barkCount++ ;
 
 
}
 
}
 
public void Eat() {
 
public void Eat() {
 
//put eat code here  
 
//put eat code here  
 
}
 
}
}</csharp>
+
}
 
 
console
 
 
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class4/dogOverloadMethodScoped.cs dogOverloadMethodScoped.cs]
 
 
 
 
 
Real barking dogs...
 
 
 
[http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class4/DogBark DogBark]
 
 
 
an example of how you don't need to know how all of the classes work just how to use them
 
 
 
 
 
 
 
 
 
 
 
==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
+
public class Basenji : Dog
 
 
<csharp>public class Dog
 
 
{
 
{
//some code
+
//override constructor
 
+
public Basenji(string newName) : base(newName)
static private int dogCount; // total number of dogs
 
 
 
public Dog()
 
 
{
 
{
barkSound = "Woof!!!";
+
BarkSound = "basenjis don't bark, but they do howl and growl.";
//Add a dog to the total dog count
 
dogCount++;
 
 
}
 
}
public Dog(string newName)
+
 +
public override void Bark()
 
{
 
{
name = newName;
+
base.Bark();
barkSound = "Woof!!!";
+
Console.WriteLine("\nBasenjis dont bark.");
//Add a dog to the total dog count
+
}
dogCount++;
+
     // some more code....
}</csharp>
 
 
 
Static dougCount Example <br />
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/dogStatic.cs dogStatic.cs] - source
 
 
 
 
 
==Operator Overloading==
 
 
 
You can overload operator in c# just like you overload constuctors
 
 
 
    * Operator Overloading In C# - www.csharphelp.com
 
    * Operator Overloading in C# - HowToDoThings.com
 
 
 
<csharp>//Overloading unary operators
 
public static return_type operator op (Type t)
 
{
 
  // Statements
 
}
 
 
 
//Overloading binary operators
 
public static ClassType operator + ( object lhs, object rhs )
 
{
 
ClassType c
 
//code to implement class addition
 
return c;
 
}</csharp>
 
 
 
dog addition? extra credit
 
 
 
How could you overload + operator so that it returns a new dog when two dogs are added together...
 
 
 
===Operator Overloading Fraction Class===
 
 
 
<csharp>public static ClassType operator + ( object lhs, object rhs )
 
{
 
    ClassType c
 
    //code to implement class addition
 
    return c;
 
}</csharp>
 
 
 
Operator also refer to implicit and explicit conversions. Fractions make a good example for opertor overloading.
 
Fraction class
 
 
 
    * Create fractions from two numbers x/y or from whole numbers
 
    * Convert fractions to wholenumbers int
 
    * Convert fractions to float or double
 
    * OverRide ToString to decribe Fraction
 
 
 
 
 
Fraction Class Example of operator overloading.<br />
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/fractionConv.cs fractionConv.cs] - source<br />
 
Fraction class with implicit and explicit conversion<br />
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/fractionConv_NoTest.cs fractionConv_NoTest.cs] - source<br />
 
Fraction class with overridden operators<br />
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/fractionConvOver.cs fractionConvOver.cs] - source<br />
 
 
 
In class build a test application does thigs with frations.<br />
 
Using <br />
 
 
 
http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/fractionConv_NoTest.cs fractionDone_NoTest.cs] create a test class that...
 
 
 
* Creates a fraction out of integers (ie cast int into fractions)
 
* Adds, subtracts, and reduces some fractions
 
* Creates floting point numbers out of fractions (is cast fraction into double)
 
* Checks the eqaulity of some fractions
 
 
 
==Class Relationships==
 
Four Class Relation ships
 
 
 
* Association - 'Uses A'
 
* Containment - 'Has A'
 
* Inheritance - 'Is A'
 
* Interfaces - implements or derives from We'll do this next week in [[OOP Class6]]
 
 
 
==Association - 'Uses A'==
 
 
 
A dog uses a hydrant to relieve them self.
 
 
 
<csharp>//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>
 
}</csharp>
  
Dog Association example
+
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/DogPoly.cs DogPoly.cs] -source
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/DogHydrant.cs DogHydrant.cs] - source
 
 
 
==Containment - 'Has A'==
 
A dog has an owner
 
 
 
 
 
<csharp>Dog fido = new Dog("fido");
 
fido.Owner = new Person("Sue");</csharp>
 
 
 
<csharp>//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<br>
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/DogOwner.cs DogOwner.cs] -source
 
  
  

Revision as of 16:32, 9 October 2007

This keyword

The this keyword used within a class refers to the current instance of the class

<csharp>public Time( int CurrentYear, int CurrentDay) {

this.Year = CurrentYear;
this.Day = CurrentDay;

}</csharp>

What is UML

UML 
Unified Modeling Language.
"A modeling language is a language whose vocabulary and rules focus on the conceptual and physical representation of a system"

The Unified Modeling Language User Guide, Booch Rumbaugh Jacobson ACM Press, Addison Wesley 1999 ISBN 0201571684 The Unified Modeling Language User Guide

Simple Notation

Class notation in UML

The name of the Class should go inside of a box

Class Name


Then Two lines are added. The first line separates Attributes. The second line separates Operations. Attributes are the equivalent of fields or properties in c# and Operations are the same as methods.

Class Name
 
 
<--Class Name
<--Attributes
<--Opertaions


A Rectagle Class might look like this

Rectangle
height
width
area()
add()
move()
copy()
isEmpty()

You can also specify Attribute types with a :

Rectangle
height : Float
width : Float
area()
add()
move()
copy()
isEmpty()

Default vlaues are specified with =

Rectangle
height : Float = 6.0
width : Float = 4.0
area()
add()
move()
copy()
isEmpty()


Operator return type are also specifed with a :. Opertor parameter signatures are specified by ( name : Type )

Rectangle
height : Float = 6.0
width : Float = 4.0
area() : Float
add()(r:Rectangle)
move()
copy() :Rectangle
isEmpty() : Boolean = true

UML Resources

Dia A Drawing Program

Dia for windows

Dog Diagrams with Dia

Dog Exmaples

Simple Dog Class from week 5 DogSimple.cs - source
DogSimple.png <csharp>using System;

//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 Dog() { BarkSound = "Woof!!!"; }

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


Dog UML with Types

Dog Class with Types from week 4 DogWithTypes.cs - source

DogWithTypes.png <csharp>using System;

//Dog simple class definition public class Dog {

	public string Name;		// the dog's name

private int Age; // the dog's age public int Weight; // the dog's weight public string BarkSound; // the sound of the dog's bark private int barkCount; //The number of time the dog has barked public Dog() { BarkSound = "Woof!!!"; }

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

       barkCount ++;
       return strBark;

}

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



Inheritance - 'Is A'

ArsDigita University Course 04: Object-oriented Program Design and Software Engineering - Lecture Notes 3

C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from.

A Basenji is an African barkless dog. A Basenji is a dog. syntax

<csharp>public class Basenji : Dog {

   //override constructor
   public Basenji() : base()
   {
       barkSound = "Basenjis don't bark, but they do howl and growl.";
       dogCount++;
   }
   public override void Bark()
   {
       Console.WriteLine("Basenjis dont bark.");
   }

// some more code.... }

public class Dog { // some dog code... //methods need to be marked virtual or protected to be overridden

   public virtual void Bark() 
   { 
       //make dog bark
       Console.WriteLine (this.Name + " says " + this.barkSound);
       //add 1 to the number of times the dog has barked
       this.barkCount++ ;
   }

}</csharp>

Basenji.cs -source

Virtual Functions

A class modifier that indicates that a method can be overridden by a derived class. We will look at virtual function when we look at abstract classes

Abstract Classes

abstract - A class modifier that specifies that the class must be derived from to be instantiated.

<csharp>abstract public class Mammal { private string birth; private string skin;

public Mammal() { birth = " gives birth to live young."; skin = " has Hair."; }

public string Birth { get { return birth; } } public string Skin { get { return skin; } } }</csharp>

Inheritance example /infod/jeff/classSource/class5/DogMammal.cs -source

Polymorphic Types

Polymorphism (computer science) Different types implementing the same functionality.
polymorphism webopedia The C# Station Tutorial - Lesson 9: Polymorphism

<csharp>abstract public class Mammal { private string name; private string birth; private string skin;

public Mammal() { name = "none"; birth = " gives birth to live young."; skin = " has Hair."; }

public Mammal(string newName) { name = newName; birth = " gives birth to live young."; skin = " has Hair."; }

public string Birth { get { return birth; } } public string Skin { get { return skin; } } public string Name { get { return name; } set { name = value; } } public virtual void Bark() { Console.WriteLine("Some Mammals Bark."); } }

public class Dog : Mammal { public string BarkSound; // the sound of the dog's bark

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

public override void Bark() { base.Bark(); Console.WriteLine(this.Name + " says " + this.BarkSound); } public void Eat() { //put eat code here } }

public class Basenji : Dog { //override constructor public Basenji(string newName) : base(newName) { BarkSound = "basenjis don't bark, but they do howl and growl."; }

public override void Bark() { base.Bark(); Console.WriteLine("\nBasenjis dont bark."); }

   // some more code....

}</csharp>

DogPoly.cs -source



HomeWork

READ

  1. Chapter 9 Inside Methods p 111-123
  2. Chapter 11 Inheritance and Polymorphism p 134-154
  3. Chpater 12 Operator Overloading p 155 - 172
  4. chapter 13 Structs p 172 - 177 (relax its only 54 pages total)

DO

Extend the class that you worked on last week.
.

Demonstrate

  • Containment 'Has A'
    • Make a class that contains or is contained by your old class
  • Association - 'Uses A'
    • Make a class that uses your original class

Extra credit add two dags

roover = fido + milo;

Quiz 2 next week

Links

The C# Station Tutorial - Lesson 7: Introduction to Classes
The C# Station Tutorial - Lesson 8: Class Inheritance
The C# Station Tutorial - Lesson 9: Polymorphism
The C# Station Tutorial - Lesson 10: Properties
The C# Station Tutorial - Lesson 12: Structs
The C# Station Tutorial - Lesson 13: Interfaces
The C# Station Tutorial - Lesson 4: C# Fundamentals