What is OOP

Object Oriented Programming: Part 1

Think of a generic person, place, or thing; any noun if you would. Think of all the details pertaining to this 'object'. What does it look like? Does it have name? What does it do? Those questions and more can be translated into code.

The best analogy I have seen to visualize the software design structure known as OOP, is using a car as an example. A car is a class.  A car has a model name, color, fuel type, capacities, etc. These are what would be called class instance variables.  The car also moves forward with the gear in position and the gas pedal pressed. It also can go in reverse and stop when the brakes are pressed. The actions of the car can be described with class methods.

Here's a an example of what I just described using C++:

#include <iostream>
using namespace std;

class Car
{
    private:
            string model;
            string color;
            string fuel;
            double fuelCapacity;

    public:
            Car( )
            void forwardMove( );
            void reverseMove( );
};

 In C++ the class Car would be saved as a header file (carType.h). It just contains the description. The second half to it would be an implementation file (carType.cpp). The implementation file defines the methods further.

Now, think of other things you can describe in real life, and translate it like I did above. You can start to understand how we can translate our real world into a digital language machines can understand and eventually utilize.

Here's my first book recommendation on the subject of OOP.
I'll include an Amazon link below.


Can be bought from Amazon.com

Comments

  1. Huuuhhhhh! (Gasp!) I saw that "using namespace std;" and freaked out! When I started taking more advance C++ courses, the professors all start yelling at you to stop using it. My thoughts were, "Why did you teach me that from start?" I've never read the recommended book, but it has high reviews on Amazon. My only concern would be that it was published in 1999.

    ReplyDelete
    Replies
    1. Using a "using directive" in a header file can result in name collisions which can result in you unknowingly calling functions that you didn't intend to call. Better to use the fully qualified names in headers (ex: std::cout) and implementation files.

      For example, if you wrote defined a function called swap() or sort() in your program and called it while "using namespace std", it would not call your function, but would call the std::swap and std::sort functions from the c++ standard library rather than the ones you defined.

      There would be no error to indicate this problem.

      Delete
    2. I did commit some coding blasphemy there didn't I lol. I apologize, I should have not shown an example with bad practice. I am still learning the ways.

      This just gave me an idea for a blog topic.

      Delete

Post a Comment

Popular posts from this blog

Using Flickr

The Workstation: Keyboard