C#

Chapter 4

Addendum

Object Oriented Programming (OOP) vs. Procedural Programming

What have we been doing so far???

All of the programs we have written so far have been of a procedural approach. This means that they are written to follow a list of steps going through various paths. Although this method of programming has allowed us to learn the basics of C#, we must learn the concepts of object oriented programming (OOP) in order to take full advantage of C# and other high level languages (like Java). The methods we have created so far are known as static class methods. When working with OOP we will learn to use another type of method known as non-static instance methods.

Analogy Example: Making Nachos

Put chips on a plate, add some beans & cheese, and jalapenos and nuke in microwave for a few minutes. During this process you don’t think about: how the microwave works: the wiring on the inside, you don’t reprogram the microwave (even if you cooked a different dish the last time it was used), you don’t look inside the microwave to find out how it works. You deal with the microwave at a certain level of detail. In OOP the level of detail at which you’re working is the level of abstraction. In other words you don’t’ deal with the details of the microwave’s innards. You treat your microwave as a box, you put the nachos in, push a few buttons using the microwave’s interface, and magically out comes dinner!

Using the Procedural Approach

  • This approach would require you to spell out the details of how the microwave specifically works to cook nachos. It would not be treated as a ‘black-box’, but rather you would deal with its operation on a detailed level. Explaining the electronics and other inner components and how they work just to cook nachos.

Using the OOB Approach

  • In the OO approach you first identify the types of objects in the problem: chips, beans, cheese, jalapenos, microwave oven. Then you model these objects without regard for the details of how they might be used in the final program. For example, you can model cheese as an object in isolation from other objects and then combine it with the beans; chips, jalapenos and the microwave oven make them interact. This level of work issaid to be done as the level of basic objects.

Classification

Just like science class taxonomies we need to classify our objects. We classify to make our problem less complex, more flexible, and reusable.

Think about how you might classify a microwave…

It’s an oven (What’s an oven?) -> a kitchen appliance (What’s a kitchen appliance?) -> a device the cooks / prepares food -> …

In OO terms, “your” microwave is an instance of the class microwave. The class microwave is a subclass of the class oven, and the class oven is a subclass of the kitchen appliance (and so on).

Defining Your Class

Arrays hold multiple variables of the same type. Classes allow us to store multiple variables of various types and their behaviors using static class methods (what we learned some time ago). This is one of the reasons why we just can’t use arrays to simulate classes.

Defining Your: “Class > Method > Non-Static Instance Method”

Your class definition should contain your variables in your class. They should describe the properties of the object.

You should create your Class in a separate “.cs” file as part of your existing project. You can do this by going to the ‘Solution Explorer’ and selecting the ‘Project’ then RMB ‘Add > Class’. Complete the dialog and click ‘Add’.

public class Vehicle// Use PascalCasing to name your class

{

public string model;// Name of model

public string manufacture;// Name of manufacture

public int numOfDoors;// The number of doors on vehicle

public int numOfWheels;// The numbe of wheels on vehicle

}

Creating Objects from Your Class (Instantiation)

Now that you have defined your class you need to be able to create some objects to work with. This process of creating objects from classes is called instantiation. The following code below creates a car of class vehicle:

Vehicle myCar = new Vehicle ();

The first part of the line declares a variable myCar of type Vehicle (yes a class is a type, just like when you declared a variable like ‘myInt’ as an integer). The “new Vehicle ()” part creates a specific object of type Vehicle and store the location into the variable myCar.

In C# terms, you say that myCar is an object of class Vehicle. You also say that myCar is an instance of Vehicle. In this context, instance means “one of”. You can also use the word instance as a verb, as in instantiating Vehicle. That’s what ‘new’ does.

Let’s create another instance of the Vehicle class for discussion purposes:

Vehicle yourCar = new Vehicle ();

Now we have two objects of class Vehicle. One is called ‘myCar’ and the other is called ‘yourCar’. It is important to realize that each object has its own set of members. In other words ‘myCar’ and ‘yourCar’ both contain unique members for the variables defined in the Vehicle class. You might assign values tosome of these variables as follows:

myCar.numOfDoors = 2;

yourCar.numOfDoors = 4;

myCar.manufacture = “BMW”;

yourCar.manufacture = “Ford”;

If you needed to access some of this information from your program, you may write the following:

Console.WriteLine(”My Car is a {0}”, myCar.manufacture)

Console.WriteLine(“My Car has {0} doors”, myCar.numOfDoors)

Console.WriteLine(”Your Car is a {0}”, yourCar.manufacture)

Console.WriteLine(“Your Car has {0} doors”, yourCar.numOfDoors)

The true power of OOP is that you can create (instantiate) an endless number of objects (vehicles in this example) using a single class definition. You only need to define the vehicle once!

Technical Details of Class Creation

TBD

PART 12: Advanced Topic - Instantiating Classes and Working with Objects (Ch 4)

Constructors

  • Special types of methods that are used to create objects
  • They create a new object by copying (or instantiating) an existing class. The class is used as a template to create a new object.

i.e. – student class: a new object for each students is created

Accessors

  • Allows you to read the current state of (data/attributes) of an object
  • They are referred to as ‘getters’
  • A standard naming convention is to add “Get” onto the front of the instance variable

Mutators

  • Allows you to change the current value (data/attributes) of an object
  • They are referred to as ‘setters’
  • A standard naming convention is to add “Set” onto the front of the instance variable

Properties: STUFF THAT DOES BOTH

  • TBD
  • TBD

YOUR CHAPTER 4Addendum NOTES

In this space write any important notes that you have learned from answering the chapter exercises, writing the programming exercises, form the Quick Review, or from any class discussions.

Page 1 of 6