- #1
- 4,652
- 38
I'm working on learning how to create classes in C++ and I'm still a little unclear on the syntax for constructor functions and access member functions.
We've been using this class called "rational" for working with rational numbers. Header and implementation files are here.
http://www.math.ucla.edu/~rclark/10a.1.05w/hw6/my/rational.h
http://www.math.ucla.edu/~rclark/10a.1.05w/hw6/my/rational.cpp
My main question is: in the header file, the constructor function looks like this:
rational(int n=0,int d=1) : num(n), den(d) {reduce();}
It creates a variable with two int parts: one for the numerator and one for the denominator.
I don't understand what the colon means here. I know that the int n = 0 and d = 1 are default values, so does the colon mean use the default values unless the user has initialized a value for num or den or both?
And I don't really understand why this assignment of n to num and d to den goes on outside of the curly braces. The only thing he does in the curly braces is call a function to reduce the fraction.
Any comments are appreciated. I use MS VC++ version 6 if that matters.
Thanks.
We've been using this class called "rational" for working with rational numbers. Header and implementation files are here.
http://www.math.ucla.edu/~rclark/10a.1.05w/hw6/my/rational.h
http://www.math.ucla.edu/~rclark/10a.1.05w/hw6/my/rational.cpp
My main question is: in the header file, the constructor function looks like this:
rational(int n=0,int d=1) : num(n), den(d) {reduce();}
It creates a variable with two int parts: one for the numerator and one for the denominator.
I don't understand what the colon means here. I know that the int n = 0 and d = 1 are default values, so does the colon mean use the default values unless the user has initialized a value for num or den or both?
And I don't really understand why this assignment of n to num and d to den goes on outside of the curly braces. The only thing he does in the curly braces is call a function to reduce the fraction.
Any comments are appreciated. I use MS VC++ version 6 if that matters.
Thanks.
Last edited by a moderator: