CSI 250L Lab 4, Spring 2012

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

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

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

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

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 4 Problem1: Trace the following program: a- #include <iostream> using namespace std; class Time { private: int hour, min; public: Time(int h, int m) {hour = h; min = m;} Time() {hour = 0; min = 0;} void setHour(int x) { cout << "Set hour" << endl; if (x>12) hour = 12; else hour = x; } void setMin(int x) { cout << "Set min" << endl; if (x>59) min= 0; else min = x; } int getHour() {return hour;} int getMin() {return min;} }; int main() { Time t1; t1.setHour(8); t1.setMin(23); cout<<"the object t1 time is:\ n"; cout << t1.getHour() << endl; cout << t1.getMin() << endl; Time t2 = t1; cout<<"the object t2 time is:\ n"; cout << t2.getHour() << endl; cout << t2.getMin() << endl; Time t3(t2.getHour()+1 , t2.getMin()+2); cout<<"the object t3 time is:\ n"; cout << t3.getHour() << endl; cout << t3.getMin() << endl; system("pause"); return 0; } 1 Problem 2: a- #include <iostream> #include <cstring> using namespace std; class Student { private: char name[30]; int age; char gender; public: void setName (char *); void setAge (int); void setGender (char); void print (); }; void setName(char * n) { strncpy (name, n); } int Student::setAge (int a) { a = age; } Student:: void setGender (char g) { gender = g; } void Student::print() { cout << name, age, gender; } void Student::display() { cout << "This function does not belong to the class."; } 2 Problem 3: Type the following program then apply the below listed steps: #include <iostream> #include <cstring> using namespace std; class Car{ public: Car(char []="No value ", char[]="No value " ,int=2000, int=0); void print(); private: char brand[10]; char color[10]; int year, price; }; Car::Car(char * b, char * c, int y, int p) { strcpy(brand, b); strcpy(color, c); year = y; price = p; } int main() { Car c("BMW", "Black", 2002, 13000); system ("pause"); return 0; } 1. Compile, link then execute the program. 2. Modify the class definition and implementation to include set and get functions for the private data members. 3. Modify the class definition and implementation to include a print function to print information about the private data members. 4. Modify the program to store the data about N cars. (N is the first input by the user). Then output the data about the cars with price above 50000. 3 Problem 4: Write the class definitions and implementations for the following class specifications. Use separate files for the definition and implementation. Publisher Attributes  ID: a number to identify each publisher.  Name: a string of characters. ( dynamic allocation)  Location: a string of 50 characters.  Type: an integer to specify the type of the publisher, the type can be 1, 2 or 3. Methods  Constructor(default values)  Print: a function, to print the information about a publisher in the following format: ID = 111, Name = Print_Shop, Location = Lebanon, Type = 1.  Destructor.  Set and get functions. Write a c++ program to store data about N publishers. The program should do the following:  Output the information about all publishers of type 1.  Display a sorted list of publisher according to the location.  Change the location “Beirut” to “Saida”.  Display the information about a publisher knowing his ID. 4