Classlec: Object Oriented Programming Lecture Note: OOP is the hardest, yet most useful part of the C++ language. In this lecture I will take it one step at a time so feel free to go slowly and read sections multiple times. Part 1: Basic Concepts (or What is an Object?) An object is an enhanced structure. It is a structure that contains not only variables, but functions. Let me elaborate: Lets say you wanted to create a program to handle employee records. Instead of putting variables together in a struct (name, #, salary, etc) wouldn't it be convienient to be able to group functions along with them so you could easily create a new employee, alter their pay, etc? That's where OOP comes in. One quick note before we move on: you will start seeing two words pop up a lot. These are: declaration and definition. Declaration: a statement of a variable that does not cause memmory to be allocated. Examples include: struct declarations (typedef struct { ...} myStruct;) Definition: a statement of a variable that causes memmory to be allocated. Examples include: short myShort; myStruct theStruct; Part 2: Defining Objects: (or Intro to Classes) In order to use objects you must declare them. For example with structs, before you use the struct you must do the following: typedef struct { vars vars morevars } myStruct; To use objects you must declare their class. The format goes like this (note: things is <> are optional and may be ignored for now... we'll cover them later). class Classname < : Superclass> { // Data Members: private: short onevar; char cool; long whee; // Member Functions: public: Classname(); ~Classname(); void ThingToDo( short thing ); }; If you're thinking "What the heck is all that?", relax, we'll go through it step by step. class Classname { This is the opening line of your class declaration. This is fairly standard, the class keyword followed by the class name. Note: typedef is built-in with classes. Never use it. //Data Members: private: short onevar; etc... Alright, here's where we put our variables. The main thing that concernes us here is the keyword private:. private: and public: are access methods and will be dealt with later. // Member Functions: public: Classname(); ~Classname(); void ThingToDo( short thing ); }; These are functions that will be grouped with the data members. Classname() and ~Classname() are the constructor and destructor. We'll cover those next. ThingToDo() is the prototype for a function you will write later. Part 3: Writing the Member Functions When you declared your class you put down the prototypes for several functions. Two of these have special significance and are required to be in every class. These are Classname() and ~Classname() commonly known as the constructor and destructor. More on these in the next section. All other member functions are declared by you. The first line of any member function looks like this: ::() Here's an example using your ThingToDo method: void Classname::ThingToDo( short thing) { } Part 4: The Life Cycle of an Object Ok, now that you've declared your class, let's create an object. There are two ways of creating an object. Automatically, and manually. We'll talk about the advantages and disadvantages of each method in a sec. But first, how to do it: Automatic creation: main() { Classname myObject; dootherstuff } Bingo! It's done! All you have to do to automatically create an object is to define it. Remember, your class is still a type even though you didn't use typedef. Now: manual creation. main() { Classname * myObject; myObject = new Classname; dootherstuff ........ delete myObject; } We'll come back to the differences in methods in a sec. Now, when an object is created it's constructor is called. The constructor is the function in the class that has the same name as the class. When the object is deleted it's destructor is called. The destructor is the function that has a ~ followed by the class name. EVERY CLASS MUST HAVE A CONSTRUCTOR AND DESTRUCTOR!!!! Constructors are good places to put initialization procedures, while destructors are good places for deinit procedures. However, you CAN have an empty constructor and destructor, you just have to have one. Automatic creation vs. Manual Ok, this deals with the scope of an object. Scope defines whether an object will be global or local. The scope rules for variables apply for objects as well. Therefore, if you want your object to persist throughout the duration of the program, use pointers and manual creation. 'nuf said. Part 5: Access rights. Remember back in the beginnging when we saw: class Thing { private: ... pubilc: ... }? private: and public: are keywords that denote the access rights of all members below them. the 4 access levels are: public: private: protected: friends: For now we will just worry about the first 2. private: means that any variables can only be changed by a function that is the member in the same class as the variable. Private functions may only be executed by other functions within the class. public: means that the variable-funtion is editable-executable by anyone. Part 6: Running class funtions and editing class variables. Let us assume you have the following class, which looks like this: class Thingy { public: int myVar; void kewl( void ); }; assuming you created your object automatically (and named it obj) you could use the dot operator to change myVar: obj.myVar = 4; here is how you would execute kews: obj.kewl(); if you used manual creation you would have to use the -> operator in place of . Part 8: This when you want to refer to a member of the current object ie: an object''s constructor wants to modify something in that object, you use the this keyword. Example: this.myVar = 4; Sets the myVar variable on the current object to 4. Part 9: An example to tie it all together. Here is a sample program that creates two emplyee objects. //Example.cpp copyright 1996 GeekSoft Inc. #include #include class Employee { //Data Members... private: //can only be accessed through member functions char employeeName[ ]; long employeeID; float employeeSalary; //Member Funtions... public: //can be run by the world. Employee( char *name, long id, float slalary ); //Constructor ~Employee( void ); //Destructor void PrintEmployee( void ); //Regular function }; Employee::Employee( char*name, long id, float salary ) //code for Construct { strcpy( this->employeeName, name ); this->employeeID = id; this->employeeSalary = salary; cout << "Creating employee #" << employeeID << "\n"; } Employee::~Employee( void ) { cout << "Deleting employee #" << employeeID << "\n"; } void Employee::PrintEmployee( void ) { cout << "----\n"; cout << "Name: " << employeeName << "\n"; cout << "ID: " << employeeID << "\n"; cout << "Salary: " << employeeSlary << "\n"; cout << "----\n"; } void main( void ) { Employee employee1( "Mike Carns", 1, 200000.0 ); //args to constructor Employee *employee2; employee2 = new Employee( "Roseann Krane", 2, 1000000.0 ); employee1.PrintEmployee(); //not a ptr, use . employee2->PrintEmployee(); //must use -> due to pointer delete employee2; } The output for the program is this: Creating Employee #1 Creating Employee #2 ---- Name: Mike Carns ID: 1 Salary: 200000.0 ---- ---- Name: Mrs. Krane ID: 2 Slary: 1000000.0 ---- Deleting Employee #2 Deleting Employee #1 Why was 2 deleted before 1? Because it was manually created and deleted whily #1 was auto created. #1 was auto deleted when main() exited. End of first chapter on classes. Be sure you read Chapter 9 in the book.