C++ Object-Oriented Programming: Lab Exercise One
- Dynamic Memory Allocation and Pointer Vairable Example
Input and run the program, observe the output, and explain the purpose of each line.
#include<iostream>
using namespace std;
int main(){
int *ptr, index;
ptr = new int[5];
if (ptr == NULL)
exit(0);
*(ptr + 1) = 3;
for (index = 0; index < 5; index++)
cout << *(ptr + index) << " ";
delete[] ptr;
return 0;
}
1.1 Program Analysis
int *ptr, index; // Declare an integer pointer and an integer variable
ptr = new int[5]; // Allocate memory for five integers dynamically
if (ptr == NULL) // Check if memory allocation was successful
exit(0); // Exit if allocation failed
*(ptr + 1) = 3; // Assign value 3 to the second element of the array
for (index = 0; index < 5; index++) // Loop through all elements in the array
cout << *(ptr + index) << " "; // Output each element with a space separator
delete[] ptr; // Free the dynamically allocated memory
return 0; // End the program
1.2 Output
-842150451 3 -842150451 -842150451 -842150451
- Reference Definition and Usage Example
Input and run the program, observe the output, and explain the purpose of each line.
#include<iostream>
using namespace std;
int main(){
int num = 10;
int &ref = num;
cout << "num=" << num << endl;
cout << "ref=" << ref << endl;
cout << "&num=" << &num << endl;
cout << "&ref=" << &ref << endl;
return 0;
}
2.1 Program Analysis
int num = 10; // Define an integer variable and assign it a value of 10
int &ref = num; // Define ref as a reference to num
cout << "num=" << num << endl; // Output the value of num
cout << "ref=" << ref << endl; // Output the value of ref
cout << "&num=" << &num << endl; // Output the address of num
cout << "&ref=" << &ref << endl; // Output the address of ref
return 0; // End the program
2.2 Output
num=10
ref=10
&num=00B3FE9C
&ref=00B3FE9C
- Pointer to Structure Variable Example
Input and run the program, observe the output, and explain the purpose of each line.
#include<iostream>
using namespace std;
struct student{
long int id;
char name[4];
float grade;
};
int main(){
student data = {89031, "LiL", 96};
student *ptr = &data;
cout << data.id << " " << data.name << " " << data.grade << endl;
cout << ptr->id << " " << ptr->name << " " << ptr->grade << endl;
cout << (*ptr).id << " " << (*ptr).name << " " << (*ptr).grade << endl;
return 0;
}
3.1 Program Analysis
struct student{ // Define a student structure
long int id; // Define a long integer variable for ID
char name[4]; // Define a character array for name
float grade; // Define a float variable for grade
};
int main(){
student data = {89031, "LiL", 96}; // Define and initialize a structure variable
student *ptr = &data; // Define a pointer to the structure
cout << data.id << " " << data.name << " " << data.grade << endl; // Access structure members
cout << ptr->id << " " << ptr->name << " " << ptr->grade << endl; // Use arrow operator to access members
cout << (*ptr).id << " " << (*ptr).name << " " << (*ptr).grade << endl; // Use dereference operator to access members
return 0;
3.2 Output
89031 LiL 96
89031 LiL 96
89031 LiL 96
- Program Design
Define a dynamic integer array (length provided by user), loop to assign values to array elements. Use pointers to swap the smallest value with the first element and the largest value with the last element. Output the modified array.
4.1 Source Code
#include<iostream>
using namespace std;
int main(){
int size;
cout << "Array length: ";
cin >> size;
int *array = new int[size];
cout << "Enter " << size << " numbers";
for (int i = 0; i < size; i++) {
cin >> array[i];
}
int *minVal = array;
int *maxVal = array;
for (int *p = array + 1; p < array + size; p++) {
if (*p < *minVal) {
minVal = p;
}
if (*p > *maxVal) {
maxVal = p;
}
}
if (minVal != array) {
int temp = *array;
*array = *minVal;
*minVal = temp;
}
if (maxVal != array + size - 1) {
int temp = *(array + size - 1);
*(array + size - 1) = *maxVal;
*maxVal = temp;
}
cout << "Output: ";
for (int *p = array; p < array + size; p++) {
cout << *p << " ";
}
cout << endl;
delete[] array;
return 0;
}
4.2 Program Analysis
First, define a variable for the array size, then input the speicfic length. Dynamically allocate memory for the array. Then read the specified number of elements into the array. Define pointers for the minimum and maximum values, traverse the array to find the corresponding elements. If the minimum is not at the first position, swap it with the first element. If the maximum is not at the last position, swap it with the last element. After swapping, use a pointer to print out all the elements. Finally, free the dynamically allocated memory and return from the function.