|
-
Jan 9th, 2008, 03:56 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Nested Class Question
I'm playing around with nested classes and I'm getting some rather strange output from what appears to be some rather simple numbers. Take a look at my program below.
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
class A {
protected:
int d;
public:
class SubA {
protected:
int a;
public:
SubA() {};
~SubA() {};
int getA() { return a; }
void setA(int _a) { _a = a; }
};
class SubB {
protected:
int b;
public:
SubB() {};
~SubB() {};
int getB() { return b; }
void setB(int _b) { _b = b; }
};
class SubC {
protected:
int c;
public:
SubC() {};
~SubC() {};
int getC() { return c; }
void setC(int _c) { _c = c; }
};
A() {};
~A() {};
int getD() { return d; }
void setD(int _d) { _d = d; }
};
int main(int argc, char *argv[])
{
//instantiate all the classes
A dummy;
A::SubA subADummy;
A::SubB subBDummy;
A::SubC subCDummy;
//set up some values
dummy.setD(5);
subADummy.setA(10);
subBDummy.setB(15);
subCDummy.setC(20);
//print them out to the screen
cout << dummy.getD() << endl;
cout << subADummy.getA() << endl;
cout << subBDummy.getB() << endl;
cout << subCDummy.getC() << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Now I'm expecting that during the output I should see this:
5
10
15
20
However there's an entirely different case. This is what the output looks like:
2013315389
2013327107
2013454920
7863840
I'm having a slight problem understanding what in the world is going on here. I understand the principals of nested classes but from what I can remember when I first started with these a long time ago, I've never seen this kind of output before. Can someone give me a hand here? Thanks a lot.
[EDIT] I wonder if those first three output number are phone numbers?
Last edited by GamerMax5; Jan 10th, 2008 at 02:27 PM.
-
Jan 9th, 2008, 03:38 PM
#2
Re: Nested Class Question
All of your set functions are wrong.. I'm amazed your compiler didn't flag them in any way..
Basically, when an integer is created.. it isn't assigned the value 0 by default, hence the random and large numbers.
In all of your set functions, you have:
_x is the variable being passed to the function. What you want.. is the other way around:
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Jan 10th, 2008, 07:46 AM
#3
Re: Nested Class Question
There's no reason why the compiler should flag them. The code is perfectly valid (logic-wise it's wrong, but it is still legal). For the compiler to flag those as warnings, it would have to comprehend what you are trying to do which is just not possible in this case. "getA" is not a function name that has any significance to the compiler, its just another function.
I don't live here any more.
-
Jan 10th, 2008, 02:26 PM
#4
Thread Starter
Hyperactive Member
Re: Nested Class Question
That was a silly problem. I was just typing away and didn't notice what it was that I was doing. I caught it about two hours after I posted the question. Thanks for all your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|