Difference between revisions of "Programming Tutorial: Classes Part 2"

esse quam videri
Jump to: navigation, search
m (Text replacement - "</csharp>" to "</syntaxhighlight>")
m (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >")
Line 3: Line 3:
 
An important component of a class is its constructor.  A constructor is a special method that is called when whenever you declare an object.  A constructor has a return type of its respective class.  Here's a possible constructor for our dog class:
 
An important component of a class is its constructor.  A constructor is a special method that is called when whenever you declare an object.  A constructor has a return type of its respective class.  Here's a possible constructor for our dog class:
  
<csharp>public dog()
+
<syntaxhighlight lang="csharp" line="1" >public dog()
 
{
 
{
  
Line 10: Line 10:
 
dog() will return a new instance of object type "dog".  As we discussed previously, you create an instance of the dog class by calling:
 
dog() will return a new instance of object type "dog".  As we discussed previously, you create an instance of the dog class by calling:
  
<csharp>dog myDog = new dog();</syntaxhighlight>
+
<syntaxhighlight lang="csharp" line="1" >dog myDog = new dog();</syntaxhighlight>
  
 
What you didn't realize before is that "new dog()" is calling the constructor and returning an instance of dog.
 
What you didn't realize before is that "new dog()" is calling the constructor and returning an instance of dog.
Line 16: Line 16:
 
The constructor should be the first function in your class.  This is not necessary, but it is the standard convention and it makes for more readable and organized code.  In our dog class from the previous tutorial, we omitted a constructor and everything worked just fine.  This is because C# (and in fact most languages) will generate a default constructor if one is not defined.  A constructor that does nothing is admittedly pointless, and since constructors are just functions, you can overload them.  Here's an example of overloaded constructor:
 
The constructor should be the first function in your class.  This is not necessary, but it is the standard convention and it makes for more readable and organized code.  In our dog class from the previous tutorial, we omitted a constructor and everything worked just fine.  This is because C# (and in fact most languages) will generate a default constructor if one is not defined.  A constructor that does nothing is admittedly pointless, and since constructors are just functions, you can overload them.  Here's an example of overloaded constructor:
  
<csharp>public dog(string name, int age, int weight)
+
<syntaxhighlight lang="csharp" line="1" >public dog(string name, int age, int weight)
 
{
 
{
 
     this.name = name;
 
     this.name = name;
Line 25: Line 25:
 
Let's examine what is going on here.  The constructor is getting in 3 parameters, and they happen to have the same member names as the dog class's members from before.  We differentiate the two with the "this" keyword.  "This" means that we are referring to that specific object's instance of the member.  If it's a little easier to understand, here's an equivalent constructor:
 
Let's examine what is going on here.  The constructor is getting in 3 parameters, and they happen to have the same member names as the dog class's members from before.  We differentiate the two with the "this" keyword.  "This" means that we are referring to that specific object's instance of the member.  If it's a little easier to understand, here's an equivalent constructor:
  
<csharp>public dog(string dogName, int dogAge, int dogWeight)
+
<syntaxhighlight lang="csharp" line="1" >public dog(string dogName, int dogAge, int dogWeight)
 
{
 
{
 
     this.name = dogName;
 
     this.name = dogName;
Line 40: Line 40:
 
Inheritance is an important component of classes and object oriented programming.  An inherited object (a class is a code object) is basically a newer version of a previously created object.  However, an inherited object can have new members and methods that the original object didn't have.  Continuing with our dog example, a dog is a generic classification for a specific type of animal.  There are specific breeds of dog however, and they all have unique traits.  For example, a Poodle is fluffy whereas a Chihuahua is not.  Let's add a string for our fur type:
 
Inheritance is an important component of classes and object oriented programming.  An inherited object (a class is a code object) is basically a newer version of a previously created object.  However, an inherited object can have new members and methods that the original object didn't have.  Continuing with our dog example, a dog is a generic classification for a specific type of animal.  There are specific breeds of dog however, and they all have unique traits.  For example, a Poodle is fluffy whereas a Chihuahua is not.  Let's add a string for our fur type:
  
<csharp>class dog
+
<syntaxhighlight lang="csharp" line="1" >class dog
 
{
 
{
 
     public string name, furType;
 
     public string name, furType;
Line 50: Line 50:
 
To make a poodle class that inherits from the dog class, we would write:
 
To make a poodle class that inherits from the dog class, we would write:
  
<csharp>class poodle : dog
+
<syntaxhighlight lang="csharp" line="1" >class poodle : dog
 
{
 
{
  
Line 57: Line 57:
 
We use a colon to say that we are inheriting from a class.  The class specified after the colon is the class we are inheriting from.  While there is no code to speak of inside of the brackets, we treat the poodle class as though it has all the same code that makes up the dog class.  poodle has name, furType, age, and weight.  It also has access to any methods defined in dog, so long as their access level is public.  Since all poodles are fluffy, we could add that into its constructor, since we know that a poodle's fur type will never be anything other than "fluffy."
 
We use a colon to say that we are inheriting from a class.  The class specified after the colon is the class we are inheriting from.  While there is no code to speak of inside of the brackets, we treat the poodle class as though it has all the same code that makes up the dog class.  poodle has name, furType, age, and weight.  It also has access to any methods defined in dog, so long as their access level is public.  Since all poodles are fluffy, we could add that into its constructor, since we know that a poodle's fur type will never be anything other than "fluffy."
  
<csharp>class poodle : dog
+
<syntaxhighlight lang="csharp" line="1" >class poodle : dog
 
{
 
{
 
     public poodle()
 
     public poodle()
Line 69: Line 69:
 
An important thing to note is that we can also call dog's constructor.  We do that by calling "base" in the same line as poodle's overloaded constructor:
 
An important thing to note is that we can also call dog's constructor.  We do that by calling "base" in the same line as poodle's overloaded constructor:
  
<csharp>public poodle(string name, int age, int weight) : base(name, age, weight)
+
<syntaxhighlight lang="csharp" line="1" >public poodle(string name, int age, int weight) : base(name, age, weight)
 
{
 
{
 
     furType = "fluffy";
 
     furType = "fluffy";
Line 78: Line 78:
 
We can add methods and members that are unique to poodle and not present in dog.  A poodle-exclusive action might be going to a poodle contest.  Let's edit poodle to have this function:
 
We can add methods and members that are unique to poodle and not present in dog.  A poodle-exclusive action might be going to a poodle contest.  Let's edit poodle to have this function:
  
<csharp>class poodle : dog
+
<syntaxhighlight lang="csharp" line="1" >class poodle : dog
 
{
 
{
 
     public poodle()
 
     public poodle()
Line 93: Line 93:
 
So now poodle has a method that is not in the original dog class.  What it boils down to is that objects that are inherited by other objects act as an effective "template" object.  Code can be written once and used in a multitude of ways.  If you are writing a program that utilizes a set of objects that can be abstracted into a more general object, you should use inheritance.  This also makes code far easier to manage; if you need to change something in a base class, then all objects that inherit from it will be updated as well.  Here's our updated program:
 
So now poodle has a method that is not in the original dog class.  What it boils down to is that objects that are inherited by other objects act as an effective "template" object.  Code can be written once and used in a multitude of ways.  If you are writing a program that utilizes a set of objects that can be abstracted into a more general object, you should use inheritance.  This also makes code far easier to manage; if you need to change something in a base class, then all objects that inherit from it will be updated as well.  Here's our updated program:
  
<csharp>using System;
+
<syntaxhighlight lang="csharp" line="1" >using System;
 
using System.Collections.Generic;
 
using System.Collections.Generic;
 
using System.Linq;
 
using System.Linq;

Revision as of 18:29, 25 January 2016

Constructors

An important component of a class is its constructor. A constructor is a special method that is called when whenever you declare an object. A constructor has a return type of its respective class. Here's a possible constructor for our dog class:

1 public dog()
2 {
3 
4 }

dog() will return a new instance of object type "dog". As we discussed previously, you create an instance of the dog class by calling:

1 dog myDog = new dog();

What you didn't realize before is that "new dog()" is calling the constructor and returning an instance of dog.

The constructor should be the first function in your class. This is not necessary, but it is the standard convention and it makes for more readable and organized code. In our dog class from the previous tutorial, we omitted a constructor and everything worked just fine. This is because C# (and in fact most languages) will generate a default constructor if one is not defined. A constructor that does nothing is admittedly pointless, and since constructors are just functions, you can overload them. Here's an example of overloaded constructor:

1 public dog(string name, int age, int weight)
2 {
3     this.name = name;
4     this.age = age;
5     this.weight = weight;
6 }

Let's examine what is going on here. The constructor is getting in 3 parameters, and they happen to have the same member names as the dog class's members from before. We differentiate the two with the "this" keyword. "This" means that we are referring to that specific object's instance of the member. If it's a little easier to understand, here's an equivalent constructor:

1 public dog(string dogName, int dogAge, int dogWeight)
2 {
3     this.name = dogName;
4     this.age = dogAge;
5     this.weight = dogWeight;
6 }

In this case, omitting "this" from the above code would still compile, but it's poor form and an understanding of "this" is important. If you're still a little confused on "this, " here's more information: http://msdn.microsoft.com/en-us/library/dk1507sz(VS.71).aspx

This would be an addition to the first empty constructor, it is not replacing it. This doesn't give us functionality that we didn't have before, but that's because this is a simplified example that shows the basics of class functionality.

Inheritance

Inheritance is an important component of classes and object oriented programming. An inherited object (a class is a code object) is basically a newer version of a previously created object. However, an inherited object can have new members and methods that the original object didn't have. Continuing with our dog example, a dog is a generic classification for a specific type of animal. There are specific breeds of dog however, and they all have unique traits. For example, a Poodle is fluffy whereas a Chihuahua is not. Let's add a string for our fur type:

1 class dog
2 {
3     public string name, furType;
4     public int age, weight;
5 
6     // Constructors omitted
7 }

To make a poodle class that inherits from the dog class, we would write:

1 class poodle : dog
2 {
3 
4 }

We use a colon to say that we are inheriting from a class. The class specified after the colon is the class we are inheriting from. While there is no code to speak of inside of the brackets, we treat the poodle class as though it has all the same code that makes up the dog class. poodle has name, furType, age, and weight. It also has access to any methods defined in dog, so long as their access level is public. Since all poodles are fluffy, we could add that into its constructor, since we know that a poodle's fur type will never be anything other than "fluffy."

1 class poodle : dog
2 {
3     public poodle()
4     {
5         furType = "fluffy";
6     }
7 }

Every poodle made will have it's furType set to "fluffy" upon initialization.

An important thing to note is that we can also call dog's constructor. We do that by calling "base" in the same line as poodle's overloaded constructor:

1 public poodle(string name, int age, int weight) : base(name, age, weight)
2 {
3     furType = "fluffy";
4 }

The class we are inheriting from is known as the "base class". ": base(name, age, weight)" calls the base class's constructor, while also passing along the variables from the constructor definition. This is useful if we want to use the base constructor and don't want to repeat code, one of the main advantages of objects and inheritance.

We can add methods and members that are unique to poodle and not present in dog. A poodle-exclusive action might be going to a poodle contest. Let's edit poodle to have this function:

 1 class poodle : dog
 2 {
 3     public poodle()
 4     {
 5         furType = "fluffy";
 6     }
 7 
 8     public void goToAPoodleContest()
 9     {
10         Console.WriteLine(string.Format("{0} went to a poodle contest!", this.name));
11     }
12 }

So now poodle has a method that is not in the original dog class. What it boils down to is that objects that are inherited by other objects act as an effective "template" object. Code can be written once and used in a multitude of ways. If you are writing a program that utilizes a set of objects that can be abstracted into a more general object, you should use inheritance. This also makes code far easier to manage; if you need to change something in a base class, then all objects that inherit from it will be updated as well. Here's our updated program:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace classTutorial
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12 
13             dog myDog = new dog();
14 
15             myDog.name = "Snapple";
16             myDog.age = 4;
17             myDog.weight = 110;
18 
19             Console.WriteLine(
20                 string.Format("Name:  {0}\r\nAge:  {1}\r\nWeight:  {2}\r\n", 
21                 myDog.name, myDog.age, myDog.weight));
22 
23             myDog.bark();
24 
25             poodle myPoodle = new poodle();
26             myPoodle.name = "Hercules";
27             myPoodle.age = 5;
28             myPoodle.weight = 30;
29 
30             Console.WriteLine();
31             myPoodle.goToAPoodleContest();
32 
33             Console.ReadKey();
34         }
35     }
36 
37     class dog
38     {
39         public string name, furType;
40         public int age, weight;
41 
42         public dog()
43         {
44 
45         }
46 
47         public dog(string name, int age, int weight)
48         {
49             this.name = name;
50             this.age = age;
51             this.weight = weight;
52         }
53 
54         public void bark()
55         {
56             Console.WriteLine("Woof!");
57         }
58     }
59 
60     class poodle : dog
61     {
62         public poodle()
63         {
64             furType = "fluffy";
65         }
66 
67         public void goToAPoodleContest()
68         {
69             Console.WriteLine(string.Format("{0} went to a poodle contest!", this.name));
70         }
71     }
72 }