#include #include /* AN OVERVIEW OF OBJECT ORIENTED PROGRAMMING Chuck Liang Every program deals with data. A program that adds time deals with minute and second values, an animal guessing game deals with animal descriptions, and a program that draws stickfigures deals with graphical coordinates. "Objects" are collections of variables and functions that are grouped together around the data they're supposed to represent and manipulate. For example, if the data in question is a bank account, then the object may contain variables for the account balance, name of the owner, and secret pass code. It will also contain functions to perform tasks such as withdraw, deposit, and balance inquiry. In order to create objects, we must first define a "class", which is a blueprint for creating objects. Many classes in the C++ language has the following common syntactic structure: class className { private: // Declaration of variables and functions that you do not want to be // visible outside of the class // Most variables will belong in the private section public: className(...) // the designated "constructor" function or method { // code to initialize certain variables } className() {} // alternate constructor // Declaration of variables and functions that you want visible outside // of the class. // Most functions (also called "methods") belong in the public section }; // end className The "constructor" function of the class typically assigns intial or default values to the variables of the class. Note that the syntax of the constructor function does not even return "void"! Also, the name of the constructor function must be the same as the name of the class. For example, a bank account class could have the following definition: */ class account { private: double balance; // current balance of the account string name; // name of the person owning the account int pin; // secret code to access the account public: // the constructor function assigns initial values to variables: account(double b0, string n0, int p0) // standard constructor { balance = b0; // sets initial balance name = n0; // sets name of owner of account pin = p0; // sets secret passcode } account() {} // alternate constructor // functions (or "methods") representing operations of the account: void withdraw(double ammount) { balance = balance - ammount; } void deposit(double ammount) { balance = balance + ammount; } double inquiry() // balance inquiry - note, value returned { return balance; } }; // end class account /* Once again, a class declaration in the format above is not an "object": it is a blueprint for creating objects. An object is also called an "instance" of the class. To use a class (inside a function), you create instances of the class with declarations of the form: className myinstance(...); This declares the variable "myinstance" to represent an object which contains all the components (variables and functions) declared in the class definition. The parameters (or arguments) in parentheses are the required parameters of the designated "constructor" method of the class, which is used to initialize the data inside the class. You can only access public components of the object in functions that are not defined inside the class. For the account class above, the following code illustrates how objects are created and used: */ int main() { // create account a1 with balance 1000, name "good student", and pin 1234: account a1(1000.0, "good student", 1234); account a2(2000.0, "evil professor", 4321); // another account object a1.deposit(500.0); // calls deposit function belonging to account a1: a2.withdraw(19.95); // calls withdraw function belonging to account a2: // calls inquiry function of both accounts, and display returned values: cout << "a1's balance is " << a1.inquiry() << "\n"; cout << "a2's balance is " << a2.inquiry() << "\n"; return 0; } // end main /* The notation "A.B" accesses component B of object A, if B is declared under "public:" in the class definition. There is much in the above example that inherits from what you've learned so far. For example, deposit and withdraw both return "void", and so they are called as statements. inquiry returns a value, and so it must be called as an expression. But there is also some new things here: We've learned that variables declared inside a function (be it a parameter variable inside the ()'s, or a normal variable inside the {}'s) are only local to the function. This remains true. However, a function inside a class also SHARE variables with other functions class. In the account class, note that the "balance" variable is shared by both desposit, withdraw and inquiry. It is important that deposit and withdraw share the same variable, otherwise they won't be modifying the same account balance! ( where did the money go? :-o ) The balance variable is shared because it's is declared in the same class that deposit and withdraw are declared in. It is important you understand this: consider what would happen if we defined deposit as follows: void deposit(double ammount) { double balance; balance = balance + ammount; } In this case, a NEW balance variable is created that is local to the deposit function every time the function is called. Changing this local variable will therefore NOT affect the balance variable declared in the class, which is supposed to represent the true balance. In other word, the real balance of the account will NOT be changed by this function. ************ Yup, this is what happened to that bank where you deposit money into your account but it never gets added to your balance!!! So make sure you understand what I just said. Otherwise only this bank will hire you :-) ************ In general, when definining a function, you must first ask the question: what kind of information will the function need? Some of the information is already contained in the class, but additional information may have to be passed in as parameters. The withdraw function requires two pieces of information: the current balance and the ammount to be withdrawn. The balance is already inside the class, but the ammount is not known until the function is called. Thus the ammount must be a parameter. But balance already exists in the class and so there is no need to redeclare it. The inquiry method only needs to know what the balance is, so it does not require any parameter; In addition to creating objects in the manner demonstrated above, we sometimes want to declare a variable, and then instantiate it later. This is accomplished by the following: account a3; // ... a3 = account(4000.0,"rich guy",9876); In either case, when creating an instance of a class we must pass the contructor function all the information it requires, in the manner that the constructor function was defined. I haven't explained fully what "public" and "private" mean yet. That'll come later. */