| | |
Help with simple classes-constructors
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 91
Reputation:
Solved Threads: 0
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:
main:
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?
header:
C++ Syntax (Toggle Plain Text)
class numgetter{ private: int num; public: numgetter() {num=0;} void definenum(int num) {} int getnum() { return num; } };
c++ Syntax (Toggle Plain Text)
#include<iostream> #include"class_header.h" using namespace std; int main() { class numgetter test; int in_num; cout<<"Input the number"; cin>>in_num; test.definenum(in_num); cout<<test.getnum(); system("pause"); }
Why doesnt it have the constructor numgetter() execute when I make the new instance of it, rather all the time-or so it seems?
•
•
Join Date: Nov 2008
Posts: 397
Reputation:
Solved Threads: 72
Your problem is the overloading of the instance of number.
Let me illustrate.
In your case you have put
you could write
Then that should work as intended.
Hope this helps. If not please feel free to repost / or ask different questions.
Let me illustrate.
c++ Syntax (Toggle Plain Text)
int num=6; { int num; // This num is not the same as above num=7; } // In this scope 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)
void definenum(const int X) { num=X; }
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
•
•
Join Date: May 2008
Posts: 577
Reputation:
Solved Threads: 93
The constructor did exactly what you asked it to (as all software should.)
Defines the constructor to initialize the num to zero.
c++ Syntax (Toggle Plain Text)
numgetter() {num=0;}
Defines the constructor to initialize the num to zero.
•
•
Join Date: Aug 2008
Posts: 91
Reputation:
Solved Threads: 0
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.
•
•
Join Date: May 2008
Posts: 577
Reputation:
Solved Threads: 93
definenum is NOT a constructor, it is a method.
The code
When you expand the code to
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." •
•
Join Date: Nov 2008
Posts: 397
Reputation:
Solved Threads: 72
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.
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
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.
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)
#include <iostream> class A { private: int X; int Y; public: A(int X) : X(17),Y(X+A::X) { } void setY(int X) { Y=X; } int getX() const { return X; } int getY() const { return Y; } }; int X=9; int main() { int X=8; A xv(20); std::cout<<"X in class = "<<xv.getX()<<std::endl; std::cout<<"Y in class = "<<xv.getY()<<std::endl; xv.setY(455); std::cout<<"Y in class= "<<xv.getY()<<std::endl; std::cout<<"X = "<<X<<std::endl; std::cout<<"X (globally) = "<<::X<<std::endl; }
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
![]() |
Similar Threads
- Game: Robots (Class, Constructors, etc.) (C++)
- Please help me to convert a simple C++ program into an object-oriented one. (C++)
- Classes again, probably quite simple. (C++)
- Beginning concepts with classes (C++)
- How did you learn C++? (C++)
- Constructor Question? (C#)
- Taking address of constructors?? (C++)
- Need Big Help !! (Java)
- fairly basic c++ code that demonstrates many aspects (C++)
- accessing private data members (C++)
Other Threads in the C++ Forum
- Previous Thread: Card Games
- Next Thread: Tutor says: it takes 25 mins to write this program
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





