Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to define and use two dimension object array in C++

2 Answers

0 votes
#include <iostream>

using std::cout;
using std::endl;

class Test {
	double a, b;
public:
	Test(double _a, double _b) {
		a = _a;
		b = _b;
	}
	void print() {
		cout << a << " : " << b << endl;
	}
};

int main()
{
	Test o[2][3] = {
		Test(1, 1), Test(2, 2),
		Test(3, 3), Test(4, 4),
		Test(5, 5), Test(6, 6)
	};

	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
			o[i][j].print();
		cout << endl;
	}

	return 0;
}


/*
run:

1 : 1
2 : 2
3 : 3

4 : 4
5 : 5
6 : 6

*/

 



answered Apr 12, 2018 by avibootz
0 votes
#include <iostream>

using std::cout;
using std::endl;

class Test {
	int n;
public:
	Test(int _n) { n = _n; }
	int get() { return n; }
};

int main()
{
	Test o[3][2] = {
		1, 2,
		3, 4,
		5, 6
	};

	for (int i = 0; i < 3; i++) {
		cout << o[i][0].get() << " : " << o[i][1].get() << endl;
	}

	return 0;
}


/*
run:

1 : 2
3 : 4
5 : 6

*/

 



answered Apr 12, 2018 by avibootz

Related questions

1 answer 163 views
1 answer 126 views
1 answer 150 views
1 answer 219 views
1 answer 152 views
1 answer 211 views
2 answers 403 views
...