Computer Programming I :: Lessons :: Object-Oriented Programming
What is Object-Oriented Programming?
In programming, an object is something created by a program that has state and behavior. State is the properties that the object exhibits and behavior is what the object can do. Object-oriented programming is a way of programming that uses objects instead of the traditional sequential programming that runs line-by-line. We have been using object-oriented programming (OOP) all along in Visual Studio, but much of the implementation has been hidden.
Classes
A class defines the available properties and behaviors of an object. We have been using a class all along in Visual Studio: the Form1 class that defines what your form can do and what form components it has. You can think of a class like a blueprint for a house. You only have a single blueprint, but you can build as many houses based off that blueprint as you want. You could technically build more forms based off the Form1 class, but that hasn't been anything we've needed to do in class.
We're going to build a class to create cars. Follow the steps below to set up the project:
- Create a new project in Visual Studio called "CarMaker" and save it to your network drive.
- Add a button, three textboxes, and a listbox to the form.
- Rename the listbox "lstCars".
- Rename the textboxes "txtColor", "txtDoors", and "txtTopSpeed".
- Create a new class by going to Project>Add Class...
- Call the class "Car.cs" and hit the "Add" button.
You will see the following code in the new class:
namespace CarMaker { class Car { } }
Properties
A class has properties that are used to specify the state of an object. Properties are stored as variables in a class. We are going to add variables to our Car class so store the car's color, number of doors, top speed, and mileage.
- Add the following code in red below:
namespace CarMaker
{
class Car
{
string color;
int door;
int topSpeed;
int miles;
}
}
We now need a place to initialize the three properties of the Car class. We don't know the color, number of doors, and top speed of the car ahead of time, but we can set them when the car is first created. We can do this by creating a class constructor. We used constructors when dealing with arrays, but that constructor was the default constructor that did not take any variables as input. We are going to create a constructor that takes three variables, known as parameters as input to initialize the three properties of the class.
- Add the following code in red below:
namespace CarMaker
{
class Car
{
string color;
int door;
int topSpeed;
int miles;
public Car(string c, int d, int s)
{
color = c;
door = d;
topSpeed = s;
miles = 0;
}
}
}
We will see how to construct an object shortly, but when you do you will send three arguments to the constructor that get assigned to the parameters c, d, and s. Those parameters are then assigned to the property variables color, door, and speed to set the initial state of the object. The miles is set to 0 every time since a car will always have 0 miles when it is first created.
Creating Objects
We've created the class, or blueprint, for a car so now we need to make some cars. We will do this in the form of your CarMaker project.
- Go to Design View of your form and double-click the button.
- Add the following code in red to your form:
namespace CarMaker
{
public partial class Form1 : Form
{
Car[] myCars = new Car[100];
int currentCar = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
We are going to use the myCars array to hold all of the cars we create. It can hold 100 cars but right now it is empty. When we click the button we want to create a new car based on the values in the textboxes.
- Add the following code in red to the button click event handler:
namespace CarMaker
{
public partial class Form1 : Form
{
Car[] myCars = new Car[100];
int currentCar = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string color = txtColor.Text;
int doors = Convert.ToInt32(txtDoors.Text);
int speed = Convert.ToInt32(txtTopSpeed.Text);
myCars[currentCar] = new Car(color, doors, speed);
currentCar++;
}
}
}
The above code takes the three values from the textboxes and creates a new car from them, which is stored in the myCars array. This is great, but what if we want to see the properties of all of the cars?
Message Passing
Right now there is no way to access the properties of our cars. We can change that through a process known as message passing that allows access to public properties of a class. We don't want to make the variables public, however, since that would mean they could be changed. Instead, we will create an accessor for each variable that will allow its value to be read from outside the class.
- Add the following code in red to the Car class:
namespace CarMaker
{
class Car
{
string color;
int door;
int topSpeed;
int miles;
public Car(string c, int d, int s)
{
color = c;
door = d;
topSpeed = s;
miles = 0;
}
public string getColor()
{
return color;
}
public int getDoors()
{
return door;
}
public int getTopSpeed()
{
return topSpeed;
}
public int getMiles()
{
return miles;
}
}
}
Accessors are a special type of method, and a method provides the behavior of an object. In this case, the behavior of an accessor is simply stating the value of the object's properties.
We can use our newly-created accessors to output all of our cars to the listbox.
- Create a new button and double-click it.
- Add the following code to the new button:
for (int i=0; i < currentCar; i++) lstCars.Items.Add(myCars[i].getColor() + " " + myCars[i].getDoors() + " door with a top speed of " + myCars[i].getTopSpeed();
Methods
Methods provides the behavior for an object. An object can't do anything without methods. We are going to create a method that lets us drive our created cars.
- Add the following code in red to the Car class:
namespace CarMaker
{
class Car
{
string color;
int door;
int topSpeed;
int miles;
public Car(string c, int d, int s)
{
color = c;
door = d;
topSpeed = s;
miles = 0;
}
public string getColor()
{
return color;
}
public int getDoors()
{
return door;
}
public int getTopSpeed()
{
return topSpeed;
}
public int getMiles()
{
return miles;
}
public void drive(int distance)
{
miles += distance;
}
}
}
The method we created will increase the mileage on the car based on the distance driven. We now need to create a way to drive cars from the form. We will create a button that reads the number of miles from a textbox as well as the car index number.
- Create a new textbox called "txtMiles".
- Create a new textbox called "txtCar".
- Create a new button called "btnDrive".
- Double click on the button and add the code below:
int car = Convert.ToInt32(txtCar.Text); int miles = Convert.ToInt32(txtMiles.Text); myCars[car].drive(miles);
Whenever you click the button it will use the number from the txtCar textbox to select a car and then call the drive method for that car with the number of miles from the txtMiles textbox.