Sunteți pe pagina 1din 6

1

s



Answer ALL questions! points will be assigned to each question
relative to the indicated time!
If you can not solve any part proceed to the next and you can use
previous functions EVEN IF YOU DID NOT SOLVE THEM!

Question I: Answer the following questions: (20 mins)

1. Dynamic memory allocation has its advantages but there may be some
problems. Explain the advantages and state one important problem and
how it can be solved.













2. Give one difference between C++ and Java.







CAIRO UNIVERSITY COMPUTER ENG. DEPT.
FACULTY OF ENGINEERING MID Spring 2008
CMP103
Programming Techniques


2
3. What is the difference between function overloading and function
templates? Give an example.















Question II:

4. Part I:

Given the following class Time:
class Time
{
int hour, min;
public:







};

Write:
A set function that validates hour and mins values.
A suitable default constructor with validation.
An operator that implements the following:
Time t1, t2;
.
if ( t1 > t2)
cout << " t1 is later than t2!";





3
5. Given the following class prog_slot that represents a slot in the TV program
on a certain day.

class Prog_slot
{
char* slot_name; // e.g. ElBeit Beitak, Ahly and Zamalek
char slot_type [20]; // e.g. talkshow, match, news, film .
Time disp_time; // display time
long duration; // duration in mins
public:










};

Write (using class Time in part I):
Suitable set functions ( for each data member separately)
Suitable constructor and destructor
Function Read_slot that reads the data of a slot through keyboard.
Function Display that displays data of a certain slot
Any function necessary to compile and run the following main
correctly.
i. write a comment next to the statement indicating the function you
will implement for it
ii. write the prototype (declaration) in the class
iii. write the function implementation at the end
Write the global function special_prog that displays programs of a
special type (e.g. news or talkshow. If none are found then the function
should display: No programs found!
Complete the main as directed.

# define NUM 100
void special_prog ( Prog_slot * prog, int num_slots, char* pr_type);

main ( )
{
Prog_slot Daily_prog [ NUM];

cout << "Enter program information:\n";

for (int i = 0; i < NUM; i++)
// function Read to input data

4



6. Given the following class Car: (
a) Write a suitable constructor and destructor.
b) Write a suitable set and get functions.
c) Complete the class by adding any necessary function and/or operator
such that the main compiles and runs correctly.
d) If a medicine may have the same name but is produced by different
companies, modify the main to display all those medicines using the
global function Display_all.

Note: First add the function prototype inside the class then write the
implementation at the end.

class Car
{
char* name; // e.g Corolla or Lanos
long buying_day; // format ddmmyy
char company [20]; // e.g. Toyota, Daewoo
float price;
public:









};
void Display_all (Car* all, float pr);

// displays all cars in all less expensive than pr

main ( )
{
Car* car_shop;
char med_nm [ 20];




Medicine med;
5
bool found = false;
long today; // date format ddmmyy
// e.g. 140706 is day 14, month 07 and year 06

For (int i = 0; i < 100; i++)
Pharmacy. input ( ); // reads Medicine info from keyboard

cout << " Enter today' s date: ";
cin >> today;

cout << " Enter the name of the medicine you are searching for;"
cin >> med_nm;

i = 0;
while ( i < 10 )
if ( Pharmacy [ i] == med_nm )
{med = Pharmacy [ i ];
found = true;
}

if ( !found )
cout << "med_nm << "not found";
else
{
cout << med_nm << " by " << med.get_comp_name ( );

if ( ! med (today) )
cout << "has not expired ";
else
cout << "has expired " on << med. get_exp ( );
}




}

// write Functions' implementations here












6









7. Given the following class: ( 30 mins)

class Date {
int month, year;
public:
Date ( int , int );
void set ( int , int );
int get_mon ( ) const;
int get_yr( ) const;
void set_mon ( int );
void set_yr (int );
};

which of the following statement will produce an error? WHY?

main ( )

{ Date d1 (3, 08), d4;
const Date d2( 11, 07);
const Date* dp1 ( 2, 05);
Date* const dp2 = new Date ( 3, 03);

d2. set ( 4, 03 );
d4. set ( 5, 01 );
dp1 set ( 7, 02 );
dp2 set ( 4, 06 );
dp2 =& d1;

cout << d1.get_mon ( ) << '\t' << d1. get_yr ( ) << endl;
cout << dp1 get_mon ( ) << '\t' << dp1 get_yr ( ) << endl;
}

If class Date is used in class Medicine instead of the long expiry_date,
what are the necessary modifications?

S-ar putea să vă placă și