We are not stepping into the world of OOP. Somehow, every modern programming language supports some form of OOP features.
Let’s first get through the basics of OOP under the following sub-topics:
- What is OOP?
- Creating Classes in C++
- Creating Objects from Class
- Accessing Data Members and Member Functions
- Benefits of Object Oriented Programming
1. What is OOP?
OOP in C++, or in programming generally, stands for Object Oriented Programming. This is an approach to programming where programs are written based on classes and objects. These are the two key aspect of OOP.
An object is a representation a real object. Just look around you, or just think for a minute! Everything is basically an object: phone, keyboard, mouse, pen, computer, food, camera, banana, apple, bike, car, dog etc.
So we can say an object is made up of attributes and methods.
A class is a blueprint of an object. Basically, we can create objects from classes. In other words, an object is an instance of a class.
The figure below illustrates this:
You can also think of fruits: apple, orange, banana, etc
How does all of this relate to programing? Well, Let’s see
2. Creating Classes C++
Since we now know what classes an object are, we should be able to create them. We define a class in C++ using the class keyword. Then we provide the class name followed by curly braces. Inside, the curly braces, we then specify it’s attributes and methods.
Here’s a class that represents a circle
class Square { public: double length; double width; double calculateArea(){ return length * width; } double calculatePerimeter(){ return 2 * length * width; } };
So we created a class named Circle.
The variables length and width are the attributes of the class. They are also called data members or member variables.
The functions calculateArea() and calculatePerimeter() are known as member functions.
3. Creating Objects from Class
As mentioned before, a class is a blueprint for creating objects. Therefore, once you have a class, you can start creating objects from it.
Creating objects from a class in called instantiation. So we can, “instantiate a circle“. In order words, create an instance of a Square or object of type, Square.
Objects are instantiated same way we create Structs or declare variables. For example:
Square square1; //Declare square1 of type Square Square square2; //Declare square1 of type Square
This code instantiates two Squares, square1 and square2.
.4. Accessing Data Members & Member Functions
So if we have created an object, we should be able to access it’s attributes(data members). Or we may want to call it’s member functions.
To achieve this, we use the dot(.) operator.
#include <iostream> #include "Square.cpp" //the class is in another file using namespace std; // main function int main () { //Create a new Square, square1 Square square1; //assign values to the data members square1.length = 20.2; square1.width = 10.5; //Calculate the area and perimeter cout <<"Area is: " << square1.calculateArea()<<endl; cout <<"Perimeter is: " << square1.calculatePerimeter() <<endl; return 0; }
Output is:
Area is: 212.1 Perimeter is: 424.2
one thing I would you to see from the program is that the class is defined in another file, Square.cpp. Then it is included in the main file using the #include directive.
Also note that the data members and member functions are declared a public. This means that they can be accessed from anywhere from the program.
Now we have cover some basics of OOP. But there’s yet much to learn. So let me end this lesson by mentioning few benefits of OOP
5. Benefits of OOP
- make the program clearer and more readable
- OOP programs are faster in execution
- they help to create easier to maintain codes since classes can be reused
- less code and shorter development time