Object oriented programming (Introduction) - Article 1
Hi Folks,
Let's start off on OOP and C++. This is an
introductory article for the bigger things coming in the future weeks.
What is
OOP or Object oriented programming?
OOP stands for object oriented programming. C#,
C++ and Java are examples of object oriented languages, where as C, Pascal are
examples of procedural languages.
Modularity, extensibility, reusability and
maintainability are some of the advantages of OO languages. In OOP, the
software programs are organized as OBJECTs.
All the entities in OOP are modelled as objects.
Suppose a problem is given, in procedural
langauge like C, the problem is divided into functions (actions on data) and
data are declared, whereas in C++, the problem is modelled as objects. For
example, a problem on car in C will look like,
float brake;
float acceleration;
main() {
accelerate_the_car();
if(obstacle
== true)
apply_brake();
}
void accelerate_the_car() {
}
void apply_brake() {
}
The same in C++ would look like:
class car {
float
brake;
float
acceleration;
public:
void
accelerate_the_car();
void
apply_brake();
};
main() {
car
ford;
ford.accelerate_the_car();
if(obstacle
== true)
ford.apply_brake();
}
Don't worry, I am going to explain the things
in detail.
Classes
& Objects
The dictionary meaning of object is as below:
Object - a material thing that can be seen and
touched.
Simply put, objects have phyical existance. Imagine
all the real world objects, the laptop, mobile or PC you have been using to
read this blog, the car you drive, books you read, etc. Let us take example of
car. All cars have properties like
engine, brakes, accelerator, windows, etc. All cars exhibit behaviours - drive, accelerate, apply_brake,
close_windows, etc.
A small
exercise: Think of objects around
you and list out their properties and behaviours.
From above, it's clear that all objects have two
characteristics: properties and behaviour. As we are concerned about
software and C++, let's map these. The properties are nothing but variables or
data and behaviours are nothing but methods of functions acting on data.
In C++, variables and corresponding functions
are bundled into objects.
So then what is a class and what is the
difference between class and object? A class is nothing but CLASSification of objects. i.e., a
group of related objects form a class. A class provides blue print for the
object and does not have physical existence, where as we have seen that objects
have physical existance.
Let me give a real world example. Ford company
manufactures cars. Suppose they plan to come up with new model. They design the
blue print of the car, i.e., how the car should look like, type of engine,
petrol or diesel, etc, etc, etc. The same goes to the factory and using this
blue print, cars are manufactured. So, this blue print forms the class for cars
that are manufactured.
In C++, objects are created using the class
type. In the example program shown, class car is the blue print. In main(), we made an object ford.
Comments
Post a Comment