Object-Oriented Programming (OOP) is a programming model in which everything revolves around objects. Now, the next obvious question that comes to mind is, What is an Object?
What is an Object? #
An object represents a real-world entity.
When we write a program or an application, our purpose is to solve real-life problems, and in real life, we communicate or interact with different entities. These real world entities are represented as objects in Object Oriented Programming.
Let’s understand this with an example:
Suppose we are trying to create a hospital management system. In this application, we will interact with different entities like a doctor, a patient, or even more abstract entities like a Blood Test or an Appointment. All these real-world entities are essentially objects.
For example, there will be several Doctor Objects. Where each doctor object represents a specific doctor in the hospital. Similarly, there will be several patient objects, and many other objects too, like appointment objects.
When a patient books an appointment with a doctor, that appointment itself will also be an object. Just like the doctor and patient are objects, the appointment is an object too. In the world of object-oriented programming, everything is an object.
How is an Object different from another object in OOP? #
Objects are different because they can hold their own data (properties or attributes) and may even behave differently depending on that data. Each object of a common type will have the same data members, but the values of these data members will differ.
Objects are organized based on their types. For example, consider three doctors, like Dr. Sam, Dr. John, and Dr. Mathew. Each doctor will have common properties like ID, specialization, and experience, and they may have similar methods like Diagnose Patient and Prescribe Treatment.
Similarly, there are multiple patient objects like Smriti, Sanjay, and Sumit. All patient objects will have common data members like patient_name
, age
, and disease
. However, the values of these data members will differ for each patient object.
Important Point:
- A doctor object is very different from a patient object, as both have different sets of data members or properties.
- However, all doctor objects share the same data members and properties. The only difference is the values of these properties for each object.
- Similarly, all patient objects share the same set of data members, but the values of each data member differ.
This shows that all doctor objects are of the same type, and all patient objects are of another type. It seems as though someone has defined a blueprint specifying that all doctor objects will have these data members and functions associated with them, even though each doctor object may have different values.
Similarly, someone has defined a blueprint for a Patient class. All patient objects have the same set of data members as defined in that blueprint.
Essentially, a type is defined as a blueprint, and objects are created from that type. This type is also known as a class, which is another important concept in object-oriented programming.
What is a Class? #
Object-Oriented Programming organizes code into reusable blueprints called classes. Each class defines the structure and behavior of objects created from that class.
When you create an object from a class, it is known as an instance or object.
All objects of the same class share similar attributes and methods but have different values for those attributes.
Let’s delve into this in more detail. Consider that we are building a Hospital Management System. Although it will include many entities, for simplicity, let’s assume there are three main entities: Patients, Doctors, and Appointments.
A Patient object might have:
- Attributes: Name, Age, Medical History, Patient ID, Disease
- Methods: Schedule appointment, Get treatment
A Doctor object might have:
- Attributes: Name, Specialization, Doctor ID, Experience
- Methods: Diagnose patient, Prescribe treatment, Schedule consultation
An Appointment object might have:
- Attributes: Appointment ID, Date, Time, Patient, Doctor
- Methods: Schedule, Reschedule, Cancel
What we have specified above is the blueprint of objects of types Patient, Doctor, and Appointment. By the term blueprint, we mean classes. Although the syntax here is not how classes are formally defined in code, this explanation serves to illustrate what is typically specified in a class definition.
Once these classes are defined, we can create objects of type Doctor, Patient, and Appointment.
These objects represent different entities within the hospital management system, but they all share the fundamental idea of having attributes and methods as specified in their respective classes.
Objects of the same type share the same attributes and methods but can have different values for those attributes.
Let’s take an example:
There are separate objects for patients Rahul, John, and Mathew. Each of these patient objects will have similar attributes but different values for those attributes.
For example, there can be patient objects like these:
Patient Object 1:
- Attributes:
name = "Rahul Singh"
,disease = "Knee Issue"
,patientID = 101
- Method:
scheduleAppointment()
- Attributes:
Patient Object 2:
- Attributes:
name = "John Smith"
,disease = "Stomach Pain"
,patientID = 102
- Method:
scheduleAppointment()
- Attributes:
Patient Object 3:
- Attributes:
name = "Mathew Singh"
,disease = "Broken Bones"
,patientID = 103
- Method:
scheduleAppointment()
- Attributes:
All patients named “Rahul Singh,” “John Smith,” and “Mathew Singh” are Patient objects. They have the same attributes like name
, age
, and patientID
, and the same method scheduleAppointment()
. However, the values of their attributes are different. These objects belong to the same type or class, which is the Patient
class.
The same concept applies to Doctor objects:
Doctor Object 1:
- Attributes:
name = "Dr. Kanika"
,specialization = "Cardiologist"
,doctorID = 201
- Method:
diagnosePatient()
- Attributes:
Doctor Object 2:
- Attributes:
name = "Dr. Sushmita"
,specialization = "Dermatologist"
,doctorID = 202
- Method:
diagnosePatient()
- Attributes:
Doctor Object 3:
- Attributes:
name = "Dr. 3
,specialization = "Othro"
,doctorID = 203
- Method:
diagnosePatient()
- Attributes:
All these doctors have the same structure—same attributes and methods—but different data. They share the same attributes like name
, specialization
, and doctorID
, and the same method diagnosePatient()
. However, the values of their attributes are different. These objects belong to the same type or class, which is the Doctor
class.
Thus, a class is like a blueprint that defines the structure of an object. It specifies the attributes and methods that every object of that type should have. This type is also known as a class in C++.
For example, in the above example, the reason patients “Rahul Singh” and “John Smith” are considered Patient objects is that they are instances of a class called Patient
. The Patient class defines that every instance or object of that class should have attributes like name
, age
, and patientID
, along with methods like scheduleAppointment()
. All patient objects will share these attributes and methods, but the values for each attribute will differ between objects.
Similarly, doctors “Dr. Kanika” and “Dr. Sushmita” are considered Doctor objects because they are instances of a class called Doctor
. The Doctor class defines that every doctor should have attributes like name
, specialization
, and doctorID
, and methods like diagnosePatient()
.
In object-oriented programming, everything revolves around objects. This approach makes the code more modular and maintainable, which is essential for handling large and complex codebases.
The concepts of objects and classes are foundational building blocks of object-oriented programming. In the next tutorial, we will learn how to define a class in C++ and create objects from that class.
Summary #
In summary, objects in Object-Oriented Programming represent real-world entities. They have a set of attributes (data members) and behaviors (methods).
Different objects of the same class share the same structure, meaning they have the same set of data members but different values for those data members. The structure of an object is defined by its class. Therefore, a class is like a blueprint for creating objects, and we can create many objects (instances) from that blueprint, with each object having different values for its attributes.