Get value from Multidimentional Array containing an Object
Last Updated :
20 Sep, 2023
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++
#include <iostream>
#include <string>
using namespace std;
class MyClass {
public :
int value;
MyClass( int val) { value = val; }
};
int main()
{
MyClass arr[2][2] = { { MyClass(1), MyClass(2) },
{ MyClass(3), MyClass(4) } };
int val = arr[1][0].value;
cout << "Value at index [1][0] is: " << val << endl;
return 0;
}
|
Java
import java.io.*;
class MyClass {
public int value;
public MyClass( int val) {
value = val;
}
}
class Main {
public static void main(String[] args) {
MyClass[][] arr = { { new MyClass( 1 ), new MyClass( 2 ) },
{ new MyClass( 3 ), new MyClass( 4 ) } };
int val = arr[ 1 ][ 0 ].value;
System.out.println( "Value at index [1][0] is: " + val);
}
}
|
Python3
class MyClass:
def __init__( self , val):
self .value = val
if __name__ = = '__main__' :
arr = [[MyClass( 1 ), MyClass( 2 )],
[MyClass( 3 ), MyClass( 4 )]]
val = arr[ 1 ][ 0 ].value
print (f "Value at index [1][0] is: {val}" )
|
C#
using System;
class MyClass
{
public int value;
public MyClass( int val)
{
value = val;
}
}
class MainClass
{
public static void Main( string [] args)
{
MyClass[,] arr = { { new MyClass(1), new MyClass(2) },
{ new MyClass(3), new MyClass(4) } };
int val = arr[1, 0].value;
Console.WriteLine( "Value at index [1, 0] is: " + val);
}
}
|
Javascript
class MyClass {
constructor(val) {
this .value = val;
}
}
let arr = [[ new MyClass(1), new MyClass(2)],
[ new MyClass(3), new MyClass(4)]];
let val = arr[1][0].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++
#include <iostream>
#include <string>
using namespace std;
class Student {
public :
string name;
int age;
double gpa;
Student(string n, int a, double g)
{
name = n;
age = a;
gpa = g;
}
};
int main()
{
Student students[2][2]
= { { Student( "Alice" , 18, 3.8),
Student( "Bob" , 20, 3.5) },
{ Student( "Charlie" , 19, 3.7),
Student( "Dave" , 21, 3.9) } };
double gpa = students[0][1].gpa;
cout << "GPA of student at index [0][1] is: " << gpa
<< endl;
return 0;
}
|
Java
public class Student {
public String name;
public int age;
public double gpa;
public Student(String n, int a, double g)
{
name = n;
age = a;
gpa = g;
}
public static void main(String[] args)
{
Student[][] students
= { { new Student( "Alice" , 18 , 3.8 ),
new Student( "Bob" , 20 , 3.5 ) },
{ new Student( "Charlie" , 19 , 3.7 ),
new Student( "Dave" , 21 , 3.9 ) } };
double gpa = students[ 0 ][ 1 ].gpa;
System.out.println(
"GPA of student at index [0][1] is: " + gpa);
}
}
|
Python3
class Student:
def __init__( self , n, a, g):
self .name = n
self .age = a
self .gpa = g
students = [[Student( "Alice" , 18 , 3.8 ),
Student( "Bob" , 20 , 3.5 )],
[Student( "Charlie" , 19 , 3.7 ),
Student( "Dave" , 21 , 3.9 )]]
gpa = students[ 0 ][ 1 ].gpa
print (f "GPA of student at index [0][1] is: {gpa}" )
|
C#
using System;
class Student
{
public string name;
public int age;
public double gpa;
public Student( string n, int a, double g)
{
name = n;
age = a;
gpa = g;
}
}
class Program
{
static void Main( string [] args)
{
Student[,] students = new Student[2, 2]
{
{ new Student( "Alice" , 18, 3.8),
new Student( "Bob" , 20, 3.5) },
{ new Student( "Charlie" , 19, 3.7),
new Student( "Dave" , 21, 3.9) }
};
double gpa = students[0, 1].gpa;
Console.WriteLine( "GPA of student at index [0, 1] is: " + gpa);
Console.ReadLine();
}
}
|
Javascript
class Student {
constructor(n, a, g) {
this .name = n;
this .age = a;
this .gpa = g;
}
}
const students = [
[ new Student( "Alice" , 18, 3.8), new Student( "Bob" , 20, 3.5)],
[ new Student( "Charlie" , 19, 3.7), new Student( "Dave" , 21, 3.9)]
];
const gpa = students[0][1].gpa;
console.log( "GPA of student at index [0][1] is: " + gpa);
|
Output
GPA of student at index [0][1] is: 3.5
Share your thoughts in the comments
Please Login to comment...