CSI 250L Lab 8, Spring 2012

American University of Science and Technology · Programming II Lab · 3 pages · Dr. Saeed Raheel

All CSI 250L files
CSI 250L Lab 8, Spring 2012, page 1

Stuck on a question? A tutor can walk you through it.

CSI 250L Lab 8, Spring 2012, page 2 CSI 250L Lab 8, Spring 2012, page 3

More CSI 250L files

Show the text of this lab
American University of Science & Technology Department Of Computer Science CSI 250L – Computer Programming-II Lab Spring 2011/2012 Lab Work 8 Problem1: Trace the following program: #include <iostream> #include <cstring> using namespace std; class inventory { char item[40]; // name of item int onhand; // number on hand double cost; // cost of item public: inventory(char *i, int o, double c) { strcpy(item, i); onhand = o; cost = c; } friend ostream &operator<<(ostream &stream, inventory ob); friend istream &operator>>(istream &stream, inventory &ob); }; ostream &operator<<(ostream &stream, inventory ob) { stream << ob.item << ": " << ob.onhand; stream << " on hand at $" << ob.cost << '\n'; return stream; } istream &operator>>(istream &stream, inventory &ob) { cout << "Enter item name: "; // enter jigsaw stream >> ob.item; cout << "Enter number on hand: "; // enter 7 stream >> ob.onhand; cout << "Enter cost: "; // enter 54.95 stream >> ob.cost; return stream; } 1 int main() { inventory ob("hammer", 4, 12.55); cout << ob; cin >> ob; cout << ob; return 0; } Problem 2 : Write the class definitions and implementations for the following classes. The class definition and implementation should be written in a separate file. Use dynamic allocations Date Attributes  day: an integer  month: an integer  year: an integer Functions  Constructor with default values  Overload the >> operator to enter a date in the format day/month/year  Overload the << to output a date in the format day/month/year Screen Attributes  brand: a string to specify the brand of the screen  size: an integer to specify the size of the screen  price: a double value to specify the price of the screen  color: a string to specify the color of the screen  date: an object of class Date to specify the manufacturing date of the screen Functions  Constructor with default values  Overload the >> operator to enter information about a screen  Overload the << to output information about a screen 2 Write a program to create an object screen to test the class functionalities. Problem 3 : Create class IntegerSet. Each object of the class can hold integers in the range 0 through 100. A set is represented internally as an array of Booleans, a. Array element a[i] is true if integer i is in the set. Array element a[j] is false if integer j is not in the set. The default constructor initializes a set to the so-called “empty set” (i.e., a set whose array representation contains all false values). Provide the following methods: Method unionOfIntegerSets creates a third set which is the set-theoretic union of two existing sets (i.e., an element of the third set’s array is set to true if that element is true in either or both of the existing sets; otherwise, the element of the third set is set to false). Method intersectionOfIntegerSets creates a third set which is the set-theoretic intersection of two existing sets (i.e., an element of the third set’s array is set to false if that element is false in either or both of the existing sets; otherwise, the element of the third set is set to true). Method insertElement inserts a new integer k into a set (by setting a[k] to true). Method deleteElement deletes integer m (by setting a[m] to false). Method setPrint prints a set as a list of numbers separated by spaces. Print only those elements that are present in the set. Print ---- for an empty set. Method isEqualTo determines if two sets are equal. Write a program to test your IntegerSet class. Instantiate several IntegerSet objects. Test that all your methods work properly. Overload the operators the following operators to be applied on an Integerset object: "+" to add an element to an Integerset "-" to delete an element to an Integerset "<<" to print the elements of an Integerset 3