To get a value from a multidimensional array containing objects in C++, you can use the same syntax as you would for a regular multidimensional array, with the added step of accessing the appropriate member variable of the object. Simply use the appropriate index notation to access the object in the array, and then use the dot operator “.” to access the desired member variable or function.
Here’s an example code snippet that demonstrates how to get a value from a two-dimensional array containing objects:
C++
// C++ Implementation
#include <iostream>
#include <string>
usingnamespacestd;
// Create a class
classMyClass {
public:
intvalue;
MyClass(intval) { value = val; }
};
// Driver code
intmain()
{
// Create a 2D array of objects
MyClass arr[2][2] = { { MyClass(1), MyClass(2) },
{ MyClass(3), MyClass(4) } };
// Access a value in the array
intval = arr[1][0].value;
// print the value
cout << "Value at index [1][0] is: "<< val << endl;
Console.WriteLine("Value at index [1, 0] is: "+ val);
}
}
Javascript
// JavaScript implementation
// Create a class
class MyClass {
constructor(val) {
this.value = val;
}
}
// Driver code
// Create a 2D array of objects
let arr = [[newMyClass(1), newMyClass(2)],
[newMyClass(3), newMyClass(4)]];
// Access a value in the array
let val = arr[1][0].value;
// Print the value
console.log('Value at index [1][0] is: '+ val);
Output
Value at index [1][0] is: 3
In this example, we define a class MyClass with a single member variable value and use it to create a two-dimensional array arr of size 2×2. We then access the value at index [1][0] of the array by using the dot operator. to access the value member variable of the object at that index.
Here’s another example of how to get a value from a multidimensional array containing objects in C++:
C++
// C++ implementation
#include <iostream>
#include <string>
usingnamespacestd;
// Define a class called "Student"
// to represent a student
classStudent {
public:
string name;
intage;
doublegpa;
// Constructor to initialize the
// values of the student
Student(string n, inta, doubleg)
{
name = n;
age = a;
gpa = g;
}
};
// Drivers code
intmain()
{
// Create a 2D array
// of student objects
Student students[2][2]
= { { Student("Alice", 18, 3.8),
Student("Bob", 20, 3.5) },
{ Student("Charlie", 19, 3.7),
Student("Dave", 21, 3.9) } };
// Access a value in the array
doublegpa = students[0][1].gpa;
// Print the value
cout << "GPA of student at index [0][1] is: "<< gpa
<< endl;
return0;
}
Java
// Java implementation
publicclassStudent {
publicString name;
publicintage;
publicdoublegpa;
// Constructor to initialize the values of the student
publicStudent(String n, inta, doubleg)
{
name = n;
age = a;
gpa = g;
}
// Main method
publicstaticvoidmain(String[] args)
{
// Create a 2D array of student objects
Student[][] students
= { { newStudent("Alice", 18, 3.8),
newStudent("Bob", 20, 3.5) },
{ newStudent("Charlie", 19, 3.7),
newStudent("Dave", 21, 3.9) } };
// Access a value in the array
doublegpa = students[0][1].gpa;
// Print the value
System.out.println(
"GPA of student at index [0][1] is: "+ gpa);
}
}
Python3
# Define a class called "Student"
# to represent a student
classStudent:
def__init__(self, n, a, g):
self.name =n
self.age =a
self.gpa =g
# Create a 2D array
# of student objects
students =[[Student("Alice", 18, 3.8),
Student("Bob", 20, 3.5)],
[Student("Charlie", 19, 3.7),
Student("Dave", 21, 3.9)]]
# Access a value in the array
gpa =students[0][1].gpa
# Print the value
print(f"GPA of student at index [0][1] is: {gpa}")
C#
// C# implementation
usingSystem;
// Define a class called "Student"
// to represent a student
classStudent
{
publicstringname;
publicintage;
publicdoublegpa;
// Constructor to initialize the
// values of the student
publicStudent(stringn, inta, doubleg)
{
name = n;
age = a;
gpa = g;
}
}
// Drivers code
classProgram
{
staticvoidMain(string[] args)
{
// Create a 2D array
// of student objects
Student[,] students = newStudent[2, 2]
{
{ newStudent("Alice", 18, 3.8),
newStudent("Bob", 20, 3.5) },
{ newStudent("Charlie", 19, 3.7),
newStudent("Dave", 21, 3.9) }
};
// Access a value in the array
doublegpa = students[0, 1].gpa;
// Print the value
Console.WriteLine("GPA of student at index [0, 1] is: "+ gpa);
console.log("GPA of student at index [0][1] is: "+ gpa);
Output
GPA of student at index [0][1] is: 3.5
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!