Oop - 01
Oop - 01
Pointer Basics: Write a program to declare an integer variable, assign it a value, and then use a
pointer to access and print the value of the variable as well as its memory address.
#include <iostream>
using namespace std;
int main() {
int num = 10;
int *ptrNum;
ptrNum = #
cout << "The value of num is: " << *ptrNum << endl;
cout << "Memory address of num is: " << ptrNum << endl;
return 0;
}
2. Dereferencing a Pointer: Write a program that declares a pointer to an integer, assigns the address
of a variable to it, modifies the value of the variable using the pointer, and prints the modified value.
#include <iostream>
using namespace std;
int main() {
int a = 10;
int* ptrA = &a;
return 0;
}
3. Pointer Arithmetic: Create a program that declares an array of integers. Use a pointer to traverse
the array and print each element by incrementing the pointer.
#include <iostream>
using namespace std;
int main() {
int arr[] = { 10, 20, 30, 40, 50 };
int size = sizeof(arr) / sizeof(arr[0]);
int* ptrArr = arr;
return 0;
}
4. Pointer and Arrays: Write a program that dynamically allocates memory for an array of
integers, takes user input for the size of the array and its elements, and then prints the elements using
pointer arithmetic.
#include <iostream>
using namespace std;
int main() {
int size;
5. Pointer to Function: Implement a simple function that swaps two integers using pointers. Write a
swap function that accepts pointers as arguments, and swap the values of two variables in the main
function.
#include <iostream>
using namespace std;
void swapf(int*, int*);
int main() {
int num1 = 10;
int num2 = 20;
cout << "Before swap function.\n";
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
swapf(&num1, &num2);
arrP(arr1, size);
return 0;
}
7. Dynamic Memory Allocation: Write a program that dynamically allocates memory for storing the
grades of students, takes input from the user for the number of students and their grades, and then
calculates and prints the average grade.
#include <iostream>
using namespace std;
int main() {
int noOfStu;
int sum;
double avg;
sum = 0;
for (int i = 0; i < noOfStu; i++) {
sum += grade[i];
}
return 0;
}
8. Pointer to Pointer: Write a program that demonstrates the use of a pointer to a pointer. Declare an
integer variable and a pointer to it. Then, declare another pointer that stores the address of the first
pointer, and use this to print the value of the integer.
#include <iostream>
using namespace std;
int main() {
int a = 10;
int* ptra = &a;
int** ptrTOptra = &ptra;
9. Void Pointer: Create a program that demonstrates the use of a void pointer. Declare variables of
different types (int, float, char), and assign their addresses to the void pointer. Use typecasting to
print the values stored at the addresses pointed to by the void pointer.
#include <iostream>
using namespace std;
int main() {
int a = 10;
float b = 2.65;
char name = 'b';
cout << "a = " << *(int*)ptrA << "\nb = " << *(int*)ptrB << "\nname = " <<
*(int*)ptrName << endl;
return 0;
}
10. Dynamic 2D Array Using Pointers: Write a program that dynamically allocates a 2D array using
pointers. Ask the user for the number of rows and columns, take input for each element, and then
print the entire 2D array.
#include <iostream>
using namespace std;
int main() {
int** array;
int r, c;
// Deallocate memory
for (int i = 0; i < r; i++) {
delete[] array[i];
}
delete[] array;
return 0;
}