Difference between revisions of "Intro Week 10"

esse quam videri
Jump to: navigation, search
m (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >")
m (Text replacement - "</csharp>" to "</syntaxhighlight>")
Line 15: Line 15:
 
//constructor code
 
//constructor code
 
}
 
}
}</csharp>
+
}</syntaxhighlight>
  
 
Simple Dog class with constructor
 
Simple Dog class with constructor
Line 38: Line 38:
 
//put eat code here  
 
//put eat code here  
 
}
 
}
}</csharp>
+
}</syntaxhighlight>
  
 
Console Example [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class4/dog1/Dog.cs Dog.cs] <br />
 
Console Example [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class4/dog1/Dog.cs Dog.cs] <br />
Line 87: Line 87:
 
}
 
}
  
}</csharp>
+
}</syntaxhighlight>
  
 
Console Example [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class4/dogOverload.cs dogOverload.cs]
 
Console Example [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class4/dogOverload.cs dogOverload.cs]

Revision as of 18:29, 25 January 2016


Constructors

A classes constructor is the method that is run when an instance of the class is created. If you do not provide a constructor the compiler provides one for you. The constructor is used to initiate all member properties. Unlike intrinsic types variables that are not initialized will be set to 0 or "" or null by the compiler in the default constructor.

The constructor allows you to set default values for the properties of the class. If you do not provide a constructor then a default constructor will be provided. The default constructor will initialize all of the properties in the class to empty/zero/null values.

constructor example

1 public class Dog
2 {
3 	public Dog()
4 	{
5 		//constructor code
6 	}
7 }

Simple Dog class with constructor

 1 //Dog simple class definition
 2 public class Dog
 3 {
 4  	public string Name;		// the dog's name
 5 	public int Age;			// the dog's age
 6 	public int Weight;	// the dog's weight
 7 	public string BarkSound;	// the sound of the dog's bark
 8 	
 9 	public Dog()
10 	{
11 		BarkSound = "Woof!!!";
12 	}
13 
14 	public void Bark() { 
15 		//put bark code here
16 	}
17 	public void Eat() {
18 		//put eat code here 
19 	}
20 }

Console Example Dog.cs

OverLoading Constructors

Overloading a constructor allows object creation to happen in different ways with different parameters. The class can be overloaded to accept different parameters but each constructor method must have a unique parameter signature. Dog class with overloaded constructor

 1 //Dog overloaded class definition
 2 public class Dog
 3 {
 4  	public string Name;		// the dog's name
 5 	public int Age;			// the dog's age
 6 	public int Weight;			// the dog's weight
 7 	public string BarkSound;	// the sound of the dog's bark
 8 	
 9 	public Dog()
10 	{
11 		BarkSound = "Woof!!!";
12 	}
13 	public Dog(string newName)
14 	{
15 		Name = newName;
16 		BarkSound = "Woof!!!";
17 	}
18 	public Dog(string newName, string newBarkSound )
19 	{
20 		Name = newName;
21 		BarkSound = newBarkSound;
22 	}
23 
24 	public void Bark() { 
25 		//put bark code here
26 	}
27 	public void Eat() {
28 		//put eat code here 
29 	}
30         public string About() {
31            string strAbout = "";
32            strAbout += this.Name;
33            strAbout += " " + this.Age;
34            strAbout += " " + this.Weight;
35            strAbout += " " + this.BarkSound;
36            return strAbout;
37         }
38 
39 }
40 
41 }

Console Example dogOverload.cs

Art as a windows app

Art history flash cards

Intro To Programming Art Flash Cards

As a windows app

http://iam.colum.edu/introtoprogramming/jeff/sp10/WindowsFormsApplicationArts.zip

Homework

Make the Art as a windows app into a testing app.