Object Oriented Programming (OOP) involves programming using objects. An Object is made from a Class which defines the attributes of the object and the behaviors. Think about it like what is it about or with adjectives (attributes) and what it can do, or verbs, (behaviors).
As an example let’s consider a Class called Student.
A student will have various attributes such as:
- name (first, middle, last, suffix)
- student number
- major
- expected graduation date
- advisor
- GPA
Notice that some of these values would be strings, others numbers, and others might be another class (advisor). An class attribute can be any type of legal data type.
They will have several behaviors as well. These are things that they can do, or can be done to them.
- print full name
- print name – last, first
- register for classes
- graduate
These behaviors would be put into a method – which is the name of a function when it’s inside of a class. As you can see, they may need to do something like combining some attributes into a larger value (print full name) or they may simply return a value, or do a combination. They may even return no value, but perform some action (register for class).
Of course, there can be a lot more attributes and behaviors for them. This is a real life example of a Class, and we can physically see examples of students all the time. Usually in a class sitting next to us.
Let’s look at a simple, but slightly more abstract Class that will be easier to work with. Let’s look at a class called Circle. It would have an attribute of radius for now, and behaviors such as the ability to get the diameter, and the area.
In a class, the attributes will be stored as an internal variable, with a data type, and the behaviors will be a function. Except, when they are in a class, a function is called a method.
Additionally, there are a special sub-set of methods we’ll look at called a constructor. These are used to initially set the object up, or “construct” it. We’ll look at those in a minute.
Defining a Class
To create a Class definition, we first need to define the class. This normally happens in it’s own file.
class Circle
{
};
The keyword class is used to help us define the class. Then a class name is choose. Standard naming rules apply to a class. Note however, that the class name normally starts with a capital letter.
Everything dealing with the class will be inside the braces.
Inside the class we’re going to define everything at public for now. We’ll change that later when we talk about security and access modifiers. For now, we’ll just add public: to inside of our braces.
class Circle
{
public:
};
(Data) Attributes
The attributes are just internal variables that describe our class, making it what it is.
class Circle
{
public:
double radius;
};
We can have as many attributes as we need, and they can be the same, or different data types, including other objects if need be. Anything defined here, can be used anywhere within the class.
Constructors
When we create an object from a class a constructor is called to build it. Inside our constructor we define initial values, and set the object up so it can be used.
class Circle
{
public:
double radius;
Circle()
{
radius = 1;
}
Circle(double startingRadius)
{
radius = startingRadius;
}
};
There are a couple of things to notice about our constructors:
- Notice in our example the constructor has the same name (including capitalization) of our class. This is a requirement.
- Notice how we don’t have a return type. This is because we are returning the data type.
- Also notice that we overloaded our constructor. One for if we are passing a starting value, one without. This way, it still can be constructed with a default value. When ever a class variable isn’t set, we need to make sure that it still gets an initial value.
- Inside our constructor, notice that a variable is assigned a value – this is the class variable that is defined up above in the class variable section.
Other Methods
After our constructors, we’ll define our other methods. In our case, we’ll start with our getArea method.
class Circle
{
public:
double radius;
Circle()
{
radius = 1;
}
Circle(double startingRadius)
{
radius = startingRadius;
}
double getArea() {
return radius * radius * 3.141592;
}
};
Notice that we don’t pass a value into our getArea method like we have previously. This is because it is using the predefined radius variable for our class.
Defining Classes for Objects was originally found on Access 2 Learn
3 Comments
Comments are closed.