Lecture 7 – Arrays and Objects

Arrays and Objects

Explore Arrays and Objects in Object-Oriented Programming. Learn how to create and manage an array of objects, understand object references and memory layout, and discover how to pass objects as parameters in C++ and Java through clear, practical examples.

Introduction

In programming, an array is used to store multiple values of the same type in a single variable.
When combined with objects, arrays allow us to handle multiple instances of a class efficiently like managing many students, employees, or cars at once.

Array of Objects

An array of objects is simply a collection (group) of objects stored together.
Each element in the array represents a separate object instance.

Example (C++):

#include <iostream>
using namespace std;

class Student {
public:
    string name;
    int age;

    void getInfo(string n, int a) {
        name = n;
        age = a;
    }

    void display() {
        cout << name << " is " << age << " years old." << endl;
    }
};

int main() {
    Student students[3];  // Array of 3 Student objects

    students[0].getInfo("Ali", 20);
    students[1].getInfo("Sara", 21);
    students[2].getInfo("Ahmed", 22);

    for (int i = 0; i < 3; i++) {
        students[i].display();
    }
}

Lecture 6 – Static Members and Methods

Explanation:

  • students[3] creates 3 Student objects.
  • Each element stores its own name and age.
  • You can access them using array indexing.
Example (Java):
class Student {
    String name;
    int age;

    void getInfo(String n, int a) {
        name = n;
        age = a;
    }

    void display() {
        System.out.println(name + " is " + age + " years old.");
    }
}

public class Main {
    public static void main(String[] args) {
        Student[] students = new Student[3]; // Array of 3 objects

        // Create objects for each array element
        for (int i = 0; i < 3; i++) {
            students[i] = new Student();
        }

        students[0].getInfo("Ali", 20);
        students[1].getInfo("Sara", 21);
        students[2].getInfo("Ahmed", 22);

        for (int i = 0; i < 3; i++) {
            students[i].display();
        }
    }
}

In Java, you must instantiate each object inside the array before using it.

Object References and Memory Layout

Each object in an array has its own memory space for storing attributes.
However, when you use object references, multiple references can point to the same object in memory.

Example (Java):

Student s1 = new Student();
Student s2 = s1; // s2 now points to the same object as s1

s1.name = "Ali";
System.out.println(s2.name); // Output: Ali

oth s1 and s2 refer to the same memory location.

Example (C++):

Student s1;
Student* s2 = &s1; // s2 points to s1

Student s1;
Student* s2 = &s1; // s2 points to s1

s1.getInfo("Ali", 20);
s2->display(); // Access using pointer

Here, s2 is a pointer reference to s1.
Changing one affects the other because both share the same address.

Passing Objects as Parameters

Objects can be passed to functions by value (copy) or by reference (address).

Example (C++):

#include <iostream>
using namespace std;

class Box {
public:
    int length;

    Box(int l) {
        length = l;
    }

    void display() {
        cout << "Length: " << length << endl;
    }
};

// Pass by value
void changeValue(Box b) {
    b.length = 50; // Only local copy changes
}

// Pass by reference
void changeReference(Box &b) {
    b.length = 100; // Original object changes
}

int main() {
    Box b1(20);
    changeValue(b1);
    b1.display(); // Still 20

    changeReference(b1);
    b1.display(); // Now 100
}

Key Takeaway:

  • By Value → Copy created → Original unchanged
  • By Reference → Memory shared → Original changed

Example (Java):

class Box {
    int length;

    Box(int l) {
        length = l;
    }

    void display() {
        System.out.println("Length: " + length);
    }
}

public class Main {
    static void modify(Box b) {
        b.length = 100; // Affects original object
    }

    public static void main(String[] args) {
        Box b1 = new Box(20);
        modify(b1);
        b1.display(); // Output: 100
    }
}

In Java, objects are always passed by reference (technically “pass by value of reference”).

Summary of Lecture 7

ConceptDescriptionExample
Array of ObjectsGroup of objects stored togetherStudent students[3];
Object ReferencesTwo variables refer to same objectStudent s2 = s1;
Passing ObjectsObjects sent to functionsvoid modify(Box &b)
Memory LayoutEach object has its own spaceSeparate attributes per object

Positive Thought for Students

“Arrays teach structure, objects teach identity together they build logic. Every element in your array counts, just like every step in your learning.”

Leave a Reply

Your email address will not be published. Required fields are marked *