What is object-oriented programming(OOP)?
In the real world, we can classify our surroundings as classes and objects. Consider an example of vehicles: here the vehicle is class and cars, motorcycles, bullock carts, etc are instances of class vehicles. Object-Oriented programming is a programming paradigm that helps us to develop programs for our real-world problems using classes and their objects.
Classes
Classes are defined with three characteristics:
Attributes
Attributes are the data elements representing the quality or the state of objects of the class.
class Vehicle{ string companyName; string vehicleModel; string registrationNumber; string ownerName; string ownerEmailAddress; long long int ownerContactNumber; }
Here
companyName
,vehicleModel
,registrationNumber
,ownerName
,ownerEmailAddress
andownerContactNumber
are attributes/properties of objects of the classVehicle
.Constructors
Constructors are used in defining the above-mentioned attributes of the class.
Vehicle(string companyname, string vehiclemodel, string regnumber, string ownername, string owneremailaddress, long long int ownercontactnumber){ companyName = companyname; vehicleModel = vehiclemodel; registrationNumber = regnumber; ownerName = ownername; ownerEmailAddress = owneremailaddress; ownerContactNumber = ownercontactnumber; }
Methods
Methods are functions that are applied to objects of the class
void getInfo(){ cout << "Company's Name: " << companyName << endl; cout << "Vehicle's Model: " << vehicleModel << endl; cout << "Registration Number: " << registrationNumber << endl; cout << "Owner's Name: " << ownerName << endl; cout << "Owner's Email Address: " << ownerEmailAddress << endl; cout << "Owner's Contact Number: " << ownerContactAddress << endl; }
Objects
Creating objects of a class
Objects are the instances of any class. Continuing with our example, an object of the class
Vehicle
is created as show below in the code.int main(){ Vehicle obj("Maruti Suzuki", "Alto LXI", "MH29 BC XXXX","Ram Prakash", "ramprakash1223@xyz.com", 98 987 XXXXX); }
Here
obj
is the object of classVehicle
, with values of attributes mentioned above.Note that: The attribute values are written in the same order as we have defined in the constructor earlier.
Calling methods of the class for predefined objects
We can call and implement the methods that are defined earlier in our class, in a way similar to calling functions.
The syntax is
object-name.method();
int main(){ Vehicle obj("Maruti Suzuki", "Alto LXI", "MH29 BC XXXX","Ram Prakash","ramprakash1223@xyz.com", 98 987 XXXXX); obj.getInfo(); }
The method
getInfo
displays all the attribute values of the object.
Summary
In this article, I have attempted to explain you:
What is object-oriented programming?
Application of OOPs in real-world problems.
What are classes and objects?
How to define classes, their attributes, constructors, methods and objects, with examples.
Though I have explained OOPs using C++ language, the concepts remain the same for any other language which supports OOPS, the only difference is that the syntax changes.
Future article plans
Continuing with OOPs, I will post articles related to the topics: Encapsulation, Inheritance, Polymorphism, etc which are essential in OOPS, so stay tuned!