In this web-based review tool, four classes of users will have access to different combinations of operations. For example, authors can mark comments as complete, but reviewers can only add comments. In this section, you will implement a hierarchy of user classes, in which the base class, User, defines virtual functions for all available operations. You will use the coding of these user classes to learn many new techniques for developing object-oriented programs within the IDE.
This section should take you approximately one to two hours to complete. In it you will learn about the following IDE tasks:
If you are interested only in learning the IDE, and do not need to understand the details of the CGI application, you can skip passages titled Implementation Details.
You should have the following source files in a directory on your system. Click the link to see the file's contents. Any changes you may need to make for your environment are indicated in the files.
| Implementation Details User class member functions are defined to produce an "Unauthorized access" error message. In derived classes, these functions are overridden to perform appropriate work for the method and the class. One advantage of a hierarchical implementation of user classes is that authority levels are managed automatically by the hierarchy, instead of requiring you to perform authority checks throughout your code to determine if the current user is allowed to perform the current operation. The User class and its subclasses implement the following functions: View, ShowAdd, Add, and Close. For the User class, the functions return an error message. Three subclasses of User are defined, which override selected User class member functions. These subclasses are Reader, Reviewer, and Author. |
To begin, you need to create three source files and add them to your configuration:
For each file, follow these steps, which you have already followed for other files in Part 3 of the tutorial:
If you create all three source files before adding any code, put a space in each source file in its workbook section, so that the first time you rebuild, all three files are saved and recognized as source files. If you do not add anything to the files before you build, the build will fail because the files do not exist.
The auxiliary functions you will need for this part of the tutorial both return an IString value. The first, Unauthorized, returns an error message stating that the user does not have authority to perform the requested function. The second, Error, is used to wrap its two arguments, a title and a message, in appropriate HTML markup so that it returns a valid HTML document.
Add the following two function definitions to funcs.cpp:
#include <istring.hpp>
IString Unauthorized(IString Function) {
return Error("Unauthorized Access",
"You do not have sufficient authority to access the "
+Function
+" function.");
}
IString Error(IString Title, IString Text) {
return "<html><head><title>"
+Title
+"</title></head><body bgcolor=\"#FFFFFF\"><p><h3>"
+Title
+"</h3><p>"
+Text
+"</p></body></html>";
}
Rebuild your project. The project should build successfully even though you did not include istring.hpp as a header file for this source file. The IDE processes the istring.hpp header file because it is included by request.hpp; its class and members can therefore be accessed by any source file in the project. It is still a good practice, however, to include the necessary header files at the top of each source file that uses them, in case you later try to compile the code using a product other than VisualAge C++, Version 4. You can add the following directive to the top of this file if you like:
#include <istring.hpp>
You may also be interested to know that you could have reversed the order of definition above, and defined Error before Unauthorized, even though Unauthorized makes use of Error; VisualAge C++, Version 4 does not enforce the C++ requirement for forward declarations before first uses.
For more information on orderless programming, see Reduced Drudgery in the What's Cool section.
The source code to be added to user.hpp and user.cpp is given in this and the next three sections. Watch for comments indicating which file should contain each portion of code. The project will build successfully and produce the same program behavior, regardless of where you place a given declaration or definition. However, it is probably a good idea not to stray too much from your usual coding practices if you later want to build your program using a different compiler.
| Implementation Details: The User class defines the functions View, ShowAdd, Add, and Complete, to return an error message, because none of these actions can be performed by a user who is not a Reader, Reviewer, or Author. These functions will be overridden in the Reader, Reviewer, and Author classes, to allow actions appropriate to the user's authority level to be dispatched. |
To locate a view of a source file, select its workbook section, or select the file name
in the Source Files view of the Source Files page in the Project section. If no section
tabs are currently visible, click the Restore icon
at the extreme right end of the menu bar.
// In user.hpp:
class User {
Request* Req;
IString UserId;
virtual IString View() {return Unauthorized("View");}
virtual IString ShowAdd() {return Unauthorized("ShowAdd");}
virtual IString Add() {return Unauthorized("Add");}
virtual IString Complete() {return Unauthorized("Complete");}
public:
User(Request* req) : Req(req),UserId("") {}
User(): Req(0), UserId("") {}
IString Action();
};
// In user.cpp:
IString User::Action() {
IString Action=IString::lowerCase(Req->GetValue("op"));
if (Action=="") return Error("No Action Specified", "The CGI call did not specify an op= argument.");
switch (Action[1]) {
case 'a':
if (Action=="add") return Add();
break;
case 'c':
if (Action=="complete") return Complete();
break;
case 's':
if (Action=="showadd") return ShowAdd();
break;
case 'v':
if (Action=="view") return View();
}
return Error("Unknown Action Specified","The CGI call requested an unknown action, "+Action);
}
| Implementation Details: The Reader class only overrides one member function of the User class, the View function. For now, this function, and all other methods overridden by derived classes of User, will simply display a message saying the function is not yet implemented. This will allow you to test whether the program is correctly dispatching based on the authority level of the user and the type of action requested, before you actually add the code for each action. |
// In user.hpp:
class Reader : public User {
virtual IString View();
public:
Reader(Request* req) : User(req) {}
};
// In user.cpp:
IString Reader::View() {
return Error("Unimplemented","Reader::View is not yet implemented");
}
| Implementation Details: The Reviewer class inherits directly from User, rather than through Reader, because the Reviewer::View function is different from the Reader::View function, and therefore Reviewer has nothing to inherit from Reader. |
// In user.hpp:
class Reviewer: public User {
virtual IString Add(),
ShowAdd(),
View();
public:
Reviewer(Request* req) : User(req) {}
};
// In user.cpp:
IString Reviewer::Add() {
return Error("Unimplemented","Reviewer::Add is not yet implemented");
}
IString Reviewer::ShowAdd() {
return Error("Unimplemented","Reviewer::ShowAdd is not yet implemented");
}
IString Reviewer::View() {
return Error("Unimplemented","Reviewer::View is not yet implemented");
}
Rebuild the project now, and correct any typing errors. Once the project builds successfully, you can look at the Classes page in the Project section to see the classes you have defined so far.
Three classes should be displayed in the Classes view of the project (the top left pane on the Classes page). Try selecting different classes; then, for each class, select different members in the Details view to the right. Whereas in Part 3, the Source view for each member function only showed its in-class declaration, the Source view now shows the full function definition for any member function you have implemented.
If you like, you can also look at the class hierarchy for your project. Select the
Class Hierarchy page in the Project section. You will see two classes, Request, and User.
The User class has a + symbol; click this to expand the class and show its derived
classes. Reader and Reviewer both derive directly from User. Notice that you did not have
to specify any special options to be able to browse the classes used by your program. If
you want to browse classes other than the ones you define yourself, click the Filter
button (
) in the title bar of the view, and
choose the appropriate filter setting for the objects that you want to view. For example,
if you select IBM Objects, the hierarchy updates to show an entry for
each base class in the IBM Open Class Library. You can click the zoom button (
) or slide the zoom bar at the left of
the view, to zoom in on a few classes; then you can slide the bottom scroll bar to locate
a class with a + button beneath it. Click this + button
and you will again see the derived classes for that class. When you have finished, click
the filter button
again and reset the
hierarchy view to display only the objects you define (My Objects).
| Implementation Details: The Author class inherits from Reviewer because the Add and ShowAdd functions are the same. However, the View function is implemented differently, because for an author, each comment must be displayed with a Complete link so that the author can mark the comment as completed. As well, the Complete function must be implemented to allow the author to complete the comment. |
// In user.hpp:
class Author : public Reviewer {
virtual IString View(),
Complete();
public:
Author(Request* req) : Reviewer (req) {}
};
// In user.cpp:
IString Author::View() {
return Error("Unimplemented","Author::View is not yet implemented");
}
IString Author::Complete() {
return Error("Unimplemented","Author::Complete is not yet implemented");
}
Rebuild the project to ensure that you have entered the above code correctly, and make any required corrections. If you like, you can view the class hierarchy again to see how it has changed.
| Implementation Details The last piece of coding work for this part of the tutorial is to change the contents of the main function to do the following:
|
Locate a Source view of the main function. You can select the workbook section for main.cpp, or use one of the methods described in Part 3. Declaring and Implementing a Class. For a new way of locating a function, try the following steps from the Search page of the Project section:
Change the contents of the main function to the following (do not remove the #include directive for fstream.h):
int main (int argc, char* argv[]) {
// Create a request object to parse out the input
Request Req;
// Define four user types:
enum {tUser=0, tReader=1, tReviewer=2, tAuthor=3 };
// Determine the user's type, based on the user's key
int uType=tUser;
ifstream UFile(UserFilepath);
IString UserLine="";
IBoolean KeyFound=false;
while (UFile && uType==tUser) {
UserLine=IString::lineFrom(UFile);
if (UserLine.word(1)==Req.GetKey())
uType=UserLine.word(2).asInt();
}
User* U;
switch (uType) {
case tReader:
U=new Reader(&Req);
break;
case tReviewer:
U=new Reviewer(&Req);
break;
case tAuthor:
U=new Author(&Req);
break;
default:
U=new User(&Req);
}
IString Result=U->Action();
if (Result=="") Result=Error("No Output","The CGI did not return any output.");
cout << "Content-Type: text/";
if (Result.indexOf("<html>")>0 || Result.indexOf("<HTML>")>0)
cout << "html";
else
cout << "plain";
cout << "\n\n" << Result << flush;
return 0;
}
If you rebuild now, you will find that UserFilepath is not defined. You can define this string in your macros file (macros.hpp) to the path and filename of the file that will contain user information. There are two ways you can find macros.hpp:
- Select its section in the workbook
- Go to the Macros page in the Project section. In the Macros view on this page, the first macro is selected by default. The source for this object is displayed in the Source view below.
Add a definition of UserFilePath to macros.hpp. Change the text shown in the example to a path that is valid on your computer:
#define UserFilepath IString("F:\\vatutor\\users.dat")
Once UserFilepath is defined, rebuild the project. You can use the links in the next section to test your code. For testing purposes, create users.dat in the directory you chose and add the following lines:
reader 1 reviewer 2 author 3
If you create the file within the IDE, note the following points:
Ensure that the VisualAge Help Server is running on your machine, then use the links in the table below to test out the CGI and see how it performs for users of different authority levels making different requests:
| User Type | Request | Link to Try | Message Type Returned |
| User | Add | op=Add+key=unknown | Unauthorized access |
| Complete | op=Complete+key=unknown | ||
| ShowAdd | op=ShowAdd+key=unknown | ||
| View | op=View+key=unknown | ||
| Unknown request | op=Anything+key=unknown | Unknown action specified | |
| Reader | Add | op=Add+key=reader | Unauthorized access |
| Complete | op=Complete+key=reader | ||
| ShowAdd | op=ShowAdd+key=reader | ||
| View | op=View+key=reader | Reader::View unimplemented | |
| Unknown request | op=Anything+key=reader | Unknown action specified | |
| Reviewer | Add | op=Add+key=reviewer | Reviewer::Add unimplemented |
| Complete | op=Complete+key=reviewer | Unauthorized access | |
| ShowAdd | op=ShowAdd+key=reviewer | Reviewer::ShowAdd unimplemented | |
| View | op=View+key=reviewer | Reviewer::View unimplemented | |
| Unknown request | op=Anything+key=reviewer | Unknown action specified | |
| Author | Add | op=Add+key=author | Reviewer::Add unimplemented |
| Complete | op=Complete+key=author | Author::Complete unimplemented | |
| ShowAdd | op=ShowAdd+key=author | Reviewer::ShowAdd unimplemented | |
| View | op=View+key=author | Author::View unimplemented | |
| Unknown request | op=Anything+key=author | Unknown action specified |
If the output you see in your browser after trying these links does not reflect what is indicated under "Message Type Returned" in the table, you may have entered some of the code incorrectly. See below for possible causes and corrective actions. If the links worked as expected, you can proceed to the next part of the tutorial:
The following are some errors that may occur as you try out the links in the table above. Possible causes and corrective actions are given.
You have not started the VisualAge Help Server. Start the IDE and select one of the items under the Help menu. This should start the VisualAge Help Server. If no help displays in a browser, you may need to reinstall the product and ensure that the Help components are installed.
The VisualAge Help Server is running, but the CGI application could
not be found in the directory where the help server expects to find CGIs. Your executable
files must be in the correct directory:
on
Windows NT, this is usually \IMNNQ_NT
on OS/2,
this is usually \NETQOS2
In addition to your executables, the directory should also contain other files that are
not part of your project. If it does not, you may have accidentally created a new
directory instead of choosing the existing one.
If only your executable file is stored in that directory, you have installed the VisualAge Help Server in a different directory. Locate that directory by searching for the file vacwebx.exe (Windows NT or OS/2) or vacwebx (AIX) on your file system. When you have found the directory where this file is stored, do the following:
If other files are in the directory but your executable file is not, follow the six steps shown above, but for step 4 use the directory where the VisualAge Help Server is installed. Make sure the file name is spelled correctly (for example, you may have accidentally specified reveiw.exe as the target when you set up your project, instead of review.exe).
The VisualAge Help Server is not configured properly, or the CGI is stored in a document directory instead of a CGI directory, or the CGI executable terminated abnormally.
You may not have entered the source code correctly. Replace the source code in your project with the source files provided at the start of Part 5. Debugging and Revising Your Application, by copying and pasting from your web browser into the IDE. Rebuild the application and try again.
Once you have corrected any problems, proceed to:
