Function is returning 0, and it shouldn't be

May 13, 2015 at 10:13pm
Howdy! I'm a long time lurker and a 1st time poster trying to learn C++ on his own.

I made a really standard "99 bottles" program, featuring Jay Z!

So I made a JayZ class, and it has a function that counts until 99 problems, and it works great!

Then I wanted to challenge myself, so I made a KanyeWest class that's supposed to get the value of the problems value(in the JayZ Class) and print it to the screen.

The problem is, KanyeWest.imGoingToLetYouFinish() only gives the value of the variable as it was declared, and not the 99 value I expected.

So I guess I'm asking two questions.

1. Why does the KanyeWest class only get the initial value of the problems variable?

2. What's the "Proper" way to have a class get variable values from another class?

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
#include <iostream>
#include <Windows.h>
#include <stdlib.h>
using namespace std;
class JayZ{
private:
	int problems = 0;
public:
	void jZ(){
		if (problems == 99){
			cout << "JayZ has " << problems << " problems, and a bitch ain't one \n";
		}
		else{
			cout << "JayZ has " << problems << " problems. \n";
			problems = problems + 1;
			Sleep(100);
			jZ();
		}
	}
	int getProblems(int x){
		x = problems;
		return x;
	}
};
class KanyeWest{
protected:
	int x;
public:
	void imGoingToLetYouFinish()  {
		JayZ jz;
		cout << "I'ma let you finish but Jay Z has " << jz.getProblems(x) << " problems. \n";
	}
};


int main()
{
	JayZ jz;
	jz.jZ();
	KanyeWest kWest;
	kWest.imGoingToLetYouFinish();
	system("PAUSE");
	return 0;
}
May 13, 2015 at 10:19pm
Probably because you created a new instance of JayZ in this function. You're not using the instance in main().

1
2
3
	void imGoingToLetYouFinish()  {
		JayZ jz;  // This is a new instance of this class.
May 13, 2015 at 10:25pm
Well that makes sense, but how do I call the value from the instance of JayZ that just ran in my int main()?

I'm specifically trying to figure out how to call values in one instance of a class in a completely different class.

TL;DR
How do I get imGoingToLetYouFinish() return the 99 from the JayZ instance I just ran.
May 14, 2015 at 1:18am
Have you tried passing kWest to the function as a parameter?

May 14, 2015 at 2:16am
That sounds like a really good idea! There's one problem though, I don't know how to do that syntactically. Where in my code would I do that? Can you show me?

P.S. I really really appreciate the help, I'm getting close to actually being able to do the things I want to do in C++ and this is really helping.
May 14, 2015 at 2:18am
You'll need to redefine your function to take a parameter of the correct type. Then when you call the function you'll need to pass the correct variable.

May 14, 2015 at 2:18am
There is a tutorial on this site that covers exactly that. Go check it out.
May 14, 2015 at 2:31am
I don't mean to sound dumb, because I think you and the tutorial firedraco pointed out answered my question; but I'm still stumped.

"redefine your function to take a parameter of the correct type. Then when you call the function you'll need to pass the correct variable." Sounds like it makes sense. I just haven't the faintest idea of how to do that. I barely know what that means.

I'm going to dig through the class tutorial on the website, but if anyone one want's to copy pasta the code that does what jlib's saying, It'd help me understand this whole thing ALOT more.

Thanks for teaching me, sorry I'm a sucky student.

Edit 5/18/2015, I learned a lot more about constructors and class inheritance, jib more than answered my question.
Last edited on May 19, 2015 at 5:05am
Topic archived. No new replies allowed.