Help with simple classes-constructors

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Help with simple classes-constructors

 
0
  #1
Dec 8th, 2008
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:
  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:
  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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 397
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Help with simple classes-constructors

 
1
  #2
Dec 8th, 2008
Your problem is the overloading of the instance of number.

Let me illustrate.

  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
  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.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 8
Reputation: sweeya is an unknown quantity at this point 
Solved Threads: 1
sweeya sweeya is offline Offline
Newbie Poster

Re: Help with simple classes-constructors

 
0
  #3
Dec 8th, 2008
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.)
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: Help with simple classes-constructors

 
0
  #4
Dec 9th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: Help with simple classes-constructors

 
0
  #5
Dec 9th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 577
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 93
Murtan Murtan is offline Offline
Posting Pro

Re: Help with simple classes-constructors

 
0
  #6
Dec 10th, 2008
The constructor did exactly what you asked it to (as all software should.)

  1. numgetter()
  2. {num=0;}

Defines the constructor to initialize the num to zero.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: Help with simple classes-constructors

 
0
  #7
Dec 10th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 577
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 93
Murtan Murtan is offline Offline
Posting Pro

Re: Help with simple classes-constructors

 
0
  #8
Dec 10th, 2008
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."
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: Help with simple classes-constructors

 
0
  #9
Dec 18th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 397
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Help with simple classes-constructors

 
1
  #10
Dec 19th, 2008
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.

  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.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC