Tuesday, February 16, 2016

Lesson 7

Outline

Chapter 11


Inheritance Continued (Lab)

Problem 11.7 & 11.8

11.7 (Quadrilateral Inheritance Hierarchy) Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the base class of the hierarchy. Make the hierarchy as deep (i.e., as many levels) as possible. Specify the instance variables, properties and methods for each class. The private instance variables of Quadrilateral should be the x–y coordinate pairs for the four endpoints of the Quadrilateral. Write an app that instantiates objects of your classes and outputs each object’s area (except Quadrilateral).

11.8 (Account Inheritance Hierarchy) Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e., debit)money from their accounts. More specific types of accounts also exist. Savings accounts, for instance, earn interest on the money they hold. Checking accounts, on the other hand, charge a fee per transaction.

Create base class Account and derived classes SavingsAccount and CheckingAccount that inherit from class Account. Base class Account should include one private instance variable of type decimal to represent the account balance. The class should provide a constructor that receives an initial balance and uses it to initialize the instance variable with a public property. The property should validate the initial balance to ensure that it’s greater than or equal to 0.0; if not, throw an exception. The class should provide two public methods. Method Credit should add an amount to the current balance. Method Debit should withdraw money from the Account and ensure that the debit amount does not exceed the Account’s balance. If it does, the balance should be left unchanged, and the method should display the message "Debit amount exceeded account balance." The class should also provide a get accessor in property Balance that returns the current balance.

Derived class SavingsAccount should inherit the functionality of an Account, but also include a decimal instance variable indicating the interest rate (percentage) assigned to the Account. SavingsAccount’s constructor should receive the initial balance, as well as an initial value for the interest rate. SavingsAccount should provide public method CalculateInterest that returns a decimal indicating the amount of interest earned by an account. Method CalculateInterest should determine this amount by multiplying the interest rate by the account balance. [Note: SavingsAccount should inherit methods Credit and Debit without redefining them.]

Derived class CheckingAccount should inherit from base class Account and include a decimal instance variable that represents the fee charged per transaction. CheckingAccount’s constructor should receive the initial balance, as well as a parameter indicating a fee amount. Class Checking-Account should redefine methods Credit and Debit so that they subtract the fee from the account balance whenever either transaction is performed successfully. CheckingAccount’s versions of these methods should invoke the base-class Account version to perform the updates to an account balance.

CheckingAccount’s Debit method should charge a fee only if money is actually withdrawn (i.e., the debit amount does not exceed the account balance). [Hint: Define Account’s Debit method so that it returns a bool indicating whether money was withdrawn. Then use the return value to determine whether a fee should be charged.]

After defining the classes in this hierarchy, write an app that creates objects of each class and tests their methods. Add interest to the SavingsAccount object by first invoking its CalculateInterest method, then passing the returned interest amount to the object’s Credit method.

 

Lesson 6


Outline

Chapter 11


Inheritance Continued


Diagraming
Is-A relationship
Constructors
Protected

Problem 11.5
(Shape Inheritance Hierarchy) The world of shapes is much richer than the shapes included in the inheritance hierarchy of Fig. 11.3. Write down all the shapes you can think of—both two-dimensional and three-dimensional—and form them into a more complete Shape hierarchy with as many levels as possible. Your hierarchy should have class Shape at the top. Class TwoDimensionalShape and class ThreeDimensionalShape should extend Shape. Add additional derived classes, such as Quadrilateral and Sphere, at their correct locations in the hierarchy as necessary.

 
Diagraming
 
In the previous lesson, we learned about base and derived classes where the base class is the parent class and the derived class is the child class. A derived class can only have one base class. In order for a derived class to inherit from more than one base class the current base class would need to inherit from second class. However, a base class can have many derived classes.

In the example below, we have a simple diagram outlining a series of classes and their inherited relationship. The top class Person is the root base class for all the other classes. All of the other classes in the inheritance diagram have the public and internal capabilities specified in the Person class.



The Staff base class has two derived classes (Teacher and Cleaner). Both will have the same capabilities outlined in the Staff class but can also contain separate properties and methods specific to their own class. In an inheritance diagram the arrows point back to the base class.

Is-A

Is-A is a phrase used to describe inherited relationships. In the previous diagram, you can say Staff Is-A Person and Teacher Is-A Staff. This relationship only works up the inheritance hierarchy. We can say Teacher is a Staff but cannot say staff is a teacher. So all teachers are staff but not all staff are teachers.

Constructors

Earlier in the lessons we talked about class constructors and their ability to accept values passed into the class when the class is instantiated into an object.  

Suppose the staff class did not have a default constructor and only supported constructors that took one or more values?

class Staff
{
    private readonly string payGrade = string.Empty;
 
    public Staff(string payGrade)
    {
        //Update private variable to specified pay grade 
        this.payGrade = payGrade;
    }
}

Our Teacher class is derived from the Staff class.

class Teacher:Staff
{
 
}

This will cause an exception. Since Staff cannot be created without the pay grade parameter the Teacher class must have at least one constructor to support the Staff class requirement.

class Teacher:Staff
{
 
    public Teacher(string payGrade)
        : base(payGrade)
    { }
 
}

The Teacher class accepts the parameters via its constructor and then passes it to the base class (Staff) via the : base. The "base" refers to the current class base class. The Teacher class can still accept its own parameters.  

private string className = string.Empty;
 
public Teacher(string className, string payGrade)
    : base(payGrade)
{
    this.className = className;
}

In the example above, the Teacher class accepts two parameters (className and payGrade). Pay grade is passed to the base constructor and the class name is handled locally within the Teacher class. Note: the sequence of the parameters does not play a factor. It would have been acceptable to reverse the order of the parameters.

Lets extend our Teacher class to support multiple constructors.

class Teacher:Staff
{
    private string className = string.Empty;
    private string firstName = string.Empty;
    private string lastName = string.Empty;
 
    public Teacher(string className, string payGrade)
        : base(payGrade)
    {
        this.className = className;
    }
 
    public Teacher(string firstName, string lastName, string className, string payGrade): this(className, payGrade)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }
 
}

The Teacher class now supports to separate constructors to instantiate the class into a object but only one constructor references the base class. With the use of constructor chaining we can call the other Teacher constructor and pass in the values collected from this constructor. Constructor chaining is acceptable as long as ultimately the base class constructor require is satisfied.

Protected

Private and Protected are very similar. Both cannot be accessed via an instantiated object. Both can be seen by their peer methods within the same brackets "{}". However, there is one main distinction. Private variables and methods can only be seen within the class. Protected variables and methods can be referenced in the derived class.

 
class Staff
{
    private readonly string payGrade = string.Empty;
    protected int Age;
 
    public Staff(string payGrade)
    {
        //Update private variable to 
        this.payGrade = payGrade;
    }
}

class Teacher:Staff
{
    private string className = string.Empty;
    private string firstName = string.Empty;
    private string lastName = string.Empty;
 
    public Teacher(string className, string payGrade)
        : base(payGrade)
    {
        this.className = className;
    }
 
    public Teacher(string firstName, string lastName, string className, string payGrade): this(className, payGrade)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        Age = 27;
    }
}

The Age variable is accessible in the Teacher class due to the protected description. Note: Protected is only accessible in the immediate derived class. In the diagram at the top, should Person have a protected variable or method, it would be accessible in the Staff class but NOT in the Teacher or Cleaner class.









Lesson 5

Outline


Chapter 11

Inheritance
Base Classes
Derived Classes

Inheritance

Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the public or internal members declared in ClassB and ClassA.

Each derived class is generally a specific version of the parent or base class. If we created a Vehicle class and then derived Car class from Vehicle class. The Car class would have access to all public or internal properties and methods of the Vehicle class.

public class Vehicle
{public int NumberOfDoors { get; set; }
public string Transmission { get; set; }
}
 
public class Car : Vehicle
{public bool Convertable { get; set; }
public bool HasTrunk { get; set; }
}
Class inheritance is denoted by a colon (:) separating the current class name from the base class name. public class Car : Vehicle tells us that Car is derived from Vehicle. Vehicle is the base class and Car is the derived class. Since the Car class is derived from Vehicle we can say that all Cars are Vehicles but not all Vehicles are Cars. Inheritance only works downstream and inheriting a base class does not change the base class. But any change to the base class will be reflected in the derived class. 

Car myCar = new Car();
myCar.Convertable = true;
myCar.NumberOfDoors = 2;Above we created an instance of the Car class and called the object myCar. We then changed the property values of myCar. If you look closely, we changed the convertible property value which is located in the Car class and then changed the property value of the NumberOfDoors property from the Vehicle class. The code looks exactly the same. The myCar class does not care which level a property or method is defined only whether it is available (public or internal) or not (private).

Multiple class can be derived
We can create multiple classes based on a parent class.
public class Car : Vehicle
{public bool Convertable { getset; }
public bool HasTrunk { getset; }
}
public class Truck : Vehicle
{public bool TowPackage { getset; }
public bool FourWheelDrive { getset; }
}
Both of the above classes are derived from the same class (Vehicle), Even though they contain different properties they have the same base class. It can be read like this "All cars are vehicles but not all vehicles are cars"

Base Classes

A base class is a class that is used to create, or derive, other classes. Classes derived from a base class are called child classes, subclasses or derived classes. A base class does not inherit from any other class and is considered parent of a derived class.

The base class forms the means by which inheritance is accomplished through derivation. A class derived from a base class inherits both data and behavior. For example, vehicle can be a base class from which the derived classes car and bus can be derived. Both car and bus are vehicles, and they each represent their own specializations of base class.

Base class helps to create a specialized class that can reuse the code that is implicitly gained from the base class (except constructors and destructors) and extend the functionality of base class by adding or overriding members relevant to derived class in derived class. In C#, events are declared in base class that can be raised from derived classes. Generic classes that are used to encapsulate operations that are not specific to a particular data type serve as base classes, providing generic behavior so as to achieve flexibility and code reusability.


Derived Classes

A derived class is a class created, or derived from another existing class. The existing class from which the derived class gets created through inheritance is known as base or super class.

While inheriting from base class, the derived class implicitly inherits all the members (except constructors and destructors) which it reuses, extends and modifies the behavior of the base class. The derived class overrides the properties and methods of the base class so that it represents the specialized version of base class. C# provides ability to override and hide methods of base class in derived class which makes both classes to evolve freely and maintain binary compatibility.

Inheritance is one of the key functionalities required for all object oriented programming languages.


Tuesday, February 9, 2016

Lesson 4

Outline

Chapter 10

Override Methods
ToString()
Garbage Collection
Object Initializers

Override Methods

An override method provides a new implementation of a member that is inherited from a base class. The method that is overridden by an override declaration is known as the overridden base method.
The overridden base method must have the same signature as the override method.

You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.

An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.

You cannot use the new, static, or virtual modifiers to modify an override method
.
An overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property, and the overridden property must be virtual, abstract, or override.

class Person
{
      public virtual string SaveDay()
      {
          return "Day Saved";
      }
}
class Student : Person
{
      public override string SaveDay()
     {
          return "Day is Lost";
     }
}

In the above example we have two classes (Person and Student) The Person class contains a virtual method SaveDay that returns a string value "Day Saved". The Student class above inherits from Person. If we did nothing else the Student class would behave exactly like the Person class. However, in the Student class we have chosen to override the SaveDay() method. We can do this because of the virtual designation in the base class. By using the override designation we can change how the method behaves in the derived class. Note that the SaveDay() method in the Student class returns "Day is Lost". If we created two object based of these classes myPersom and myStudent, both would have the same methods but the method themselves would behave differently.

ToString()

Object.ToString is the major formatting method in the .NET Framework. It converts an object to its string representation so that it is suitable for display.

Classes frequently override the Object.ToString method to provide a more suitable string representation of a particular type. Types also frequently overload the Object.ToString method to provide support for format strings or culture-sensitive formatting.

public class Vehicle
{
    public string Make { get; set; }
    public string Model { get; set; }
    public override string ToString()
    {
       return Make + " " + Model;
    }
}

In this class we have two properties to collect the make and model of our vehicle. We added the override ToString()method to change its behavior within the vehicle class.

static void Main(string[] args)
{
    Vehicle myCar = new Vehicle();
    myCar.Make = "Dodge";
    myCar.Model = "Charger";
    Console.WriteLine("My Car is a " + myCar.ToString());
}

Our implementation of the Vehicle class (myCar) has the properties set to Dodge and Charger. When we call the ToString() method of the object it will return "Dodge Charger" because we have changed the way the ToString() works in the Vehicle class.

Garbage Collection

The .NET Framework's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap. As long as address space is available in the managed heap, the runtime continues to allocate space for new objects. However, memory is not infinite. Eventually the garbage collector must perform a collection in order to free some memory. The garbage collector's optimizing engine determines the best time to perform a collection, based upon the allocations being made. When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.

Object Initializers

Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor.

In our myCar implementation above we set the value of the Make and Model class implicitly. In using object initializers we could do the following:

Vehicle myCar = new Vehicle() { Make = "Dodge", Model = "Charger" }

This is functionally the same as our previous sample but reduces the lines of code in our project. There are no hard and fast rules on when to use one approach over another. Choose which you are comfortable with and be consistent.







 

Tuesday, January 26, 2016

Lesson 3

Outline


Chapter 10

Properties
     Full
     Private Fields
     Auto-Implemented
     Read Only
     Write Only
     Calculated

Properties

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

Properties are used in classes to enable the derived objects to hold values (even different values) within the object. Properties generally are associated with a private field variable that is not accessible outside the class/object. The property provides a method to set or get the value of the private field from outside the object.

It is important to think of properties as adjectives or descriptors of the object. Suppose we had a person class. Some properties might be used to describe the object (class instance)  such as name, height, weight, race, or gender. The person class containing these properties allow the object to maintain its own unique description. So a person object for me might have different values for each of the properties even though they are based on the same class.

Full Property

Full properties allow the greatest flexibility for a developer. All of the parts of the property are exposed and can be modified to create the desired behavior. Below we have a simple person class with a single property Name.

class Person
{
 
    private string name;

     public string Name
     {    
     get { return name; }
     set { name = value; }
     }  
}
 


Examining the property we see that part of the property is public and part is private. The public Name is what the developer will see when the property is accessed from an object. The private name will hold the value set by developer.
Person me = new Person();
me.Name = "Larry Taylor";

In the code above we first create an instance of the Person class, in this case our object will be named me. (Note: me is a horrible object name, please try to use a descriptive name for your instance). Once me is instantiated we can then access the properties defined in the class via the newly created object. me.Name is accessing the public Name property we defined in the class allowing me to set the value of the private name variable to "Larry Taylor"
When a value is applied to a property it accesses the properties "set" statement. In our name property we defined we have this statement: set { name = value; } When value the is applied the property sets the private field "name" equal to the value being passed into the property.
The reverse is true as well. When the get accessor is called the value stored in the private field is returned.

Console.WriteLine(me.Name);

Name refers to the property and within the property the private field value is returned.

Auto-Implemented

Sometimes (well most of the time) your class will only need simple properties. C# has a shortcut for creating simple properties call auto-implemented properties. These properties work EXACTLY like the full properties above have a lot of the plumbing hidden from the developer. The Name property we created above using the full property approach would be written as follows using auto-implemented:
       
public string Name { get; set; }

We still have a get and set to apply and retrieve the value held in the private field. But where is the private field? It is still there only hidden. This approach makes for a much simpler property to generate within the class. The shortcut for the auto-implemented property is prop followed by two tabs.

Read Only Properties

Read only properties cannot have the private value set outside of the class. We can still read it from outside the class but not change it. Think about a class for account management at a bank. We might want to set the account number once from within the class and then all the methods within the class access the account number during the debit and credit functions of the account management process. We would not want the account number to be changed mid process when making a deposit.

Read Only Full Property

     private string name;

     public string Name
     {    
     get { return name; }
     } 

Note: The set accessor is no longer present.

Read Only Auto-Implemented Property

public string Name { get; private set; }

The set accessor is marked private to prevent access from outside the class

The value of the property can only be set within the constructor when we instantiate a class into an object or within a method located in the same class. 

Write Only Property

These are properties that can be set outside the class but not read from outside the class. This type of property is not used that often. Examples are below:

Read Only Full Property

     private string name;

     public string Name
     {    
     set { name = value; }
     }  
 
Read Only Auto-Implemented Property

public string Name { private get; set; }

Properties are a useful way to enable your class/object to hold data specific to that instance of the object.

Calculated Properties

This type of property "assembles" or generates the return value of the property dynamically when the get accessor is called.

public string FirstName { get; set; }
public string LastName { get; set; }

public string FullName
{get { return FirstName + ' ' + LastName; }
} 
Our example above contains 3 properties. Two of the properties allow the developer to read and write the first and last name. The third property is a read only property that assembles the first and last name property values into a full name property. Since we have the first and last name there is no need to create a property to accept a full name. We just use the values we already have in the class.

Properties are an essential component of object oriented programming. They allow our objects to retain values specific to its instance and allow for proper encapsulation of information.


Friday, January 15, 2016

Lesson 2

Outline


Chapter 10
  • Constructors
    • Default/Parameterless
    • Overloaded Constructors
  • Destructors
  • Static Class Members
  • Read Only Instance Field
Constructors

We introduced the concept of a class constructor last week. We will expand on it this week using overloaded constructors. A class can support one to many constructors in a class. It must follow these rules:

All constructors must have the same name as the class.
All constructors must have a different combination of parameter types.


namespace Chapter10_HousePlans_Sample
{
    /// <summary>
    /// Simple house plans class
    /// </summary>
    class HousePlans
    {
        /// <summary>
        /// Private (not visible outside of class) variable to keep track of door status (defaults to false:closed)
        /// </summary>
        private bool doorOpened;
        private int bedrooms;
        private int bathrooms;
 
        /// <summary>
        /// Public (visible outside the class and even the project) method to change to the door open status to true:opened
        /// </summary>
        public void OpenDoor()
        {
            doorOpened = true;
        }
 
        public HousePlans()
        { 
        }
 
        public HousePlans(int bedrooms)
        {
            this.bedrooms = bedrooms;
        }
 
        public HousePlans(int bedrooms, int bathrooms)
        {
            this.bedrooms = bedrooms;
            this.bathrooms = bathrooms;
        }
 
        /// <summary>
        /// Public (visible outside the class and even the project) method to change to the door open status to false:closed
        /// </summary>
        public void CloseDoor()
        {
            doorOpened = false;
        }
 
        /// <summary>
        /// Overrides the standard tostring method (available in all object) to a specific string
        /// </summary>
        /// <returns>Status of the door(open or closed)</returns>
        public override string ToString()
        {
            return "The door is " + (doorOpened ? "Open" : "Closed");
        }
    }
}

In our sample class above we have three constructors, a parameterless constructor, a constructor that takes one integer, and a constructor that takes two integers. Note each constructor takes a different number of parameters. We could also differentiate constructors by the type of parameter as well.

public HousePlans(int bedrooms)
{
    this.bedrooms = bedrooms;
}
 
public HousePlans(string paintColor)
{
    this.paintColor = paintColor;
}

If we had two constructors in our class that each took a single parameter as in our example above, there would be no conflict. Even though the constructors are identical in the number of parameters they still differ by type. One constructor takes an integer parameter and the other takes a string. Also, once we add our own constructor the hidden default constructor is removed. If we still want a parameterless constructor in our class we will need to manually define it.

NOTE: THE PARAMETER NAMES NOT CONSIDERED WHEN OVERLOADING A CONSTRUCTOR. ONLY THE NUMBER OF PARAMETERS AND THIER TYPES.

Destructors

A constructor creates an instance of an object based on a class. A destructor destroys the created object allowing the system garbage collection to free up any resources used by the object. Without destructors the application could grow in memory taking up more and more resources. This is called a resource or memory leak.

To implement a destructor in our class we implement the IDisposable interface within our class. Implementing an interface in a class will cause the class to stub out code necessary to support the interface functionality.

Adding the IDisposable interface to our HousePlans class:
class HousePlansIDisposable

To add the interface to our class, we add a colon to the right of the class name. After the interface is added it must be implemented (supported) within the class. To implement the IDisposable interface click on the word "IDisposable" and a small underscore will appear.








After the underscore appears, hover the cursor over it to enable a small drop down.












When the menu appears, click the "Implement interface IDisposable". Viewing the code of the class you will notice a new method "Dispose"


public void Dispose()
{
    throw new NotImplementedException();
}


Remove the "throw new NotImplementedException():". Once implemented, you can "dispose" of your object like this:


//Creates an instance of the class
HousePlans myHouse = new HousePlans();
//Disposes or removes the instance of the class
myHouse.Dispose();


It is a good idea to implement the IDisposable interface even though we are not doing anything within the dispose method. It does provide a nice way to remove any unused objects as well as a place holder when we do need to perform some tasks when disposing of an object. We will discuss that in a later lesson.


Static Class Members


A static class is basically the same as a non-static class, but with one difference. A static class cannot be instantiated. That is, we cannot create an instance of a static class (i.e. use the new keyword). Static classes are generally used to hold to hold methods that do not rely on an instance value.


You may recall in the previous lesson creating the myHouse and yourHouse objects. While based on the same class they may still contain different attribute values. These differences are based on the instance of the object. A static class does not use any instance settings and therefore does not require the new keyword.


public static class TemperatureConverter
{
    public static double CelsiusToFahrenheit(string temperatureCelsius)
    {
        // Convert argument to double for calculations. 
        double celsius = Double.Parse(temperatureCelsius);
 
        // Convert Celsius to Fahrenheit. 
        double fahrenheit = (celsius * 9 / 5) + 32;
 
        return fahrenheit;
    }
 
    public static double FahrenheitToCelsius(string temperatureFahrenheit)
    {
        // Convert argument to double for calculations. 
        double fahrenheit = Double.Parse(temperatureFahrenheit);
 
        // Convert Fahrenheit to Celsius. 
        double celsius = (fahrenheit - 32) * 5 / 9;
 
        return celsius;
    }
}


This class has the same look and feel of the HousePlans class with one exception. Notice the keyword "static", this denotes that the class does require an instance to access the methods. To use the HousePlans methods we had to create an instance of the HousePlans class.


//Creates an instance of the class
HousePlans myHouse = new HousePlans();
//Access method within the new instance
myHouse.CloseDoor();
//Disposes or removes the instance of the class
myHouse.Dispose();


To access a method within a static class we only need to call the method directly.


//Static classes/methods do not require an instance to be accessed
TemperatureConverter.CelsiusToFahrenheit("32");


When a class is going to work EXACTLY the same consider making the class a static class.


Read Only Instance Field


Read only fields within a class can only have a value applied where the instance is defined or within a constructor.


//Read only field with value applied
public readonly string Address = "123 Main";
 
 
//Constructor that sets value of read only field
public HousePlans(int bedrooms, string address)
{
    this.bedrooms = bedrooms;
    this.Address = address;
}

Once the instance of a class is created, this value cannot be changed. The value of the field can be accessed from an instance of the class.

//Create instance of the HousePlans class.
HousePlans yourHouse = new HousePlans(3, "1000 Capitol");
//Access the read only field
Console.WriteLine(yourHouse.Address);


Attempting to set a value at the instance level will cause an exception.
















Read only fields are used when the value can change from one instance of a class to another but once instaniated the cannot change for the life of the object.