Is there a way to inherit...

Is there a way for an object of the same class to inherit exactly one attribute. Object is constructed randomly using static function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
	
int main()
{

	srand(static_cast<unsigned int>(time(0))); // set initial seed value to system clock
	rand(); // If using Visual Studio, discard first random value
	
	std::vector<Bunny> package;
	package.reserve(1000); // pre-allocate memory in one go
	int count = 0;
	int amount = 0;
	for (int i = 0; i < 5; i++)
	{
		Bunny bunny = BunnyGenerator::generateBunny();
		package.push_back(bunny);
	}
	while (package.size() < 40)
	{
		// Increase the age of every single bunny in the farm
		for (unsigned int i = 0; i < package.size(); i++)
		{
			package[i].increaseAge();
			if (package[i].getAge() > 9)
			{
				package.erase(package.begin() + i);
			}
		}
	
		// Create as many new rabits as there are females atleast year 2
		for (unsigned int i = 0; i < package.size(); i++)
		{
			if (package[i].getSex() == "Female" && package[i].getAge() > 1)
			{
				Bunny bunny = BunnyGenerator::generateBunny();
				bunny.m_color = package[i].m_color;
				package.push_back(bunny);
			}
				
		}

	}
	for (vector<Bunny>::iterator it = package.begin(); it != package.end(); it++)
		cout << *it << endl;
	


	return 0;
}



I made m.color public, however, i know the dangers of that so i would love to hear from you if you can think of any better solution
Last edited on
Make only the attribute you want protected, rest private.
Once i do that the variable is inaccessible.
Why do you care if making an attribute public is dangerous or not?
If that is the case, you can create getter - setter functions.
> Is there a way for an object of the same class to inherit exactly one attribute.
¿what are you talking about? objects don't inherit, classes do (from another class)


BunnyGenerator::generateBunny(package[i].m_color);
ne555 >>> English isn't my native language so i didn't know the right word for that. Sorry if i was misleading. I meant to say that i needed one object to get the same value form another well inherit is probably still suitable word. A rabbit father has to give another rabbit kid object his color, that's what i meant by saying inherit.
If you want to inherit, make all attributes and methods you want to inherit in the base class protected, and the rest are private. To make the protected attributes and methods inherited from base class visible outside the derived class, create getter and setter functions.
> A rabbit father has to give another rabbit kid object his color
Then the `generateBunny()' function should receive as parameter the parents of the rabbit.

BunnyGenerator::generateBunny(package[i]);
only that function would need access to m_color, and perhaps only read access

> rand(); // If using Visual Studio, discard first random value
¿what for?
Topic archived. No new replies allowed.