|
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
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.
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.
|
![]() |
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.
|