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,948 questions

51,890 answers

573 users

How to implement int array class in C++

1 Answer

0 votes
#include <iostream>

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

class array {
	int *p;
	int size;
public:
	array(int _size) {
		p = new int[_size];
		if (!p) exit(1);
		size = _size;
	}
	~array() { delete[] p; }
	array(const array &object);
	void set(int index, int val) {
		if (index >= 0 && index < size)
			p[index] = val;
	}
	int get(int i) { return p[i]; }
	void print() {
		for (int i = 0; i < size; i++)
			cout << p[i] << ' ';
		cout << endl;
	}
};

array::array(const array &object) {
	p = new int[object.size];
	if (!p) exit(1);
	for (int i = 0; i < object.size; i++)
		p[i] = object.p[i];
}

int main()
{
	const int TOTAL = 5;
	array arr(TOTAL);

	for (int i = 0; i < TOTAL; i++)
		arr.set(i, i + 3);

	cout << arr.get(1) << endl;
	
	arr.print();

	array copy_arr = arr;
	
	arr.print();

	return 0;
}

/*
run:

4
3 4 5 6 7
3 4 5 6 7

*/

 



answered May 4, 2018 by avibootz
edited May 4, 2018 by avibootz

Related questions

1 answer 225 views
1 answer 211 views
1 answer 80 views
1 answer 157 views
1 answer 167 views
1 answer 185 views
...