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

51,935 answers

573 users

How to create class with function that generate random in C++

1 Answer

0 votes
#include <iostream>
#include <ctime> 

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

class CRandom {
	int n;
public:
	void rnd(char *s);
};

void CRandom::rnd(char *s) {
	n = (rand() % 10) + 1; 
	cout << s << ": " << n << endl;
}

int main()
{
	CRandom o1, o2;

	srand(time(NULL));

	o1.rnd("o1");
	o2.rnd("o2");
	o1.rnd("o1");
	o2.rnd("o2");
	o1.rnd("o1");
	o2.rnd("o2");

	return 0;
}



/*
run:

o1: 7
o2: 10
o1: 1
o2: 4
o1: 6
o2: 1

*/

 



answered Jun 5, 2018 by avibootz
...