home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


Steps

First of all, you will take a look at a “normal” procedural solution to the problem. More than one way to lay out such a program exists, but the first issue is that the main program contains the variables first, second, and tested. These variables are passed by value to and from the functions in which their copies are manipulated. The variables belong to the main program and not the functions. Control of the entire program is orchestrated from the main program and control is maintained there at all times.

1.  The main program asks the function GetLetter() to return a letter. That letter is then placed into the variable first, which exists only within main (see Figure 5.1):
first = GetLetter();


Figure 5.1  main calls GetLetter, which returns a value to first.

2.  The same thing happens to obtain a value for second (see Figure 5.2):
second = GetLetter();
3.  In order to determine the relationship between first and second, they are handed to the function Equal() and the outcome of that comparison is returned to the main program where it is stored in the variable tested (see Figure 5.3):
tested = Equal(first,second);
4.  Finally, the value contained in tested is handed to the function Result() where it is displayed to the screen (see Figure 5.4):
Result(tested);

Now compare the “normal” solution with the object-oriented approach. The difference is subtle but very important.


Figure 5.2  main calls GetLetter again, which returns a value to second.


Figure 5.3  The function Equal is called from main and the variables first and second are copied into the function.


Figure 5.4  The function Result is called from main and the variable tested is copied into the function.

1.  No variables are stored in the main program. From within the main program, control is given to the pilot object and its GetLetter member function is called into life. The user input of two letters is stored internally within the pilot object. After the pilot object has done its work, it returns control back to the main program. No variables are exchanged as in “normal” programming (see Figure 5.5):
pilot.GetLetter();


Figure 5.5  Control is given to the pilot object by main, and the GetLetter member function is activated.

2.  Control is then passed again to the pilot object where the Equal member function acts upon the variables stored within itself. After the actions of Equal are completed, control is passed back to main (see Figure 5.6):
pilot.Equal();


Figure 5.6  Control is given to the pilot object by main, and the Equal member function is activated.

3.  Again, control is passed to the pilot object where the Result member function acts upon the variables stored within the object. When the actions of Result are completed, control is passed back to main (see Figure 5.7):
pilot.Result();


Figure 5.7  Control is given to the pilot object by main, and the Result member function is activated.

How It Works

In the procedural model, variables are stored within the main body of the program and passed to the functions. The functions are tools that are called to act upon the data. Control always remains with the main program and the data and member functions are distinctly separate items. Here is the procedural program in full:

// None OOP version
#include <iostream.h>

bool Equal(char alpha, char beta);
char GetLetter(void);
void Result(bool b);

main()
{
    char first;
    char second;
    bool tested;

    first = GetLetter();
    second = GetLetter();
    tested = Equal(first,second);
    Result(tested);
    return(0);
}

bool Equal(char alpha, char beta)
{
    if (alpha == beta)
        return(true);
    else
        return(false);
}

char GetLetter(void)
{
    char letter;

    cout << “Enter the letter : “;
    cin >> letter;
    return(letter);
}

void Result(bool b)
{
    if (b == true)
        cout << “Letters are the same”;
    else
        cout << “Letters are not the same”;
    cout << endl << endl;
}

In the object-oriented model, no variables are stored within the main program. Objects have their own variables and member functions that act upon those variables specifically. An object is a mini-program in its own right and the main program passes control to it. After control is given to the object, it acts upon its own internal variables using its own member functions. Here is the program:

// OOP version
#include <iostream.h>


class LetterTest {
                private:
                    char alpha;
                    char beta;
                    bool gamma;

                public:
                    void Equal(void);
                    void GetLetter(void);
                    void Result(void);
                };

void LetterTest::Equal(void)
{
    if (alpha == beta)
        gamma = true;
    else
        gamma = false;
}

void LetterTest::GetLetter(void)
{
    cout << “Enter the first letter : “;
    cin >> alpha;
    cout << “Enter the second letter : “;
    cin >> beta;
}

void LetterTest::Result(void)
{
    if (gamma == true)
        cout << “Letters are the same”;
    else
        cout << “Letters are not the same”;
    cout << endl << endl;
}

main()
{
    LetterTest pilot;

    pilot.GetLetter();
    pilot.Equal();
    pilot.Result();
    return(0);
}

Comments

There are no hard and fast rules about when you should use OOP and “normal” programming styles. You can achieve the same user interaction with either. However, OOP is by far the more powerful of the two styles, especially in larger, more complicated programs. For example, writing GUI programs is a major undertaking in traditional linear programming, but is much simpler with OOP. The OOP feature of inheritance makes the life of the programmer far easier and rapid application development (RAD) is achievable. Object-oriented programming is as much a way of thinking as a programming style. After you get into that mindset, you might wonder how you ever lived without OOP.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.