You are on page 1of 2

Lab [3]: [Object Oriented Programming]

Introduction In this lab, we shall practice the topics that we studied during last three lectures including: - Introduction to Classes - Constructors - Overloaded Constructors - DMA in classes (to be explored by students) Objectives The objective of this lab is to understand the topics mentioned above and to enable students to write intermediate level programs using the mentioned topics. Tools/Software Requirement Microsoft Visual Studio 2010 Description You may consult lecture slides for the recent lectures and related book chapters during the lab session whenever you feel necessary. Lab Tasks 1. Write a class CheckBook that has data members including balance, lastCheck, lastDeposit and necessary member functions (as discussed in the class). Use a separate header file for class specification, another .cpp file for class implementation and another .cpp file to write the main function which tests this class. 2. Write a class Triangle that has two int type data members: base and altitude. The class should have functions to set and get the values of data members. In addition, the class should also have a member function called area() that can calculate the area of the triangle. Note: The formula for finding the area is: * base * altitude 3. Create a class called rational for performing arithmetic with fractions. Use integer variables to represent the private data of the class the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. Your program should store the fraction in reduced form. For example, the fraction 2/4 would be stored in the object as 1/2. Provide public member functions that perform each of the following tasks:

a. Multiplying two rational numbers. The result should be stored in reduced form. b. Dividing two rational numbers. The result should be stored in reduced form. c. Adding two rational numbers. The result should be stored in reduced form. d. Subtracting two rational numbers. The result should be stored in reduced form. Write a program to test your class. 4. One of the weaknesses of C++ is that it does not automatically check array indexes to see whether they are in bounds. Write a class called safearray that has data members: an int type pointer to pointer (to store address of dynamically created 2D array), int type row (to store row size of 2D array) and int type column (to store column size of 2D array). The class should have overloaded constructors and necessary member functions to set and get the values. The class also has two functions putel(int value, int rowIndex, int colIndex) and getel(int rowIndex, int colIndex). The function putel() receives as argument a value to be stored in the 2D array at index specified through the other two arguments rowIndex and colIndex. The function getel() returns the value stored at the index specified through the two arguments rowIndex and colIndex.

Both functions should check the row and column index arguments to make sure they are not less than 0 or greater than row-1 or col-1. Dont forget to de-allocate the memory allocated in the class whenever its usage is over. Write a program to test your class. Deliverables 1) The first three questions are to be submitted at the end of this lab session, followed by viva. Please make a single .CPP file of all your programs and submit it on LMS. The name of submitted file must contain lab number, your name and registration number. For example, for lab-1 if Ali Khan having registration number 123 is submitting his solution, the name of his file shall be: Lab-01-Ali_Khan-123.CPP. 2) The last question is to be submitted by 2300 hrs on Sunday, Oct 07, 2012. The link to upload your solution shall be shared later on LMS. The name of file should be: Lab-Assignment-01_your-regNo.CPP. The viva for lab assignment shall be conducted in the next lab.

You might also like