Skip to main content
Hany Ammar
Professor Emeritus, Computer Science and Electrical Engineering

Labs

Lab #1

Objectives

  1. Program Structure: Dissect a C++ program, and look through the error messages produced when an important part is missing or incorrectly specified.
  2. Interactive Development Environments: Practice the use of a C++ IDE.

Procedure

    pixel.cpp program is in the directory G:\lang\turbocpp\examples, load it using the "open project" option in the project menu of the IDE. The program consists of the files point.h, pixel.cpp and point2.cpp.

  1. Run the pixel.cpp (see footnote) program discussed in class using any C++ IDE with different values for the point locations (e.g., a starting location of (0,0) and move it to the opposite diagonal location). Write detailed comments in the program to describe the important parts of the program. Start with a comment at the top describing the program as a whole.
  2. Edit the file point.h and modify it as shown below, then compile the .cpp files and record the compilation errors listed in each case.
  • Delete the ";" located after the closing braket (i.e., "}") of the class Location.
  • Change the name of function Location in the public part of class Location to "location"(i.e., with a lower case 'l').
  • The function Point in class Point has two arguments. Delete the "int" keyword located in front of each argument.
  • Edit the file point2.cpp, delete the class designation "Location::" located in front of the function GetX, compile the program and record the effect of this change.
  • Use the debugging utility in your IDE to step over the execution of pixel.cpp. Use the Watch Window to watch the behaviour of the object Apoint.
  • Edit the file pixel.cpp, expand function main to create two additional Point objects (e.g., Bpoint, and Cpoint) adjacent to Apoint. Show all pixels and move them together.
  • Load and run the programs circle.prj and mcircle.prj. Describe the added functionality in each program.
  • Comment briefly in your report on the OOP concepts of encapsulation and inheritance discussed in class by giving examples from the above programs.
  • Lab #2

    Objectives

    Data types: Practice the use of data types by modifying example programs and writing a simple function.

    Procedure

    The following programs are in the examples directory: G:\lang\turbocpp\examples

    Part 1: The following problems involve the program intro28.cpp and the program ex7.prj which consist of the files list.h, list.cpp and ex7.cpp

    1. Load and run the program ex7.prj. Modify function main in file ex7.cpp to add a constant integer to the elements of the list 1 (you will need to utilize the member functions get_elem and put_elem of class list).
    2. Add a member function called add_const to the functions of the class list to accomplish the same operation as in 1 above. This function should have one argument of type const int, and should have no return statements. Test the function by modifying the function main() .
    3. Add a member function called sort() to the functions of class list to sort the elements of an array(an object of class list) in ascending order. Do the sort using the simple bubble sort technique, that is, start in the beginning of the array compare the first two items if the first item is smaller than the second, test the second and the third, etc. until you find a pair that is out of order, then exchange them in the array and start over at the beginning of the array. When a pass through the entire array finds no pair out of order, stop. Define a general purpose function called swap to exchange two out of order elements of the array and call it from function sort() .
    4. Load and run program intro28.cpp. Change the program to enter the values of the hours array from the keyboard using a simple loop (use cout for printing a prompt message and cin to read the values).
    5. Modify the above program to utilize the class list. In this case the array hours should be declared as an object of class list, and elements of the array will have to be initialized, modified and retrieved using the class member functions. The class list declaration and function definitions will also have to be modified. Call the modified class flt_pt_list for floating point arrays, and add to it the needed functions to accomplish the operations shown in program intro28.cpp (Note that you will need to include the header file for the class and you also need to start a new project on the IDE and add to it the items or files intro28.cpp and flt_pt_list.cpp).

    Part 2: The following problems involve the programs figdemo.prj and dynpoint.prj

    6. Load program figdemo.cpp, inspect the code and comments written in function main, then run the program. File figures.h contains declarations for the class location, point, and circle as well as the prototype of function GetDelta(). The body of this function is defined in figures.cpp. Implement the arguments of this function using pointers instead of references, and change all affected lines of code in the program. show only the affected lines of code in your report. Comment on the differences between the two implementations. Rerun the program and then trace over the code using the trace option in the run menu and watch the values of the pointers used in the function.

    7. Load and run the program dynpoint.prj. Comment on using the storage allocation operators new and delete in function main of file dynpoint.cpp. Also comment on using a pointer to object Apoint and the way the member function show() was called. Modify the main routine to create an object of type circle using the new operator and call the Drag function this time to drag this object.

    Lab #3

    Objectives

    Data types: Practice the use of structs and classes by modifying example programs and writing programs using linked lists.

    Procedure

    The following programs are in G:\lang\turbocpp\examples directory.

    1. Load the program listdemo.prj to your IDE. Run the program and comment on the effect on memory status after each object is created. What could be the reason behind using a linked list in this program. Also comment on using a pointer of type Point as a pointer to the objects of type Arc and Circle (although this is related to the concept of inheritance which will be discussed later in more detail, it good to note this now).
    2. Load and run program solar.cpp. Change the program to define the elements of the array solar_system using a linked list. To do this you need to declare a linked list class called Planet_list. The list element should consist of a pointer to an object of type Planet and of course a pointer to the next element in the list. Define the class member functions as needed to accomplish the same application shown in the program main(). Generally you will need similar functions as in the example program listdemo.prj in 1 above, such a function to add an element to the end of the list, and a function to print the elements of the list.
    3. Test the class in 2 above by rewriting program main() . You need to create an empty list of planets called Solar_system, then each planet will be created, initialised with data, and then added to the list. Finally print all the elements in the list(you only need a couple of planets as the current function main () is showing).
    4. Develop a sort membership function of the class linked list of planets described in 2 above. This sort of function will sort the elements in the linked list in ascending order according to size of the planet(i.e the radius attribute). You also need as in lab2 to have a function called swap() which will be called from sort(). The sort() function should swap the planets in the list by swapping their pointers. Test this function by calling it from function main().

    Lab #4

    Objectives

    Practice the use of a class structure by modifying an example program and developing a class for sparse matrix applications.

    Procedure

    Note: All the program listings and their executables are to be given in a floppy along with printouts.

    Part I: The following programs are in G:\lang\turbocpp\examples .

    1. Load the program string.cpp in your IDE. The class string defined in this program specifies string type objects using encapsulation and information hiding. Answer the following questions based on the above class.
      Explain, with reasons, the results of running the program by incorporating the following changes.
    • Replace the key word 'class' with 'struct' in the program. Comment on the difference between the two implementations.
    • Make changes to any of the three constructors so as to return a value on successful initialization of the object.
    • If any of the constructors are:
      • Omitted, or
      • Moved to the private section of the class. Explain the results on running the program.
    • Add a fourth default constructor that does not initialize any variables.
  • Load and run the program ex5.prj in your IDE. The file def.h declares the class Definition specifying objects defining a word and several sentences describing its different meanings. Answer the following questions based on this class.
    • What would happen if the constant integer Maxmeans is declared as a data member of the class in the private section. Try to run the program and comment on the error messages produced. State clearly the changes required, to elimanate errors.
    • Add a constructor function with one argument which will initialize in its init-list(not in the function body) the constant Maxmeans. Refine the main() routine to test the constructor.
    • Add another constructor to the one implemented above. This constructor should also initialize the word string with a given character string, and should have a default value of 5 to Maxmeans. Again test this constructor from the main() routine.

    Part II: Sparse Matrix Applications

    Develop a class called Sparse Matrix to implement the definition of sparse matrices using linked lists. Each row of matrix will be implemented by a linked list containing the nonzero elements of the row. Each element of the list should contain a column index and a nonzero floating point value. The member functions of this class should consist of a constructor, a destructor, a put element function which returns a value indicating whether the element was successfully inserted in proper linked list, a get element function which also returns a value indicating that the element value has been successfully located and retrived, and a function delete element which locates and deletes a matrix element. Test your implementation of the class and its member functions using a simple main() routine.

    Lab #5

    Objectives

    Practice the use of nested classes by modifying a program. Develop a simple discrete-event simulation of a client-server model.

    Procedure

    Part I

    Load the program listdemo.prj. Change the file listdemo.cpp to include the Node structure declaration within the public section of class List. Change other parts of the program (if needed) and comment on the effect and use of this change. Is it possible to instantiate an object of type Node from nonmember functions of the class List? if so instantiate a Node type object in fuction setlist() make it point to an object of type Circle and add it to the List Alist (the member function add in class List should be overloaded by a function of the same name with an argument of type Node&, this function should then insert the referenced node to the Alist object).

    Part II

    Discrete Event Simulation

    You are required to develop a discrete event simulation of a simple client-server model. The model consists of clients arriving at service facility. Each client has an arrival time and service time demand from a server in the service facility. The facility consists of a first-come-first-serve (FIFO) queue and an array of n servers. When a client arrives he immediately goes to a server if one is available, otherwise he joins the end of the queue.

    The simulation will be carried out using an event list, where an event can be either a client arrival or a client departure. Elements of the event list consist of a pointer to a client, an event type (arrival or departure), an event time, and perhaps the server number assigned to a client. The list is ordered in ascending order by the time attribute(i.e the event that will happen next is at the head of the list). The simulation time will be advanced to the time of the event at the head of the list, the event will then be removed from the list and simulated as follows:

    1. For an arrival event, a next arrival event will be generated (with a time equals to the current simulation time plus the inter-arrival time between clients) and added to the event list(i.e., an arriving client will trigger the arrival of the next client). The array of servers will then be checked for an available server, if one is found, a departure event will be generated( with a time equals to the current time plus the service demand of the arriving client) and added to the event list. If all servers are busy the client will be put at the end of a linked list simulating the FIFO queue.
    2. For a departure event, the client at the head of the waiting queue will be removed and another departure event will be generated with a time equals the current time plus the service time demand of the client (a departing client triggers another departure if the queue is not empty).

    The simulation ends when the simulation time reaches the total simulation time. Use the following simulation parameters which are assumed to be deterministic values for simplicity (usually in such simulations the inter-arrival time and the service demand time are random variables with a given probability distribution generated using functions such as random() which is one of the standard library functions declared in stdlib.h):

    • Total simulation time = 3600 seconds
    • Inter-arrival time between clients = 20 seconds
    • Service demand time for each client = 100 seconds
    • Number of servers = 4

    The program should print the total number of clients generated, the number of clients served, and the average time spent by a client in the service facility during the specified simulation period.

    Lab #6

    Objectives:

    Practice the use of the "this" pointer, static data members of a class, and operator overloading. Develop a simple class for complex numbers manipulation, and a class for complex vector manipulation.

    Procedure

    Part I

    1. The implementation of a class for doubly linked lists is an example of using the "this" pointer as discussed in class. Load program Listdemo.prj into your IDE. Modify struct Node and the member functions of class List to implement a doubly linked list.
    2. Add a static data member of type int called no_of_objects to the protected section of class Point in figures.cpp. This member should be initialized to zero in the beginning of file figures.cpp, and incremented by one in the constructor function Point. Print the value of this data member at the end of function main().

    Part II

    1. Develop a class called "Complex" for complex floating point number manipulation. The class should contain data members needed to specify a complex number, and functions to support the operators +, -, *, and to carry out a complex number addition, subtraction, multiplication, and conjugate operations, respectively. Also include functions for the operators +=, -=, and *=.
    2. Develop a class "Complex_Vector" for specifying and manipulating vectors whose elements are of type Complex. The length of the vector should be declared as a static constant data member of value 100 and used as a default argument in the constructor function of the class. The member functions of this class should support the operators +, -, * (which stands for a dot product), and the indexing operator [].
    3. Write a main() routine to test the classes developed above. Use the indexing operator to add or get elements to a Complex_Vector object. Implement several expressions which involve objects and combine the above operators, and verify their results. For example, let A,B,C, and D be objects of type Complex_Vector, and let x, y, and z be of type Complex, evaluate the following expressions:

      z += y *= !x + y,
      A[0] = z,
      D = A + B + C,
      x = (A+B-C) * D

    Lab #7

    Objectives

    Practice the use of public, protected and private class inheritance.

    Procedure

    Part I

    1. Program ex8.prj in the examples directory defines the class Stack for stack type data objects as a derived class from class List. Modify function main() in file ex8.cpp to make the Stack type object use the member function put_element of class List (instead of the member function push of class Stack) to specify values of the stack elements. Use the function get_element of class list to print out the elements inserted in the Stack type object (instead of using the print() function of Stack).
    2. Is it necessary to use public inheritance in the above program?. What would happen if class Stack was defined as privately derived from class List?. Following the changes to function main() in 1 above, modify class Stack to implement private inheritance, and comment on any error messages produced. Modify class Stack once again to implement protected inheritance and observe the outcome. Comment on the difference between public, private, and protected inheritance as shown in this example and in general.
    3. Both Class list and class Stack in the above program have their own print() member functions. Class Stack inherits the print() member function of class List and overloads it with its own member function. Is it possible for the Stack type object s defined in function main() to invoke the print function of class List ?, if so show how this can be done by modifying function main() only ( in this case the member function get_element() of class List need not be used in main() to print out the elements of the Stack type object);

    Part II

    1. The program listdemo.prj show an example of several levels of inheritance. Notice that the data members of class Location, class Point, and class Circle are declared in the Aprotected@ sections of these classes. What would happen if they were declared in the private sections instead?
    2. Add another class of objects of type Cylinder to the above program. Class Cylinder should be derived from class Circle. The data member needed for this new class is an integer representing the height of objects of type Cylinder. The member functions include set_height(int). get_height(), area(), and volume(). The last three functions should be implemented as const functions (recall that a const member function can access the data members but can not modify them) . Class Cylinder should be declared in a separate header file cylinder.h, and its functions defined in file cylinder.cpp. Modify function setlist() in listdemo.cpp to instantiate an object of type Cylinder and add it to the linked list Alist.

    Lab #8

    Objectives

    Practice the use of multiple class inheritance and the concept of polymorphism using virtual functions.

    Procedure

    Part I

    1. Program ex9.prj  in the examples directory defines the class Stack for stack type data objects as a derived class from class List. The print member function in class list is declared as a virtual function. Delete the keyword virtual, run the program, and observe the difference between the two implementations. Given the meaning of a virtual function discussed in class, comment on the need for using virtual functions in this example.
    2. The program listdemo.prj shows a simple example of using polymorphism. Notice that the member functions show(), hide(), and Drag() of class Point are all declared as virtual functions. Comment on the need for declaring these functions as virtual functions in this example. Is it also useful to declare the member functions show() and hide() of class Circle to be virtual ?, if so declare the functions as virtual and modify function main() to illustrate the use of this change.

    Part II: Simulating a slot machine

    Consider the program that simulates the actions in a slot machine Slot.cpp. The slot machine consists of three turning wheels randomly displaying colored shapes in one row. The program displays a row of randomly changing shapes. The rate of change should slow down and then reach zero to simulate the effect of the wheels slowing down and coming to a stop.. The shapes needed for the slot machine program are: a Cherry drawn as a red Ball; a Grape drawn as a blue Ball, a square drawn as a white rectangle with equal sides; and a pyramid drawn as a green triangle with equal sides. The program contains the definition of class noshape which is defined to introduce the operation of erasing a displayed shape. This operation is needed in order to display different shapes on the same place on the screen to mimic a turning wheel. Class Wheel defines the shapes in a wheel and provides a function to draw one of the shapes at random. Function main simulates the actions using the sound(), delay(), and nosound() library functions. You are asked to do the following:

    1. Run the program and observe its operation using different values for the loop count and delay time values in main(). Comment on the rational for using multiple inheritance in this program.
    2. Specify which member function in the above program should be declared as virtual function and why? Modify the code to make use of the concept of polymorphism. (Hint implement the shapes in the Wheel class by an array of type pointer to Shape, add a member function to the class to populate the elements of this array, other member functions of Wheel as well as the main() routine should also be changed).

    Lab #9

    Objects

    Practice the use of templates, and document the initial analysis and design of the term project using the attached project requirements.

    Procedure

    Part I: Templates

    1. 1. Program ex7.prj in the examples directory defines the class List as an array of integers. Change the class definition to use templates in order to implement a generic list class whose objects can be an array of integers, floating point numbers, or characters. Test the class implementation from function main() and instantiate objects of type List using int, float, and char data types.

    Part II: Term project

    The project will be developed on several phases. You are required to work on phase I and phase II in this assignment described as follows:

    Phase I:

    Analyze the project requirements and specify the required classes of objects for the application.

    Using the given project requirements identify using Rational Rose and UML Notation the classes of objects needed in the application together with their attributes(i.e., the data members and the functions or methods). Try to use the concept of abstraction as much as possible to specify a class hierarchy.

    Phase II:

    For the class hierarchy specified in phase I, develop a design for the class definitions and for the class member functions. Concentrate on the functions needed to define, check, and show the configuration of a queuing network. The function bodies should only contain design information without implementation details (i.e., C++ code is not required for the functions, only steps of the algorithms).

    The above analysis and design should be divided on each group member. The document submitted should clearly specify the analysis and design done by each member by attaching the group member name with specific analysis or design artifacts.

    Lab #10

    Objective

    Implementation and Testing: Implement the classes designed in lab 9 incrementally and test the implementation using unit testing and scenario-based testing (using the scenarios of define, check, show, and simulate discussed in class).

    Procedure

    1. Implement and test the methods in the classes specified in lab 9 needed to define a queuing network (i.e., class members needed to create nodes and links and add them to a network). Test classes as units using unit testing when possible then use scenario testing to test the define network scenario.
    2. Implement and test the methods in the classes needed to check a queuing network (i.e., class members needed to check the nodes and links of a network). Test classes as units using unit testing when possible then use scenario testing to test the check network scenario.
    3. Implement and test the methods in the classes needed to show a queuing network configuration(i.e., class members needed to show the nodes and links of a network). Test classes as units using unit testing when possible then use scenario testing to test the show network scenario.
    4. Implement and test the methods in the classes needed to simulate a queuing network (i.e., class members needed to simulate the nodes and links of a network). Test classes as units using unit testing when possible then use scenario testing to test the simulate network scenario.