When
I firstly heard the term of Object Oriented Programming or simply
called OOP, I had no idea what the “object” term actually means
and what it does. What I knew that there’s just script on lines,
there’s no “moving object”, or any other signs that looked like
object that I usually saw in the real world.
So,
what does “object-oriented” actually means? OOP is a programming
style that associates with the concept of ‘class’ and ‘object’,
simply meaning that we construct (declare) the object, while the
class is its instance for the object declared. For clearer, suppose
an ‘object’ were a building therefore a ‘class’ itself would
be blue-print for that building. For example, if any architect wants
to build the same building anywhere, just use that blue-print for the
building to construct.
With
the analogy above in mind, will it make sense when we see the
building ‘object’ is just a word when facing scripting? It
didn’t when I firstly learn OOP! But a long with time to go, I got
it. Here with the simple example of adding ‘time’-object may make
sense, especially for a beginner.
//instance for any object declared
//or blueprint for any building
class Time{
int hour;
int minute;
public:
Time(){}
Time(int h,int m);
Time operator+(const Time & t) const;
friend ostream & operator<<(ostream & os, const Time & t);
};
Now
we can construct (declare) any object with the instance (blue-print).
As many object we declare as we want, the characteristic and features
will be the same.
//construct the object
Time Lupy_waits{1,56}; //hour,minute;
Time Catty_waits{2,24};
//chek it
cout<<"Lupy: "<<Lupy_waits<<endl;
cout<<"Catty: "<<Catty_waits<<endl;
//add the objects
Time Total_waits = Lupy_waits + Catty_waits;
//see the result
cout<<"total time: "<<Total_waits<<endl;
The
first line of the code, Luppy_waits object is constructed by
Time instance with two arguments passed; hour and minute
values. Like stated above.
The
fifth ine, the shift-left bitwise operator ‘<<’ is used to
display the content of the object. And finally for the line:
Time Total_waits = Lupy_waits + Catty_waits;
shows
that the words Lupy_waits and Catty_waits act as if they were
objects. On the code, I deliberately take the ‘+’ overloading
operator to make clearer that the script e.g., “Lupy_waits” can
be added visually like an object.
Finally
the result is shown by ‘<<’ operator. The result would be
like:
Lupy: 1 hours, 56 minutes
Catty: 2 hours, 24 minutes
total time: 4 hours, 20 minutes
here is the complete code:
#include <iostream>
using namespace std;
class Time{
int hour;
int minute;
public:
Time(){}
Time(int h,int m);
Time operator+(const Time & t) const;
friend ostream & operator<<(ostream & os, const Time & t);
};
Time::Time(int h,int m)
{
hour = h;
minute = m;
}
Time Time::operator+(const Time & t) const
{
Time sum;
sum.minute = minute + t.minute;
sum.hour = hour + t.hour + sum.minute / 60;
sum.minute %= 60;
return sum;
}
ostream & operator<<(std::ostream & os, const Time & t)
{
os << t.hour << " hours, " << t.minute << " minutes";
return os;
}
int main()
{
Time Lupy_waits{1,56}; //hour,minute;
Time Catty_waits{2,24};
cout<<"Lupy: "<<Lupy_waits<<endl;
cout<<"Catty: "<<Catty_waits<<endl;
Time Total_waits = Lupy_waits + Catty_waits;
cout<<"total time: "<<Total_waits<<endl;
return 0;
}
That’s
it, the object term means. I hope the example above can give you a
visualization about object in code context. If you have any question
relating to the OOP concept, please ask bellow. Thanks for reading!
0 Comments
Post a Comment