C++ Notes: Arrays as Class Members and Arrays of Objects
1. Introduction
In C++, arrays and classes are two fundamental concepts. When combined, they enable
powerful ways to organize and manage data:
1. Arrays as class member data – an array is declared inside a class as a data
member.
2. Arrays of objects – a whole array is created where each element is an object of some
class.
Both concepts are extremely important for problems like: - Managing marks of multiple
subjects for a single student. - Storing data for many students, employees, products, etc. -
Implementing small record-management systems.
This note covers both topics in detail with explanations, syntax, and complete examples.
2. Arrays as Class Member Data
2.1 Concept
• An array as a class member means that one of the data members of a class is an
array.
• All objects of that class will contain their own copy of that array.
• The size of the array must be a compile-time constant (for built-in arrays).
Example idea: A Student class may contain an array of 5 marks:
class Student {
int rollNo;
int marks[5]; // array as class member
};
Every Student object will have its own rollNo and its own marks[5].
2.2 Declaring Arrays as Data Members
General form:
class ClassName {
data_type array_name[size];
// other members
};
Examples:
class Sample {
int a[10]; // array of 10 integers
float price[5]; // array of 5 floats
char name[30]; // character array (string)
};
Rules: - size must be a constant integer known at compile time. - You cannot use a non-
const variable as the size of a built-in array.
2.3 Initializing Array Members
Array members cannot be directly initialized in the class body using simple assignment
like:
class Test {
int arr[3] = {1, 2, 3}; // allowed only in modern C++ (since C+
+11) as in-class member initializer
};
In older C++ standards (before C++11), you usually initialize array members inside the
constructor.
2.3.1 Initialization Using Constructor (Traditional Way)
#include <iostream>
using namespace std;
class Student {
int rollNo;
int marks[5];
public:
Student(int r, int m1, int m2, int m3, int m4, int m5) {
rollNo = r;
marks[0] = m1;
marks[1] = m2;
marks[2] = m3;
marks[3] = m4;
marks[4] = m5;
}
void display() {
cout << "Roll No: " << rollNo << "\n";
cout << "Marks: ";
for (int i = 0; i < 5; i++) {
cout << marks[i] << " ";
}
cout << "\n";
}
};
int main() {
Student s1(101, 80, 75, 90, 85, 88);
[Link]();
return 0;
}
Key points: - The constructor receives individual marks as arguments. - Each element of
marks[] is assigned inside the constructor body.
2.3.2 Initialization with Loops Inside Constructor
When there are many elements, manually assigning each index is tedious. Use loops:
#include <iostream>
using namespace std;
class Student {
int rollNo;
int marks[5];
public:
Student(int r, int m[]) {
rollNo = r;
for (int i = 0; i < 5; i++) {
marks[i] = m[i];
}
}
void display() {
cout << "Roll No: " << rollNo << "\n";
cout << "Marks: ";
for (int i = 0; i < 5; i++) {
cout << marks[i] << " ";
}
cout << "\n";
}
};
int main() {
int m[5] = {80, 75, 90, 85, 88};
Student s1(101, m);
[Link]();
return 0;
}
Here an external array m is passed to the constructor.
2.4 Accessing Array Members (Encapsulation)
Array members are typically kept private and accessed through member functions.
class Sample {
int arr[5];
public:
void input() {
cout << "Enter 5 integers: ";
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
}
void display() {
cout << "Array elements: ";
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << "\n";
}
};
int main() {
Sample s;
[Link]();
[Link]();
return 0;
}
Benefits: - Encapsulation: direct access to array is restricted. - Validation: you can
validate inputs before storing.
2.5 Character Arrays as Class Members
Character arrays are often used to store strings.
#include <iostream>
#include <cstring>
using namespace std;
class Person {
char name[30];
public:
void setName(const char *n) {
strcpy(name, n); // copy C-string into char array
}
void display() {
cout << "Name: " << name << "\n";
}
};
int main() {
Person p;
[Link]("Amit Kumar");
[Link]();
return 0;
}
Note: In modern C++, std::string is usually preferred instead of raw char arrays.
2.6 Multidimensional Arrays as Members
You can also declare 2D or 3D arrays as class members.
Example: Storing marks of 3 students in 4 subjects.
#include <iostream>
using namespace std;
class Marks {
int m[3][4]; // 3 students, 4 subjects each
public:
void input() {
cout << "Enter marks of 3 students (4 subjects each):\n";
for (int i = 0; i < 3; i++) {
cout << "Student " << i + 1 << ": ";
for (int j = 0; j < 4; j++) {
cin >> m[i][j];
}
}
}
void display() {
cout << "\nMarks List:\n";
for (int i = 0; i < 3; i++) {
cout << "Student " << i + 1 << ": ";
for (int j = 0; j < 4; j++) {
cout << m[i][j] << " ";
}
cout << "\n";
}
}
};
int main() {
Marks obj;
[Link]();
[Link]();
return 0;
}
2.7 Key Points and Common Mistakes (Arrays as Members)
• Size must be constant at compile time for built-in arrays.
• Do not forget to initialize array elements (they may contain garbage values).
• Use loops to process arrays inside member functions.
• Prefer std::string over char[] for strings where possible.
• For variable-size data, prefer dynamic allocation or std::vector instead of fixed
arrays.
3. Arrays of Objects
3.1 Concept
• An array of objects means that each element of the array is an object of a particular
class.
• All objects in the array are created consecutively in memory.
• Useful when you want to manage many similar entities: many students, employees,
items, etc.
General declaration:
ClassName objectArray[size];
Example:
class Student {
// data members and member functions
};
Student s[50]; // array of 50 Student objects
3.2 Simple Example: Array of Objects (Employee)
#include <iostream>
using namespace std;
class Employee {
int empId;
float salary;
public:
void getData() {
cout << "Enter employee id and salary: ";
cin >> empId >> salary;
}
void putData() {
cout << "ID: " << empId << ", Salary: " << salary << "\n";
}
};
int main() {
Employee e[3]; // array of 3 Employee objects
cout << "Enter data for 3 employees:\n";
for (int i = 0; i < 3; i++) {
cout << "Employee " << i + 1 << "\n";
e[i].getData();
}
cout << "\nEmployee details:\n";
for (int i = 0; i < 3; i++) {
e[i].putData();
}
return 0;
}
Explanation: - e[0], e[1], e[2] are objects of type Employee. - getData() and
putData() are called for each object in a loop.
3.3 Constructors and Arrays of Objects
When you create an array of objects:
ClassName obj[size];
• The default constructor (if defined) is called for each element.
• If only a parameterized constructor exists (no default constructor), you cannot
create a simple array like above without providing arguments.
3.3.1 Using Default Constructor
#include <iostream>
using namespace std;
class Item {
int code;
float price;
public:
Item() { // default constructor
code = 0;
price = 0.0;
}
void getData() {
cout << "Enter code and price: ";
cin >> code >> price;
}
void putData() {
cout << "Code: " << code << ", Price: " << price << "\n";
}
};
int main() {
Item it[3]; // default constructor called 3 times
for (int i = 0; i < 3; i++) {
cout << "Item " << i + 1 << "\n";
it[i].getData();
}
cout << "\nItem details:\n";
for (int i = 0; i < 3; i++) {
it[i].putData();
}
return 0;
}
3.3.2 Array of Objects with Parameterized Constructor
You cannot simply write Item it[3]; if there is only a parameterized constructor and no
default constructor. One option is to: - Provide a default constructor, or - Use dynamic
allocation with new, or - Use an initializer list (for small cases).
Example using both default and parameterized constructors:
#include <iostream>
using namespace std;
class Item {
int code;
float price;
public:
Item() { // default constructor
code = 0;
price = 0.0;
}
Item(int c, float p) { // parameterized constructor
code = c;
price = p;
}
void display() {
cout << "Code: " << code << ", Price: " << price << "\n";
}
};
int main() {
Item it[3] = {
Item(101, 25.5),
Item(102, 30.0),
Item(103, 18.75)
};
for (int i = 0; i < 3; i++) {
it[i].display();
}
return 0;
}
3.4 Passing Arrays of Objects to Functions
You may often need to pass the entire array of objects to a function.
General form:
void functionName(ClassName objArray[], int size);
Example:
#include <iostream>
using namespace std;
class Student {
int rollNo;
float marks;
public:
void getData() {
cout << "Enter roll number and marks: ";
cin >> rollNo >> marks;
}
void putData() {
cout << "Roll: " << rollNo << ", Marks: " << marks << "\n";
}
};
void displayAll(Student s[], int n) {
cout << "\nStudent details:\n";
for (int i = 0; i < n; i++) {
s[i].putData();
}
}
int main() {
const int N = 3;
Student s[N];
for (int i = 0; i < N; i++) {
cout << "Student " << i + 1 << "\n";
s[i].getData();
}
displayAll(s, N);
return 0;
}
3.5 Dynamic Arrays of Objects (Using new and delete)
Sometimes the required size of the array is not known at compile time. In that case, you can
create an array of objects dynamically.
#include <iostream>
using namespace std;
class Test {
int x;
public:
Test() {
x = 0;
}
void setX(int val) {
x = val;
}
void show() {
cout << x << " ";
}
};
int main() {
int n;
cout << "Enter number of objects: ";
cin >> n;
Test *ptr = new Test[n]; // dynamic array of n objects
for (int i = 0; i < n; i++) {
ptr[i].setX(i + 1);
}
cout << "Values: ";
for (int i = 0; i < n; i++) {
ptr[i].show();
}
delete[] ptr; // free memory
return 0;
}
Points: - new Test[n] creates n objects using the default constructor. - Always release
memory using delete[].
4. Combined Example: Class with Array Member and Array of Objects
This example uses both concepts: - Each Student object has an array member marks[3]. -
Student objects themselves are stored in an array.
#include <iostream>
using namespace std;
class Student {
int rollNo;
int marks[3]; // array as class member (3 subjects)
public:
void getData() {
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter marks in 3 subjects: ";
for (int i = 0; i < 3; i++) {
cin >> marks[i];
}
}
float total() {
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += marks[i];
}
return sum;
}
float percentage() {
return total() / 3.0f;
}
void display() {
cout << "Roll No: " << rollNo << ", Total: " << total()
<< ", Percentage: " << percentage() << "\n";
}
};
int main() {
const int N = 2;
Student s[N]; // array of objects
cout << "Enter data for " << N << " students:\n";
for (int i = 0; i < N; i++) {
cout << "\nStudent " << i + 1 << "\n";
s[i].getData();
}
cout << "\nResult:\n";
for (int i = 0; i < N; i++) {
s[i].display();
}
return 0;
}
This example clearly shows: - Array as member: marks[3] inside Student. - Array of
objects: Student s[N]; in main().
5. Difference Between Arrays as Members and Arrays of Objects
Aspect Arrays as Class Members Arrays of Objects
Definition Array declared inside a class Array whose elements are
as a data member objects of a class
Focus Multiple values inside one Multiple objects of the
object same class
Example int marks[5]; inside Student s[50]; in
class Student main()
Memory per Each object has its own copy Single array holds many
object of the array separate objects
Common use Many attributes for one Many entities of same
Aspect Arrays as Class Members Arrays of Objects
entity type
Typical Loops inside class member Loops in main() or other
operations functions functions
6. Best Practices and Summary
1. Use arrays as class members when:
– You need to store a fixed number of related values for each object.
– Example: fixed number of subjects, fixed number of monthly sales, etc.
2. Use arrays of objects when:
– You need to store and process many similar records (students, employees,
items).
3. For flexible sizes:
– Prefer dynamic arrays (new / delete[]) or Standard Library containers like
std::vector.
4. Keep arrays private and provide functions for input/output and processing.
5. For strings:
– Prefer std::string over char[] unless specifically practicing character
arrays.
Understanding these two concepts and their differences is essential for building record-
based mini projects and for mastering basic object-oriented programming in C++.