RPGCode — Object Oriented Coding — Old vs. New
old
RPGC3 attempts to provide new, more intuitive, ways to structure your code. For example, observe how you might code a date entity previously:
// February 22nd, 2004 -- date index 1 dateMonth[1]! = 2 dateDay[1]! = 22 dateYear[1]! = 2004 // Show the date showDate(1) // Method to show a date method showDate(idx!) { month[1]$ = "January" month[2]$ = "February" month[3]$ = "March" month[4]$ = "April" month[5]$ = "May" month[6]$ = "June" month[7]$ = "July" month[8]$ = "August" month[9]$ = "September" month[10]$ = "October" month[11]$ = "November" month[12]$ = "December" saveScreen() clear() pixelText(10, 7, month[dateMonth[idx!]!]$) pixelText(14 + getTextWidth(month[dateMonth[idx!]!]$), 7, _ CastLit(dateDay[idx!]!) + ", " + CastLit(dateYear[idx!]!)) wait() restoreScreen() }
While this approach may work, it has a number of flaws. First, and most serious, the names of the globals you use the set the date may not be easy to remember - especially to programmers other than yourself who are trying to use your code. While you could create functions to set these vars, the resulting overhead would be unacceptable. And if you decided to change how a date was implemented at a later time, changing the globals used, for example, you would have to manually modify the code everywhere a date was used. What is needed is some way to keep the data and the functions surrounding it tightly knit.
new
Classes provide a way in which to define data and functions that operate on the data in one location. When you create a class, you are creating a new data type that can be created like any other type of variable (namely numerical and literal as RPGCode goes). Using classes, we could re-write the date entity as follows:
// A date class CDate { // Public visibility public: // Constructor method CDate(month!, day!, year!) // Display the date method display() // Private visibility private: m_month! // Month m_day! // Day m_year! // Year } // CDate - constructor method CDate::CDate(month!, day!, year!) { // Copy over to members m_month! = month! m_day! = day! m_year! = year! } // Display the date method CDate::display() { month[1]$ = "January" month[2]$ = "February" month[3]$ = "March" month[4]$ = "April" month[5]$ = "May" month[6]$ = "June" month[7]$ = "July" month[8]$ = "August" month[9]$ = "September" month[10]$ = "October" month[11]$ = "November" month[12]$ = "December" saveScreen() clear() pixelText(10, 7, month[m_month!]$) pixelText(14 + getTextWidth(month[m_month!]$), 7, _ CastLit(m_day!) + ", " + CastLit(m_year!)) wait() restoreScreen() }
The code you see above may seem complicated, but really it's not. The class CDate line defines a beginning a class - CDate. Within a class we have two visibilities - public and private. Using public: and private: lines changes the current visibility. In the public visibility you should place the declarations of methods (and optionally their code, but this is not recommended for complicated classes), whereas the private visibility houses variables and functions that cannot be accessed from outside the class.