Understanding object-oriented programming will allow you to understand modern computer codes better and thus make you a more well-rounded engineer.
part_process
function. In particular, create a class
part_object
with the following private
data:
steel_content
, brass_content
, and
material_cost
. The class should also have a public
function called process
which does the same work as
part_process
did. Of course, process
uses the
private data of the object.
In main
, create an object of type part_object
called
part
. Use this object's process
function to do the
processing of a part when 'P' is selected from the menu. (25%)
part_name
, in its private
data, and a function display
in its public data. Function
display
needs to print out the data of the part object on
the screen. Now in main
, behind the while
loop that processes the menu choices, print out: ``Thank you for
using this program. The last part you processed was:''. Then
invoke part.display
to display the latest part data.
Finally write to the screen: ``Please come again.'' (25%)
Hint: To input the part's name, use the following code:
cout << "Enter the part's name: " << flush; cin >> ws; cin.getline(part_name, sizeof(part_name));
Select the best answer for all questions below:
main
may use.
main
and the member functions
of the object may use.
main
may use.
main
and the member functions
of the object may use.
Select the best answer for all questions below:
float vector[3];
int primes[3];
primes[0]=1;
primes[1]=2;
primes[2]=3;
primes[3]=5;
primes[0]
does not exist.
primes[3]
does not exist.
primes[3]
exists.
cout << primes[3] << ' ';
if (i<=3) cout << primes[3] << ' ';
if (i<=3) cout << primes[i] << ' ';
for (i=0; i<3; i++) cout << primes[3] << ' ';
for (i=0; i<3; i++) cout << primes[i] << ' ';
i=0; while (i<3) cout << primes[++i] << ' ';
i=1; while (i<3) cout << primes[i++] << ' ';
The following program is intended to print out the primes ``1 2 3 5 7 11 13 17 19 23'':
#include <iostream.h> void print_primes(int array[], int size) { int i; for (i=0; i<size; i++) cout << array[i] << " "; } void main(void) { int primes[10] = {1, 2, 3, 5, 7, 11, 13, 17, 19, 23}; print_primes(primes, 10); }
array
'' in print_primes
must be
replaced by ``primes
''.
array
'' in print_primes
could be
replaced by ``primes
''.
array
'' in print_primes
cannot be
replaced by ``primes
''.
print_primes
,
[]
.
[]
.
[]
.
print_primes
above, the loop
for (i=0; i<size; i++) cout << array[i] << " ";
for (i=1; i<=size; i++) cout << array[i-1] << " ";
i=0; while (i<size) cout << array[i++] << " ";
[0]
;
NULL is stored in location [10]
[0]
,
but the last location is [9]
; there is no place for NULL.
print_primes(primes, ...
print_primes(primes[], ...
print_primes(primes[0], ...
print_primes(primes[10], ...;
print_primes(primes, 10);
char the_great[3]="LvD";
char the_great[4]="LvD";
char the_great[5]="LvD";
char the_great[]="LvD";
for (i=0; the_great[i]; i++) cout << the_great[i] << ' ';
for (i=0; the_great[i]; i++);
for (digit='0', i=0; digit<='9'; digit++) string[i++]=digit';
string
.
name[0]='L'; name[1]='v'; name[2]='D'
set the
string name
equal to ``LvD''?
name[3]=NULL
.
Consider the following class declaration:
class student_object { private: char name[30]; float GPA; void input_name(void); void input_GPA(void); public: void input_data(void); void cout_name(void) {cout << name;}; float get_GPA(void) {return GPA;}; }
student_object
void input_name();
student_object
.
student_object
. The function definition will be
elsewhere.
void cout_name();
student_object
.
student_object
. The function definition will be
elsewhere.
GPA
can be used
main
.
grade_object
get_GPA
can be used
main
.
grade_object
main
, the declaration student_object
student1,
student2;
float
,
int
, or char
, but not student_object
!
main
, cin >> student_object.GPA;
student_object
is just a description:
you first need to create an actual student_object
.
main
, cin >> student1.GPA;
main
.
main
since student_object
student1
was created by
main
.
main
, to print out student1
's name, use:
cout << student1.name;
cout << student1.name[];
student1.cout_name();
cout << student1.cout_name();
main
, to print out student2
's GPA, use:
cout << GPA;
cout << student2.GPA;
student2.get_GPA();
cout << student2.get_GPA();
void:
process_student
(student_object
student)
the
statement if (student.GPA > 2.) cout << ``Good student'';
void:
process_student
(student_object
student)
the
statement if (student.get_GPA > 2.) cout << ``Good student'';
aluminum_cost
and
steel_cost
in dollars and cents, in the form
$ddd.cc
. But if they are still undefined (the same value
they were initialized at), print ``Undefined''. Test your program
with $1.00, $1.2345 (to print as $1.23), $10.00, $12.3456,
and $0.0345. Hint: Use the I/O manipulator
setiosflags(ios::fixed)
to set non-scientific notation.
This one is used the same way as other I/O manipulators that
are discussed in the book. It is turned off by
resetiosflags(ios::fixed)
.