Lets first understand what is OOP
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or functions that perform operations on the data,
while object-oriented programming is about creating objects that contain both data and functions.
Object-oriented programming has several advantages over procedural programming:
OOP is faster and easier to execute
OOP provides a clear structure for the programs
OOP helps to keep the C++ code DRY "Don't Repeat Yourself",
and makes the code easier to maintain, modify and debug
OOP makes it possible to create full reusable applications with less code and shorter development time
What are Classes and Objects?
Classes and objects are the two main aspects of object-oriented programming.
Lets understand it with an example:-
If 'fruits' or 'cars' is assumed to be a class then objects can be 'mango', 'banana', 'apple' etc. for fruits class and 'volvo', 'audi' etc. for cars class.
So, a class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the variables and functions from the class.
A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects.
Creating a class and an object to a class:-
We can also create multiple objects for a single class.
Class Methods:-
Methods are functions that belongs to the class.
There are two ways to define functions that belongs to a class:
Inside class definition
Outside class definition
In the following example, we define a function inside the class, and we name it "myMethod".
Note: You access methods just like you access attributes; by creating an object of the class and using the dot syntax (.)
To define a function outside the class definition, you have to declare it inside the class
and then define it outside of the class. This is done by specifiying the name of the class,
followed the scope resolution :: operator, followed by the name of the function:
Similarly we can also pass parameters to these methods.