next up previous
Up: Return

Object-Oriented Programming

Group: Captain: Recorder: Reflector:

Motivation

In order to handle more complex and larger programs, object oriented programming is needed. It allows data to be protected and computer code to be reused better. Much current code is written in an object setting, including C++, Windows, Java programs.

Understanding object-oriented programming will allow you to understand modern computer codes better and thus make you a more well-rounded engineer.

Objectives

1.
Learn about arrays.
2.
Learn about objects.
3.
Learn about character strings.
4.
Improve program neatness.
5.
Strengthen previous knowledge.

Performance Criteria

1.
Success in modifying your program so that an object replaces the 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%)

2.
Success in modifying your program so that the part object also includes the name of the part, 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));

3.
The neatness, clarity, and simplicity of your implementation of your program. (20%)

4.
The knowledge demonstrated by your definitions of the vocabulary. (10%)

5.
The depth of understanding demonstrated by your answers to the critical thinking questions. (20%)

Resources

1.
Your class notes
2.
The course web page.
3.
The textbook, especially ``Storing Multiple Values in Arrays'', ``Understanding Character Strings'', and ``Starting with C++ Classes''.
4.
The online help facilities of Windows, Netscape or Internet Explorer, and the C++ compiler.

Plan

1.
Skim through the lessons ``Storing Multiple Values in Arrays'', ``Understanding Character Strings'', and ``Starting with C++ Classes''.
2.
Do the programming tasks that are performance criteria.
3.
Define the vocabulary.
4.
Answer the critical thinking questions.

Vocabulary

Select the best answer for all questions below:

1.
Array
(a)
A data structure that allows a single storage location to store multiple values.
(b)
A set of storage locations with a common name.
(c)
A matrix.

2.
Index
(a)
A pointer to a memory location in an accessible part of the declared memory.
(b)
An integer that selects a particular element of an array.
(c)
A table of contents of a project.

3.
Character strings
(a)
An array of characters.
(b)
A string of numbers.
(c)
A string of letters and numbers.

4.
Object
(a)
A thing, such as a telephone.
(b)
Code and data packaged together.
(c)
An objection to code by the compiler.

5.
Classes.
(a)
The subdivision of C++ programming structures into groups of related constructs.
(b)
The C++ implementation of objects.
(c)
Courses you take at school.

6.
Member functions
(a)
Hmmmmmm....
(b)
Functions that are member of a program
(c)
Functions that belong to an object

7.
Private data.
(a)
A soldier's statistics
(b)
A bathroom's statistics
(c)
Data in an object that the object may use.
(d)
Data in an object that the member functions of the object may use.
(e)
Data in an object that main may use.
(f)
Data in an object that main and the member functions of the object may use.

8.
Public data:
(a)
The government's statistics
(b)
Data in an object that the object may use.
(c)
Data in an object that the member functions of the object may use.
(d)
Data in an object that main may use.
(e)
Data in an object that main and the member functions of the object may use.

Critical Thinking Questions

Select the best answer for all questions below:

1.
float vector[3];
(a)
declares storage for 3 floating point numbers.
(b)
declares storage for 3 vectors.
(c)
declares storage for 2 floating point numbers since the last one is NULL.
(d)
declares storage for 2 vectors since the last one is NULL.

2.
int primes[3]; primes[0]=1; primes[1]=2; primes[2]=3; primes[3]=5;
(a)
is legal.
(b)
is illegal; primes[0] does not exist.
(c)
is illegal; primes[3] does not exist.
(d)
is illegal; only primes[3] exists.
(e)
is illegal; an index is needed within the brackets, not an integer.

3.
To print out the primes from the previous question, use
(a)
cout << primes[3] << ' ';
(b)
if (i<=3) cout << primes[3] << ' ';
(c)
if (i<=3) cout << primes[i] << ' ';
(d)
for (i=0; i<3; i++) cout << primes[3] << ' ';
(e)
for (i=0; i<3; i++) cout << primes[i] << ' ';
(f)
i=0; while (i<3) cout << primes[++i] << ' ';
(g)
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);
   }

4.
In the above program,
(a)
the name ``array'' in print_primes must be replaced by ``primes''.
(b)
the name ``array'' in print_primes could be replaced by ``primes''.
(c)
the name ``array'' in print_primes cannot be replaced by ``primes''.

5.
In the parameter list in the definition of print_primes,
(a)
10 must be inserted within [].
(b)
10 can be inserted within [].
(c)
10 cannot be inserted within [].

6.
In the definition of print_primes above, the loop
   for (i=0; i<size; i++) cout << array[i] << " ";
(a)
should be
   for (i=1; i<=size; i++) cout << array[i-1] << " ";
(b)
should be
   i=0; while (i<size) cout << array[i++] << " ";
(c)
could be any of these

7.
In the declaration of the array of primes, 10 primes are stored.
(a)
That is OK.
(b)
That is wrong; the last storage location must be NULL.
(c)
That is OK; the first storage location is numbered [0]; NULL is stored in location [10]
(d)
That is wrong; the first storage location is numbered [0], but the last location is [9]; there is no place for NULL.

8.
The call print_primes(primes, ...
(a)
is OK.
(b)
should be print_primes(primes[], ...
(c)
should be print_primes(primes[0], ...
(d)
should be print_primes(primes[10], ...;

9.
The 10 in print_primes(primes, 10);
(a)
is OK.
(b)
should be 9.
(c)
should be 11.

10.
Which declaration is incorrect:
(a)
char the_great[3]="LvD";
(b)
char the_great[4]="LvD";
(c)
char the_great[5]="LvD";
(d)
char the_great[]="LvD";

11.
For that example,
for (i=0; the_great[i]; i++) cout << the_great[i] << ' ';
(a)
will not compile: a condition is needed after the first semi-colon.
(b)
prints out ``LvD''
(c)
prints out ``L v D ''

12.
For that example, for (i=0; the_great[i]; i++);
(a)
does nothing, the statement is empty.
(b)
outputs ``LvD'' to the screen.
(c)
counts the number of letters in ``LvD''.
(d)
counts one more.

13.
for (digit='0', i=0; digit<='9'; digit++) string[i++]=digit';
(a)
is wrong; you cannot increment characters.
(b)
puts the characters '0' through '9' in the first ten positions of string.
(c)
Creates the character string ``0123456789''.

14.
Does name[0]='L'; name[1]='v'; name[2]='D' set the string name equal to ``LvD''?
(a)
No, you need to close the string ``LvD'' with name[3]=NULL.
(b)
Yes. The computer will add NULL to the end of the string automatically.

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;};
    }

15.
Class student_object
(a)
is illegal. An object is a thing, not a person.
(b)
is legal.

16.
void input_name();
(a)
is illegal; a function cannot be within brackets.
(b)
is the definition of a private member function of class student_object.
(c)
is the prototype of a private member function of class student_object. The function definition will be elsewhere.

17.
void cout_name();
(a)
is illegal; a function cannot be within brackets.
(b)
is the definition of a public member function of class student_object.
(c)
is the prototype of a public member function of class student_object. The function definition will be elsewhere.

18.
Variable GPA can be used
(a)
by function main.
(b)
by the member functions of grade_object
(c)
by both
(d)
by neither

19.
Function get_GPA can be used
(a)
by function main.
(b)
by the member functions of grade_object
(c)
by both
(d)
by neither

20.
Inside main, the declaration student_object student1, student2;
(a)
is wrong. You can declare things to be float, int, or char, but not student_object!
(b)
creates storage for two floating point numbers.
(c)
creates two students which must have the same name and GPA.

21.
Inside main, cin >> student_object.GPA;
(a)
is correct.
(b)
is illegal; student_object is just a description: you first need to create an actual student_object.

22.
Inside main, cin >> student1.GPA;
(a)
is illegal: GPA is private.
(b)
is correct: private data can be used inside main.
(c)
is correct: private data can be used inside main since student_object student1 was created by main.

23.
Inside main, to print out student1's name, use:
(a)
cout << student1.name;
(b)
cout << student1.name[];
(c)
student1.cout_name();
(d)
cout << student1.cout_name();

24.
Inside main, to print out student2's GPA, use:
(a)
cout << GPA;
(b)
cout << student2.GPA;
(c)
student2.get_GPA();
(d)
cout << student2.get_GPA();

25.
Inside a function with prototype void: process_student (student_object student) the statement if (student.GPA > 2.) cout << ``Good student'';
(a)
is legal.
(b)
is illegal.

26.
Inside a function with prototype void: process_student (student_object student) the statement if (student.get_GPA > 2.) cout << ``Good student'';
(a)
is legal.
(b)
is illegal.

Exercises

1.
Change the program so that the parts can include aluminum. Make all appropriate changes. Hint: Learn the search and cut and paste features of your editor.

2.
Change the program so that it includes an inventory (how much is in stock) of each part. The user should be able to display or change the inventory. Hint: Read the program carefully.

3.
In the menu, print out the values of 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).

Evaluation

1.
Give two strengths of your group.
2.
Indicate two areas for improvement.
3.
What two new insights did your group obtain?
4.
Give two strengths of the class.
5.
Indicate two areas for improvement of the class.
6.
What two new insights did you obtain about the class?

next up previous
Up: Return
Author: Leon van Dommelen