Advanced Programming with C++

ETE 312

Course Description:

Introduction to C++ including console input/output, file input/output, function overloading, class structures, arrays, composition, single and multiple inheritance, virtual functions; and techniques for building class libraries.

Using recursive functions to manipulate arrays.        2

Program that calls functions using a pointer array.        2

To perform arithmetic operations in classes using operator overloading techniques.        3

Lab 7        3


Using recursive functions to manipulate arrays.

First we include standard header. Next we declare the prototype of the function linear search with the elements as an array and an integer. We set a constant integer to 100  which sets the size of the array to 100 elements. We than declare an array, search key which is the number we are looking for in the array, and element which is the array index that we found our search key. We populate the array with numbers starting at 0 and increasing by multiples of 2. We than ask the user to enter a number they would like to search for in the array. We than set the variable element to equal the result of our linear search function. This function checks to every index of our array to see if the number we are searching matches. If the number matches one of our arrays we return the index number other wise we return -1. If the search matches and the index number is returned we than print found value in element with the corresponding index number. Otherwise we print value not found.

Program that calls functions using a pointer array.

 Initially we were given the data of studentGrades as an array. Also a list of options the user can choose: row sum, column sum, average, print array. We had to also set up an pointer array to call the functions. Also given to us is the pointer array pre defined to our functions, as well as the code for the users choice. We have to write each of the functions. In this we were given the data of studentGrades as an array.  We had to also set up an pointer array to call the functions: row sum, column sum, average, print array. The function row sum used nested for loop to call the students and then each exam within the student at which it added and stored the value in a temp variable untill finished at which time it returned the value. The column sum was very similar in that it used a nested for statement however it ran throguh exam than student. The average function simply used a nested for inorder to sum all the elements of the array studentGrades and devides it by the total number of elements. Lastly the function Print array simply prints the elements of studentGrades. We also had to print the menu and retrieve the users option which than was passed to the array processGrade which was a pointer that pointed to each of the coressponding funcion.

This programming exercise is designed to give students some experience

in writing object-oriented programs using arrays as private data members

of the class.

Create a class Rectangle, which stores only Cartesian coordinates of four

corners of a rectangle as arrays shown below:

p4[x4,y4] p3[x3,y3]

p1[x1,y1] p2[x2,y2]

The constructor calls a set function that accepts four sets of coordinates.

The set function verifies that the supplied coordinates do, in fact, form a

rectangle.

Provide member functions that calculate length, width, perimeter, and

area. The length is larger of the two dimensions. Include a predicate

function square that determines whether the rectangle is a square.

Write a program for the above described rectangle class, compile it,

Execute it, and demonstrate to your instructor.First I declared the class and defined my point system where point[point number][x and y coordinates] Next I wrote the set function in which i ran a for loop in order to get the inputs for each point from the user. I Then output the points the user has enter so they can verify their inputs. The set function also checks to see whether the points entered form a rectangle by checking p1.x = p4.x & p1.y = p2.y & p2.x = p3.x & p3.y = p4. We than calculate length and width functions and use these functions within perimeter, area and square to get their results. Thus we than print out our results.

To perform arithmetic operations in classes using operator overloading techniques.

First we had to define a complex class with private and public members using operator-overloading techniques to add, subtract, multiply, and divide complex numbers. The overloaded operators we are using were the “+”,”-”,”*”,”/”, we also had to define constructors. Each of the functions I simply used a return operator with the required data for each of the operations. Using the operator-overloading technique allow us to use “+”,”-”,”*”,”/”, with our new class complex. That way we do not have to call a function to perform the operations of basic operators. We tie the operator signs to functions thus overloading-operators.

Lab 7

In this lab we were to use multiple inheritance using three classes: resident, student, employee. Where resident is our base class and student and employee are derived from resident. In our base class we have name and SSN also a set data function and a show data function. In the student class we inherit both the data and the function which we than add an additional data which is student number. We use the same functions using inheritance from the resident class of the previous class as well as getting and showing the new data. In the Employee class  we inherit both the data and the functions from the resident class which we than add an additional data which is employee number and salary. We use the same functions using inheritance of the previous class as well as getting and showing the new data.