C++: Work out the method first

This entry is part 31 of 61 in the series C++

Methods of a class is just like functions in a procedural language.  In a procedural language, we just have to think of the procedures and it makes procedural language such as C to be rather straight forward.

In C++, a method is just a part of an object.  Once you have thought of a method, you still need to work out the bigger picture, that is, the object associated with this method.

Creating an object is like another layer that we need to deal with.  This is what knocks us off when we first started learning object-oriented programming.

As mentioned, designing an OOP program is like working out a sentence with the noun to be in front of a verb.

We are led to believe that we need to first have a clear concept of the object before designing the class.

I have like to suggest that just like conventional procedural programming language,  we first work the method rather then the object.

Let’s go back to our previous example.  The example calculate the number of days needed for a train to travel from one city to another.  We obviously would need a method called Days that can just do that.

Once the method is worked out, it is easy for us to see that we need a train class with 2 data members distance and speed for this class.







Series Navigation<< C++: Creating an object
C++: Another example on creating an object >>