943,714 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 871
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 8th, 2008
0

Help with simple classes-constructors

Expand Post »
I was writing a simple class to test my understanding of class and the first class program had a number -int num private- that was modified by the user by void definenum(num) and retrieved with int getnum(). It functioned as I expected-i would define it as 4 and it would retrieve 4. However when i tried to add a constructor to initialize the program it got nutty. It compiled fine with this code:
header:
C++ Syntax (Toggle Plain Text)
  1. class numgetter{
  2. private:
  3. int num;
  4. public:
  5. numgetter()
  6. {num=0;}
  7. void definenum(int num)
  8. {}
  9. int getnum()
  10. {
  11. return num;
  12. }
  13. };
main:
c++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include"class_header.h"
  3. using namespace std;
  4. int main()
  5. {
  6. class numgetter test;
  7. int in_num;
  8. cout<<"Input the number";
  9. cin>>in_num;
  10. test.definenum(in_num);
  11. cout<<test.getnum();
  12. system("pause");
  13. }
I would input 4 and it would retrieve 0.
Why doesnt it have the constructor numgetter() execute when I make the new instance of it, rather all the time-or so it seems?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Dec 8th, 2008
1

Re: Help with simple classes-constructors

Your problem is the overloading of the instance of number.

Let me illustrate.

c++ Syntax (Toggle Plain Text)
  1. int num=6;
  2. {
  3. int num; // This num is not the same as above
  4. num=7;
  5. }
  6. // In this scope
  7. cout<<"num =="<<num;

In your case you have put
void definenum(int num) {} and this has produced a new local variable (called num) and it does nothing with it.

you could write
c++ Syntax (Toggle Plain Text)
  1. void definenum(const int X)
  2. {
  3. num=X;
  4. }

Then that should work as intended.

Hope this helps. If not please feel free to repost / or ask different questions.
Last edited by StuXYZ; Dec 8th, 2008 at 5:57 am.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
Dec 8th, 2008
0

Re: Help with simple classes-constructors

Hi,
The constructor will be called only when the instance is first created. In your function definenum you does not seem to be doing anything. So the value of your private variable remains zero (This is initialised in the constructor.)
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sweeya is offline Offline
8 posts
since Nov 2008
Dec 9th, 2008
0

Re: Help with simple classes-constructors

void definenum(int num)
{}
it defies num, when called it does a pbv and so int num is now whatever I passed it to be
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Dec 9th, 2008
0

Re: Help with simple classes-constructors

Also, to STUXYZ why didnt my constructor work-it didn't have access? Because I think its still talking about the same num in terms of overloading.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Dec 10th, 2008
0

Re: Help with simple classes-constructors

The constructor did exactly what you asked it to (as all software should.)

c++ Syntax (Toggle Plain Text)
  1. numgetter()
  2. {num=0;}

Defines the constructor to initialize the num to zero.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 10th, 2008
0

Re: Help with simple classes-constructors

ya, but I cant figure out why when I compile it the constructor works but then I can't define define it again with definenum. If it is an operator overloading error or pbv or pbr error please tell me. It works when I change the void definenum(int num) {} into definenum(int in_num){num=in_num;}
Last edited by CPPRULZ; Dec 10th, 2008 at 1:25 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Dec 10th, 2008
0

Re: Help with simple classes-constructors

definenum is NOT a constructor, it is a method.

The code void definenum(int num) {} is like me walking up to you and handing you something, but not telling you what to do with it; eventually, you'll throw it away.

When you expand the code to void definenum(int in_num){num=in_num;} you told the class what to do with the something. "When someone calls definenum, take what they gave you and save it in num, throwing away any previous value."
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 18th, 2008
0

Re: Help with simple classes-constructors

I was saying getnum was a constructor and my problem was that I thought if I call with the parameter int in_num why doesnt it do a pbv and set num=in_num. I needed to know if it was a matter of access priveeges or blocking. the first response to my question by StuXYZ made the most sense.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Dec 19th, 2008
1

Re: Help with simple classes-constructors

Well having been away (sorry). But I would like to make a summary of were I think we all are.

As is my custom, it will be with examples. Now the discussion is really about scope. In C++ it is possible to have many different variables called the same thing. But the compiler will only allow one variable name per scope. Once you understand the arcane magic called scoping rules all is easy. (Although true understanding of all scoping rule requires a very very long beard

So to an example. I would like to post complete programs. copy them an run them and see the stangeness on your own computer.
In this case we are going to use the variable name X to mean all the many different X I am going to produce.

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. class A
  4. {
  5. private:
  6. int X;
  7. int Y;
  8. public:
  9.  
  10. A(int X) : X(17),Y(X+A::X)
  11. { }
  12. void setY(int X) { Y=X; }
  13. int getX() const { return X; }
  14. int getY() const { return Y; }
  15. };
  16.  
  17. int X=9;
  18.  
  19. int
  20. main()
  21. {
  22. int X=8;
  23. A xv(20);
  24. std::cout<<"X in class = "<<xv.getX()<<std::endl;
  25. std::cout<<"Y in class = "<<xv.getY()<<std::endl;
  26. xv.setY(455);
  27. std::cout<<"Y in class= "<<xv.getY()<<std::endl;
  28. std::cout<<"X = "<<X<<std::endl;
  29. std::cout<<"X (globally) = "<<::X<<std::endl;
  30. }

Well I had better have a go an explaining the mess. First off
let us look at class A. It has a class variable X.
I have complicated the thing by calling the constructor with a local method variable X. The rules basically say that the more local a variable is the higher its precedence, i.e. which variable will be called X. All other variables can be obtained but have to be qualified.

so line 10 says: set the variable X in A to 17. Then set variable Y to the local variable X + the class variable X which we just set to 17.
From line 23, Y will be 17+20 == 37.

line 12: setY uses a local variable X. Y is set to that local variable. The class variable X is irrelevent until you change the setY(int X) to something like setY(int Z) .

line 17: sets a global variable X.
line 22: sets a local variable X (to the function int main) and as that is more local line 28 use that.
line 29: This shows how to get a global variable when there is a local variable in scope.

Try playing with the values, and the name X to see what happens.
If you understand what goes on here it will make you a MUCH better c++ programmer.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Card Games
Next Thread in C++ Forum Timeline: Tutor says: it takes 25 mins to write this program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC