Oop Lab 3
Oop Lab 3
Oop Lab 3
Creating Pointers
You learned from the previous chapter, that we can get the memory
address of a variable by using the & operator:
Example
string food = "Pizza"; // A food variable of type string
A pointer variable points to a data type (like int or string) of the same type, and
is created with the * operator. The address of the variable you're working with is
assigned to the pointer:
Example
string food = "Pizza"; // A food variable of type string
string* ptr = &food; // A pointer variable, with the name ptr, that
stores the address of food
Example explained
Create a pointer variable with the name ptr, that points to a string variable, by
using the asterisk sign * (string* ptr). Note that the type of the pointer has to
match the type of the variable you're working with.
Use the & operator to store the memory address of the variable called food, and
assign it to the pointer.
Superior University Lahore, Faisalabad Campus
Now, ptr holds the value of food's memory address.
Tip: There are three ways to declare pointer variables, but the first way is
preferred:
Pointers are variables that store the address of another variable or a memory location.
They are used for various purposes, such as:
Here is a list of headings with coding examples for some of the uses of pointers:
// main function
int main()
{
// declare two integers
int x = 10;
Superior University Lahore, Faisalabad Campus
int y = 20;
// output:
// x = 10, y = 20
// x = 20, y = 10
return 0;
}
// assign the address of the first element of the array to the pointer
ptr = arr;
// output: 10 20 30 40 50
// main function
int main()
{
// declare two integers
int x = 5;
int y = 10;
return 0;
}
Superior University Lahore, Faisalabad Campus
1. Task 1: Write a C++ program that uses pointers to swap the values of two variables. Prompt the
user to input two integers and then use pointers to swap the values.
2. Task 2: Write a C++ program that uses pointers to allocate dynamic memory for an array of
integers. Prompt the user to input the size of the array, and then use pointers to allocate
memory for the array. Once the memory is allocated, prompt the user to input values for the
array elements and then display the contents of the array.
3. Task 3: Write a C++ program that uses pointers to find the maximum value in an array of
integers. Prompt the user to input the size of the array, allocate dynamic memory for the array,
prompt the user to input values for the array elements, and then use pointers to find the
maximum value in the array.
4. Task 4: Write a C++ program that uses pointers to create a linked list. The program should
prompt the user to input values for each node in the list and then display the contents of the
list.
5. Task 5: Write a C++ program that uses pointers to implement a stack. The program should
prompt the user to input values to push onto the stack or pop from the stack, and then display
the contents of the stack after each operation.
Task 1: Write a C++ program that uses pointers to swap the values of two variables. Prompt the user to
input two integers and then use pointers to swap the values.
#include <iostream>
*a = *b;
*b = temp;
}
Superior University Lahore, Faisalabad Campus
int main()
cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
swap(&num1, &num2);
cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
return 0;
Task 2: Write a C++ program that uses pointers to allocate dynamic memory for an array of integers.
Prompt the user to input the size of the array, and then use pointers to allocate memory for the array.
Once the memory is allocated, prompt the user to input values for the array elements and then display
the contents of the array.
#include <iostream>
int main()
int size;
Superior University Lahore, Faisalabad Campus
cout << "Enter the size of the array: ";
cout << "Enter " << size << " integers: ";
delete[] arr;
return 0;
Task 3: Write a C++ program that uses pointers to find the maximum value in an array of integers.
Prompt the user to input the size of the array, allocate dynamic memory for the array, prompt the user
to input values for the array elements, and then use pointers to find the maximum value in the array.
#include <iostream>
Superior University Lahore, Faisalabad Campus
using namespace std;
return max;
int main()
int size;
cout << "Enter " << size << " integers: ";
cout << "The maximum value in the array is: " << max << endl;
delete[] arr;
return 0;
Task 4: Write a C++ program that uses pointers to create a linked list. The program should prompt the
user to input values for each node in the list and then display the contents of the list.
#include <iostream>
struct Node
int data;
Node* next;
};
newNode->data = data;
newNode->next = *head;
Superior University Lahore, Faisalabad Campus
*head = newNode;
head = head->next;
int main()
cout << "Enter the data for node " << i + 1 << ": ";
addNode(&head, data);
displayList(head);
return 0;
Task 5: Write a C++ program that uses pointers to implement a stack. The program should prompt the
user to input values to push onto the stack or pop from the stack, and then display the contents of the
stack after each operation.
#include <iostream>
class Stack
private:
int top;
int data[MAXSIZE];
public:
Stack()
top = -1;
}
Superior University Lahore, Faisalabad Campus
bool push(int item)
return false;
else
top++;
data[top] = item;
return true;
if (top < 0)
return false;
else
item = data[top];
top--;
return true;
}
Superior University Lahore, Faisalabad Campus
void display()
if (top < 0)
else
};
int main()
Stack myStack;
cout << "1. Push an item onto the stack" << endl;
cout << "2. Pop an item from the stack" << endl;
do
switch (choice)
case 1:
if (myStack.push(value))
break;
case 2:
if (myStack.pop(value))
cout << "Item popped from stack: " << value << endl;
break;
case 3:
Superior University Lahore, Faisalabad Campus
myStack.display();
break;
case 4:
break;
default:
break;
return 0;