Richard G Baldwin (512) 223-4758
 
Practice Exam #2, CIS2204, Summer 1997 
 
The following problems are of the type, style, and general complexity  
that you can expect to find on your second exam.  However, the  
specification format may not match that on the exam.  You should study  
them carefully.  As explained in the preface to the first sample test,  
it is especially important that you pay attention to key terms such as  
public, private, parameterized, returns, etc.  If you dont understand  
the terminology, you wont understand the program specifications on the  
exam, and you cannot possibly do well.  After studying your textbook if  
there are still terms that are not clear to you. please let me know. I  
will attempt to define or explain the terms for the benefit of you and  
the remainder of the class. 
 
Although this sample test is fairly comprehensive in terms of topics  
covered, it is not exhaustive.  The fact that a topic does not appear on  
this sample test does not mean that it will not appear on the real  
examination. 
======================================================================= 
 
/*CIS2204 Sample problem #14 
Put YOUR name on the screen and display a terminating message. 
 
Create a class named FOLKS which contains two private data members 
of type char named POOR and RICH. 
 
The class contains three public member functions. 
 
The public function named SHOW() is used to display the two data 
members. 
 
The second public member function is a destructor which displays 
the message "goodbye object." 
 
The third public member function is a parameterized constructor 
which initializes the values of POOR and RICH. 
 
Use automatic in-lining for all public functions. 
 
Provide a main() function which declares a 2x3 array of objects, 
named ob, of type FOLKS, and initializes the values of POOR 
and RICH when you declare the array such that the array 
contains the following character values: 
 
A B   C D    E F 
G H   I J    K L 
 
Declare a pointer of type FOLKS and point it to the first 
data member in the array. 
 
Write a loop in main() which uses the pointer to display 
the contents of each of the two data members of the six objects 
in the array in a format similar to that shown above 
*****************************************************************/ 
 
 
/*CIS2204 Sample problem #15 
Put YOUR name on the screen and display a terminating message. 
 
Create a class named FOLKS which contains two private data members 
of type char named GOOD and BAD. 
 
The class contains three public member functions. 
 
The public function named display() is used to display the two data 
members. 
 
The second public member function is a destructor which displays 
the message "Object Destroyed." 
 
The third public member function is a function named PUT() which 
is used to assign values to GOOD and BAD. 
 
Use automatic in-lining for all public functions. 
 
Provide a main() function which uses the keyword new to 
dynamically allocate a 3-element array of objects, of type FOLKS. 
Confirm that the space for the array is successfully 
allocated, and terminate with an error message if the space is 
not successfully allocated. 
 
Cause the values of GOOD and BAD in each of the 
array elements to contain the following character values: 
 
A B 
C D 
E F 
 
Write a loop in main() which displays the contents of each of 
the two data members of the three objects in the array in a format 
similar to that shown above. 
 
Use the keyword delete to return the allocated memory to the 
system before the program terminates. 
*****************************************************************/ 
 
 
 
/*CIS2204 Sample problem #16 
Put YOUR name on the screen and display a terminating message. 
 
Create a class named FOLKS which contains one private data member 
which is a char array named TALL. 
 
The class contains three public member functions. 
 
The public function named display() is used to display the data 
member when treated as a string. 
 
The second public member function is a destructor which displays 
the message "Object Destroyed." 
 
The third public member function is a parameterized constructor 
which stores its argument in TALL. 
 
Use automatic in-lining for all public functions. 
 
Provide a main() function which creates an object named obj of 
type FOLKS and initializes the data member of that object to the 
string "Tall folks". 
 
The main function passes the object by reference to a function 
of your own design named ShowByRef() which displays the string 
stored in TALL. 
****************************************************************/ 
 
 
/*CIS2204 Sample problem #17 
Put YOUR name on the screen and display a terminating message. 
 
Create a class named FOLKS which contains one private data member 
which is an integer named SHORT. 
 
The class contains several public member functions. 
 
A public function named display() is used to display the data 
member. 
 
A destructor which displays the message "Object Destroyed." 
 
Constructor member functions are included such that any of 
the following declarations is valid: 
 
		FOLKS obj1(25); 
		FOLKS obj2; 
		FOLKS obj3("4321"); 
 
In the first case, a value of 25 is assigned to SHORT.  In the 
second case, a value of 0 (zero) is assigned to SHORT.  In the 
third case, the integer value 4321 is assigned to SHORT. 
 
Use automatic in-lining for all public functions. 
 
Provide a main() function which creates the three objects shown 
above. 
 
The main function passes each object in turn by reference to 
a function of your own design named ShowByRef() which displays 
the value stored in SHORT. 
***************************************************************/ 
 
 
/*CIS2204 Sample problem #18 
Put YOUR name on the screen with a terminating message. 
 
The following program defines a class named APPLE and creates 
an object of that class named PEACH.  The value of an integer 
stored in that object is set to 5. 
 
A second object named PLUM is created using the following 
statement: 
 
	APPLE PLUM = PEACH; 
 
The values stored in the two objects are then displayed, and as 
might be expected, they are the same. 
 
It was intended that it be possible to store different values 
in the two objects.  The value of PLUM is then changed to 7 and 
the values of the two objects are displayed again.  At this 
point, the values are both 7.  In other words, changing the 
value stored in PLUM causes the value stored in PEACH to 
change also which is the result of a programming error. 
 
Without modifying the statement which reads: 
 
	APPLE PLUM = PEACH; 
 
correct the program by adding a copy constructor so that 
the initial value in PLUM is the same as the value in PEACH, but 
changing the value stored in PLUM will not modify the value 
stored in PEACH. 
 
 
#include<iostream.h> 
#include<stdlib.h> 
class APPLE {	//begin class definition 
  int *p; 
public: 
  APPLE() { //constructor 
    p = new int; 
    if( !p )exit(1);	//exit on no allocated memory 
} //end constructor 
 
~APPLE() {	//destructor 
  delete p; 
  cout << "Destruction\n"; 
} //end destructor		 
... 
... 
void put(int j){*p = j;} //user interface function 
int get(){return *p;}  //user interface function 
 
};//end class definition 
 
 
main() 
{ 
	APPLE PEACH;	//create object of type APPLE 
	PEACH.put(5);  //put a value into the object 
 
	//create another object based on the object named PEACH 
	APPLE PLUM = PEACH;	//DO NOT MODIFY THIS STATEMENT 
 
  cout << "Display both objects\n"; 
	cout << "The object named PEACH contains " << PEACH.get() << "\n"; 
	cout << "The object named PLUM contains " << PLUM.get() << "\n"; 
 
	cout << "\nChanging the value of the object named PLUM\n\n"; 
	PLUM.put(7); 
 
  cout << "Display both objects again\n"; 
	cout << "The object named PEACH contains " << PEACH.get() << "\n"; 
	cout << "The object named PLUM contains " << PLUM.get() << "\n"; 
 
  cout << "\nTerminating, Dick Baldwin\n"; 
  return 0; 
}//end main()	 
 
****************************************************************/ 
 
 
/*CIS2204 Sample problem #19 
Put YOUR name on the screen and display a terminating message. 
 
Write a single function named display() which can be called 
in any of the following three ways.  When called, it will 
display the arguments in the order that they appear in the 
argument list. 
 
	display(3); 
	display(3,4); 
	display(3,4,5); 
 
Provide a main() function which demonstrates that the 
display() function works properly by making the three calls 
listed above. 
 
***********************************************************/ 
 
 
/*CIS2204 Sample problem #20 
Put YOUR name on the screen and display a terminating message. 
 
Create an overloaded function named display() which will 
accept either an integer or a float as an argument, 
display the argument, and report back which type of 
argument was used in the call to the function. 
 
Create a main() function to do the following: 
 
Create a pointer named pti which points to the integer 
version of the function and use this pointer to call the 
function with an argument of 4096. 
 
Create a pointer named ptf which points to the float 
version of the function and use this pointer to call the 
function with an argument of 3.14159. 
************************************************************/ 
 
 
/*CIS2204 Sample program #21 
Put YOUR name on the screen and display a terminating message. 
 
Create a class named TIME capable of storing a time interval 
in MINUTES and SECONDS as integers.  Provide a parameterized 
constructor for the class so that an object of the class 
can be declared and initialized using two arguments (MIN, SEC). 
Provide default values of 0 MINUTES and 0 SECONDS if no 
arguments are provided when the object is declared. 
 
Provide a member function named get_TIME() which returns the 
values of MINUTE and SECOND stored in an object of the TIME 
class as reference parameters. 
 
Overload the + operator for this class so that two objects 
can be added with a result being produced in MINUTES and 
SECONDS where the value of SECONDS is always less than 60. 
 
Create four objects having the following values: 
	TIME1 contains 2 MINUTES  8 SECONDS 
	TIME2 contains 2 MINUTES 10 SECONDS 
	TIME3 contains 4 MINUTES 44 SECONDS 
	TIME4 contains 0 MINUTES 0 SECONDS (default values) 
 
Test your program by displaying the result of the following 
expression: 
 
	TIME4 = TIME1 + TIME2 + TIME3; 
 
The result should be that TIME4 then contains 9 MINUTES and 
2 SECONDS. 
*************************************************************/ 
 
 
/*CIS2204 Sample problem #22 
Put YOUR name on the screen and display a terminating message. 
 
Create a class named LENGTH capable of storing a length 
in FEET and INCHES as integers.  Provide a parameterized 
constructor for the class so that an object of the class 
can be declared and initialized using two arguments (FT, IN). 
Provide default values of 0 FEET and 0 INCHES if no 
arguments are provided when the object is declared. 
 
Provide a member function named get_LENGTH() which returns the 
values of FOOT and INCH stored in an object of the LENGTH 
class as reference parameters. 
 
Overload the ++ operator for this class so that an object of 
the class can be incremented, meaning that the total number 
of INCHES stored in the object is increased by one (but 
the object continues to store the LENGTH in FEET and INCHES). 
Don't worry about the difference between prefix and 
postfix incrementing. 
 
Note that 1 FOOT and 11 INCHES incremented should result 
in 2 FEET and 0 INCHES. 
 
Overload the -- operator for this class so that an object of 
the class can be decremented, meaning that the total number 
of INCHES stored in the object is decreased by one (but 
the object continues to store the LENGTH in FEET and INCHES). 
Don't worry about the difference between prefix and 
postfix decrementing. 
 
Note that 2 FEET and 0 INCHES decremented should result 
in 1 FOOT and 11 INCHES. 
 
Provide a main function which requests a LENGTH in a 
	FOOT INCH 
format from the keyboard, stores the lengths in an object 
named ob1 when the object is created, displays the LENGTH 
stored in the object, increments the object, displays the 
LENGTH again, decrements the object, and displays the LENGTH 
again. 
*************************************************************/ 
 
 
/*CIS2204 Sample problem #23 
Put YOUR name on the screen and display a terminating message. 
 
Create a class named MyTime capable of storing an elapsed time 
in hours and minutes as integers.  Provide a parameterized 
constructor for the class so that an object of the class 
can be declared and initialized using two arguments (Hr, Min). 
Provide default values of 0 hours and 0 minutes if no 
arguments are provided when the object is declared. 
 
Provide a member function named get_MyTime() which returns the 
values of hour and minute stored in an object of the MyTime 
class as reference parameters. 
 
Overload the + operator for this class so that any of the 
following statements will execute properly, increasing the 
total number of minutes stored in the object, and continuing 
to store the time in hours and minutes. 
 
	MyObjA = MyObjB + 20; 
	MyObjA = 5 + MyObjB; 
	MyObjA = 5 + MyObjB + 20; 
 
For an input value of 1 hour and 59 minutes, the three 
statements above should produce the following outputs: 
 
	2 hours 19 minutes 
	2 hours 4 minutes 
	2 hours 24 minutes 
 
Provide a main function which requests a time in an 
	hour minute 
format from the keyboard, stores the times in an object 
named MyObjB when the object is created, displays the time 
stored in MyObjB, creates an object named MyObjA, executes the 
following statement: 
 
	MyObjA = MyObjB + 20; 
 
displays the time stored in MyObjA, executes the following 
statement: 
 
	MyObjA = 5 + MyObjB; 
 
displays the time stored in MyObjA again, executes the 
following statement: 
 
	MyObjA = 5 + MyObjB + 20; 
 
and displays the time stored in MyObjA again. 
**************************************************************/ 
 
 
 
/*CIS2204 Sample problem #24 
Put YOUR name on the screen and display a terminating message. 
 
Beginning with the following base class definition: 
 
class POP{	//create base class 
	char PopsName[21]; 
public: 
	void SetPopsName(char *ptr){strcpy(PopsName,ptr);} 
	void ShowPopsName() {cout << PopsName << "\n";} 
};  //end POP class definition 
 
create a derived class named son with the following members: 
 
	char SonName[21]; 
	void SetSonName(char *ptr){strcpy(SonName,ptr);} 
	void ShowSonName() {cout << SonName << "\n";} 
 
so that the following main() function will properly run 
allowing the name of the father and son to be set and 
displayed: 
 
main() 
{ 
	son family;			//create an object of type son named  
family 
	family.SetPopsName("John"); 
	family.SetSonName("Sam"); 
	cout << "Father's name: "; 
	family.ShowPopsName(); 
	cout << "Son's name:    "; 
	family.ShowSonName(); 
 
	cout << "\nTerminating, Dick Baldwin\n"; 
  return 0; 
}//end main() 
 
END COMMENTS     ***********************************************/ 
 
 
 
/*CIS2204 Sample problem #25 
Put YOUR name on the screen and display a terminating message. 
 
Beginning with the following base class definitions: 
 
class Pop{	//create base class 
	char PopsName[21]; 
public: 
	void ShowPopsName() {cout << PopsName << "\n";} 
	Pop(char *ptr){strcpy(PopsName,ptr);}//constructor 
};  //end Pop class definition 
 
class Mom{	//create base class 
	char MomsName[21]; 
public: 
	void ShowMomsName() {cout << MomsName << "\n";} 
	Mom(char *ptr){strcpy(MomsName,ptr);}//constructor 
};  //end Mom class definition 
 
 
create a derived class named Child which begins as follows: 
 
class Child : private Pop, private Mom{ 
	char ChildsName[21]; 
	... 
	... 
  ... 
};  //end Child class definition 
 
 
and which contains the necessary additional members so that 
the following main() function will properly run, allowing 
the name of the child, father, and mother to be set and 
displayed: 
 
main() 
{ 
	Child family("Harry","Bill","Mary"); 
	cout << "Child's, Father's, and Mother's names are:\n"; 
	family.ShowFamily(); 
 
	cout << "\nTerminating, Dick Baldwin\n"; 
  return 0; 
}//end main() 
 
END COMMENTS     ********************************************/ 
 
 
 
-end of sample test -. 

