OOP Notes
OOP Notes
void modifyValue(int x) {
x = 20; // Changes the local copy of 'x'
}
int main() {
int num = 10;
modifyValue(num);
cout << num; // Output: 10 (original 'num' is unchanged)
return 0;
}
Pass by Reference:
void modifyReference(int& x) {
x = 20; // Changes the original 'x'
}
int main() {
int num = 10;
modifyReference(num);
cout << num; // Output: 20 (original 'num' is modified)
return 0;
}
Pass a variable by reference using pointers
void modifyWithPointer(int* x) {
*x = 20; // Changes the original variable pointed to by 'x'
}
int main() {
int num = 10;
modifyWithPointer(&num);
cout << num; // Output: 20 (original 'num' is modified)
return 0;
}
Single Pointer
int Value = 42;
int *singlePtr = &Value;
Double Pointer
int **doublePtr = & singlePtr;
Triple Pointer
int ***triplePtr = & doublePtr;
// Pointer to int // Pointer to char
int intValue = 10; char charValue = 'A';
int *intPtr = &intValue; char *charPtr = &charValue;
// Accessing elements using the pointer // Accessing elements using the array name & [ ]
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
cout << char2DArrayPtr[i] << endl; for (int j = 0; j < 5; j++) {
} cout << char2DArray[i][j] << endl;
}
// Accessing elements using pointer name & [
] & pointer arithmetic // Accessing elements using arrayname & [ ] &
for (int i = 0; i < 2; i++) { pointer arithmetic
for (int j = 0; j < 5; j++) { for (int i = 0; i < 2; i++) {
cout << *(char2DArrayPtr[i] + j); for (int j = 0; j < 5; j++) {
} cout << *(char2DArrayPtr[i] + j);
cout << endl; }
} cout << endl;
}
// Accessing elements using pointer
arithmetic // Accessing elements using pointer arithmetic
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
for (int j = 0; j < 5; j++) { for (int j = 0; j < 5; j++) {
cout << *(*(char2DArrayPtr + i) + j); cout << *(*(char2DArrayPtr + i) + j);
} }
cout << endl; cout << endl;
} }
Pointers Array
int arr1[] = {1, 2, 3};
int arr2[] = {4, 5, 6};
int arr3[] = {7, 8, 9};
// Example of using the pointer array // Example of using pointer notation to print
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) { for (int j = 0; j < 3; j++) {
cout << ptrArray[i][j] << " "; cout<< *(*(ptrArray + i) + j) << " ";
} }
cout << endl; cout << std::endl;
} }
*(*(ptrArray+i)+j) is equivalent to
ptrArray[i][j]
// DMA 1D int array Allocate and initialize a dynamic
int *dma1DArray = new int[5]; 1D integer array using a loop
dma1DArray[0] = 10;
dma1DArray[1] = 20; int main() {
dma1DArray[2] = 30; int size = 5;
dma1DArray[3] = 40; int *dma1DArray = new int[size];
dma1DArray[4] = 50;
delete[] dma1DArray; for (int i = 0; i < size; i++) {
dma1DArray[i] = (i + 1) * 10;
}
return 0;
}
// DMA 2D int array allocate, initialize, and deallocate a dynamic 2D
int **dma2DArray = new int *[2]; integer array using loops
dma2DArray[0] = new int[3]; int main() {
dma2DArray[1] = new int[3]; int rows = 2; int cols = 3;
dma2DArray[0][0] = 1; int **dma2DArray = new int *[rows];
dma2DArray[0][1] = 2; for (int i = 0; i < rows; i++) {
dma2DArray[0][2] = 3; dma2DArray[i] = new int[cols];
dma2DArray[1][0] = 4; }
dma2DArray[1][1] = 5;
dma2DArray[1][2] = 6; int value = 1;
delete[] dma2DArray[0]; for (int i = 0; i < rows; i++) {
delete[] dma2DArray[1]; for (int j = 0; j < cols; j++) {
delete[] dma2DArray; dma2DArray[i][j] = value;
value++;
}
}
int main() {
// Function returning a pointer
int* ptr1 = returnPointer();
cout << "Value returned by function: " << *ptr1 << endl;
delete ptr1;
return 0;
}
// Swap values using pointers
int x = 5;
int y = 7;
int* xPtr = &x;
int* yPtr = &y;
cout << "Swapped values: x = " << *xPtr << ", y = " << *yPtr << endl;
Encapsulate the allocation, initialization, printing, and deallocation of dynamic arrays
into functions for better code organization
int main() {
//allocation, initialization
int* dma1DArray = createAndInitialize1DArray(5);
int** dma2DArray = createAndInitialize2DArray(2, 3);
//printing
print1DArray(dma1DArray, 5);
print2DArray(dma2DArray, 2, 3);
//deallocation
delete1DArray(dma1DArray);
delete2DArray(dma2DArray, 2);
return 0;
}
// Function to allocate and initialize a 1D dynamic array
int* createAndInitialize1DArray(int size) {
int* array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = (i + 1) * 10;
}
return array;
}
▪ Structures are useful for defining complex data types that represent real-
world entities, such as a Student, Car, or Book.
Syntax:
struct StructureName {
data_type member1;
data_type member2;
...
};
Example:
struct Student {
string name;
int age;
float marks;
};
This structure defines a Student with three
attributes: name, age, and marks.
Example:
struct Student {
string name;
int age;
int age;
for (int i = 0; i < 2; i++) {
float marks;
cout << "Student " << i+1 << ": " << students[i].name
};
<< ", Age: " << students[i].age
<< ", Marks: " << students[i].marks << endl;
}
return 0;
}
6. Pointer to Structure
We can use pointers to access structure members.
Example:
struct Student { int main() {
string name; Student s1 = {"Ali", 20, 85.5};
int age; Student* ptr = &s1;
float marks;
}; // Accessing structure members using pointer
cout << "Name: " << ptr->name << endl;
cout << "Age: " << ptr->age << endl;
cout << "Marks: " << ptr->marks << endl;
return 0;
}
Here, ptr->name is the same as (*ptr).name.
7. Structure Object Inside Structure
A structure can contain another structure Object.
Example:
struct Address {
string city;
int zip; int main() {
}; Student s1 = {"Ali", 20, {"New City", 10001}};
struct Student { cout << "Name: " << [Link] << endl;
string name; cout << "City: " << [Link] << endl;
int age; cout << "ZIP: " << [Link] << endl;
Address address;
}; return 0;
}
8. Structure with Functions
Functions can be used inside and outside structures.
Example 1: Function Inside Struct
struct Student {
string name;
int age;
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Student s1 = {"Ali", 20};
[Link]();
return 0;
}
Example 2: Passing Structure to Function
struct Student {
string name;
int age;
};
void display(Student s) {
cout << "Name: " << [Link] << ", Age: " << [Link] << endl;
}
int main() {
Student s1 = {"Ali", 20};
display(s1);
return 0;
}
1. Pointer Inside a Structure
A structure can contain a pointer as a member, which allows it to reference
dynamically allocated memory or other structures.
Example: Pointer Inside Structure
struct Student { int main() {
string name; Student s1;
int* age; // Pointer to an integer int a = 20;
}; [Link] = "Ali";
[Link] = &a; // Assigning address of a
return 0;
}
Basic structure using the struct keyword.
return 0;
}
Basic structure using the struct keyword.
return 0;
}
Example of a C++ program with a structure containing an array as a member:
return 0;
}
Example of a C++ structure with an array of structures:
int main() {
// Create an array of "Student" structures
// Define a structure named const int numStudents = 3; // Number of students
"Student" to represent Student students[numStudents];
student information
// Assign values to the structure members for each student
struct Student { students[0].name = "Ali";
string name; students[0].rollNumber = 101;
int rollNumber;
}; students[1].name = "Bilal";
students[1].rollNumber = 102;
students[2].name = “Usman";
students[2].rollNumber = 103;
// Display the information for each student in the array
for (int i = 0; i < numStudents; i++) {
cout << "Student " << i + 1 << " Information:" << endl;
cout << "Name: " << students[i].name << endl;
cout << "Roll Number: " << students[i].rollNumber << endl;
cout << endl;
}
return 0;
}
Example of a C++ program with a nested struct.
struct Point {
int x;
int y;
};
printCoordinates(myPoint.x, myPoint.y);
Passing Structure Variables (object) as Parameters:
You can pass entire structure variables as function parameters:
void printPoint(Point p) {
cout << "X: " << p.x << ", Y: " << p.y << endl;
}
printPoint(myPoint);
Returning Structure from Function:
Functions can return structures as well:
cout << "X: " << pPoint->x << ", Y: " << pPoint-
>y << endl;
Passing Structure Pointers as Arguments to a Function:
You can pass pointers to structures as function parameters:
void modifyPoint(Point* p) {
p->x += 2;
p->y += 2;
}
modifyPoint(&myPoint);
Returning a Structure Pointer from Function:
Functions can also return pointers to structures:
new Point allocates memory on the heap (free store). Heap memory does NOT go out
of scope when the function ends. Only the pointer variable p goes out of scope, not
the object it points to. The returned pointer still points to valid memory.
int main() {
Point* newPoint = createAndReturnPoint(3, 7);
cout << "Point: (" << newPoint->x << ", " << newPoint->y << ")\n";
return 0;
}
Passing Array of Structures:
You can create an array of structures and pass them to functions:
struct Student {
string name;
int age;
};
printStudents(classStudents, 3);
Dynamic allocation within a struct typically involves allocating memory for one or
more members of the struct using pointers. This is commonly used when you need
to handle variable-sized data or when you want to manage memory manually.
return 0;
}
Dynamic allocation of a 2D array within a struct involves using
pointers to create a dynamically allocated 2D array and then
storing a pointer to this array as a member of the struct.
return 0;
}
Functions within structures
struct Person { int main()
string name; {
int age; // Create a Person object
double height;
Person person1;
// Member function to initialize a Person object
// Call the initialize function to set the values
void initialize(const string& n, int a, double h) {
name = n; [Link]("Junaid", 30, 6.1);
age = a;
height = h; // Call the display function to show information
}
[Link]();
// Member function to display information about the person
return 0;
void display() { }
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Height: " << height << endl;
}
};
Functions within structures
struct Rectangle { int main() {
double length; // Create a Rectangle object
double width;
struct Student {
string name;
int age;
float gpa;
};
void show(Student s) {
cout << [Link] << " " << [Link] << endl;
}
Call:
Student s1 = {"Ali", 20, 3.5};
show(s1);
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
float gpa;
};
Call:
Student s1 = {"Ali", 20, 3.5};
showMembers([Link], [Link]);
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
float gpa;
};
void updateByValue(Student s) {
[Link] = 25; // does NOT change original
}
Call:
Student s1 = {"Ali", 20, 3.5};
updateByValue(s1);
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
float gpa;
};
Call:
Student s1 = {"Ali", 20, 3.5};
updateByReference(s1);
struct Student {
string name;
int age;
float gpa;
};
Call:
Student s1 = {"Ali", 20, 3.5};
updateByPointer(&s1);
cout << [Link] << endl; // see difference after each call
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
float gpa;
};
struct Student {
string name;
int age;
float gpa;
};
Student createStudent() {
Student s = {"John", 23, 3.2};
return s;
}
Call:
Student s2 = createStudent();
cout << [Link] << endl;
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
float gpa;
};
Student* createStudentPtr() {
Student* s = new Student{"Mike", 24, 3.1};
return s;
}
Call:
Student* ptr = createStudentPtr();
cout << ptr->name << endl;
delete ptr; // free memory
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
float gpa;
};
Call:
Student s3;
inputStudent(s3);
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
float gpa;
};
Display Function
Call:
displayStudent(s3);
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
float gpa;
};
Student a = {"Ali",20,3.5};
Student b;
b = a; // assignment
Direct Copying
Student c = a; // copy at initialization
Shallow Copy Deep Copy
struct Student {
string name;
int age;
float gpa;
};
Constant Structures
struct Student {
string name;
int age;
float gpa;
};
Pointer to const
struct Student {
string name;
int age;
float gpa;
};
Const pointer
Student s5 = {"Tom",21,3.4};
Student *const ptr2 = &s5;
ptr2->age = 50; // allowed
// ptr2 = &s1; // Not allowed
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
float gpa;
};
Const pointer
Student s5 = {"Tom",21,3.4};
Student *const ptr2 = &s5;
ptr2->age = 50; // allowed
// ptr2 = &s1; // Not allowed
DMA (Dynamic Memory Allocation) examples in C++ using structures
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
};
Allocate
Student *ptr = new Student;
Assign Values
ptr->name = "Ali";
ptr->age = 20;
Display
cout << ptr->name << " " << ptr->age << endl;
Free Memory
delete ptr;
DMA (Dynamic Memory Allocation) examples in C++ using structures
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
};
1D DMA – Array of Structures
Allocate Array
int n = 3;
Student *arr = new Student[n];
Assign Values
for(int i = 0; i < n; i++) {
arr[i].name = "Student";
arr[i].age = 18 + i;
}
Display
for(int i = 0; i < n; i++) {
cout << arr[i].name << " " << arr[i].age << endl;
}
Free Memory
delete[] arr;
DMA (Dynamic Memory Allocation) examples in C++ using structures
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
};
2D DMA – Array of Structure Arrays
Allocate 2D Array Display
int rows = 2, cols = 2; for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
Student **arr = new Student*[rows]; cout << arr[i][j].name << " "
<< arr[i][j].age << " ";
for(int i = 0; i < rows; i++) { }
arr[i] = new Student[cols];
cout << endl;
}
Assign Values }
for(int i = 0; i < rows; i++) { Free Memory
for(int j = 0; j < cols; j++) { for(int i = 0; i < rows; i++) {
arr[i][j].name = "S"; delete[] arr[i];
arr[i][j].age = 20 + i + j; }
} delete[] arr;
}
1. Create a student structure, whose members are
i. Name (a char array),
ii. roll_number,
iii. marks (an array of type float having size 5),
iv. major (a char array, to show the major of the student).
2. There shall also be a nested structure of type date struct inside
the student structure, for the birthdate and registration date.
3. Now first create a student variable named CSStudent.
4. Fill up all the fields (members) with some random values from
the console using “cin”
5. Secondly create another student variable named EEStudent.
6. Assign CSStudent to EEStudent.
7. Show the values of the members of both struct variables using
cout.
#include <iostream>
using namespace std;
return 0;
}
Topic Syntax Key Point Function Call
Define Structure struct S { int a; }; Groups different data types S s1;
Access Member s1.a Use . with object —
Access via Pointer ptr->a Use -> with pointer —
Pass by Value void f(S s); Copy created f(s1);
Pass by Reference void f(S &s); No copy, original changes f(s1);
Pass by Pointer void f(S *s); Use -> inside f(&s1);
Pass Members f(s.a); Only selected values sent f(s1.a);
Array of Structures void f(S arr[], int n); Pass array name f(arr, n);
▪ Array of Structures:
Create an array of struct Book containing title, author, and price. Store details of 3 books and display them.
▪ Nested Structures:
Define a structure Address inside struct Employee. Store city and state within Address. Create an employee instance and print its
details.
▪ Pointer to Structure:
Create a pointer to a struct Student, dynamically allocate memory, assign values, and display them.
// -----------------------------
// Student Database
// -----------------------------
struct Student {
string name;
int rollNo;
int age;
float gpa;
};
// -----------------------------
// Movie Booking System
// -----------------------------
struct Seat {
int seatNumber;
int row;
bool isBooked;
};
// -----------------------------
// Visitor Temporary Storage
// -----------------------------
struct Visitor {
string name;
string purpose;
string arrivalTime;
};
// -----------------------------
// MAIN FUNCTION – Practice Skeleton
// -----------------------------
int main() {
cout << "\n--- Student Database ---\n";
int nStudents = 2;
Student *students = new Student[nStudents];
for (int i = 0; i < nStudents; i++) inputStudent(students[i]);
for (int i = 0; i < nStudents; i++) displayStudent(students[i]);
delete[] students;
return 0;
}
10. Conclusion