Question

Feb 14, 2015 at 5:48am
Hi;

Plz tell me, why we use . operator when we are using classes ?


Like Here:

In this line: factor = gcf(numerator, second.numerator);

Fraction::Fraction( long num, long den )
{
int factor;
if( den == 0 )
den = 1;
numerator = num;
denominator = den;
if( den < 0 )
{
numerator = -numerator;
denominator = -denominator;
}
factor = gcf( num, den );
if( factor > 1 )
{
numerator /= factor;
denominator /= factor;
}
}
Fraction Fraction::operator+( const Fraction &second ) const
{
long factor, mult1, mult2;
factor = gcf(numerator, second.numerator);
mult1 = denominator / factor;
mult2 = second.denominator / factor;
return Fraction( numerator * mult2 + second.numerator * mult1,
denominator * mult2 );
}
Feb 14, 2015 at 6:05am
It's called the dot operator. Every class has member functions, which are just functions that only the class can use. In order to access these class functions, we use the dot operator which allows us to use any of it's public member functions. I like to think of it as the way we access the classes possible actions. A human class might have functions like eat(), or sleep(). So, we specify the object, which is human, and then tell it what to do....
Human.eat();
Last edited on Feb 14, 2015 at 6:07am
Feb 14, 2015 at 6:10am
What will "factor = gcf(numerator, second.numerator);
" will do in this case ?

Thanks.
Feb 14, 2015 at 6:20am
Some times why we use const keyword at last, Like as:

Fraction operator+( const Fraction &second ) const;
Feb 14, 2015 at 7:51am
@AqsaAnum, what you need to pick is a good C++ book, not to raise question on every language feature/keyword and stuff. You're much more likely to learn a lot from a book before you ask us what if, while, do...while are, and why C++ is not named ++C or why they chose :: as a scope resolution instead of ;;
Feb 14, 2015 at 8:14am
I have a good C++ book.

Tell me, why C++ language has the name of C++ why not C-- ?

Why we use semicolon at the end of the statement, why not we use : or , ?
Feb 14, 2015 at 8:19am
Lol, are you trolling? Why is your name AqsaAnum? Not BillyBob?
Last edited on Feb 14, 2015 at 8:19am
Feb 14, 2015 at 8:23am
Why do you use a . at the end of your sentence instead of a ~?
Feb 14, 2015 at 8:37am
I don't know but I was asking that due to this reply.

@AqsaAnum, what you need to pick is a good C++ book, not to raise question on every language feature/keyword and stuff. You're much more likely to learn a lot from a book before you ask us what if, while, do...while are, and why C++ is not named ++C or why they chose :: as a scope resolution instead of ;;
Feb 14, 2015 at 8:38am
Find a good c++ book and learn the language, and you'll understand. If you don't know how to ride a bike, asking what a kickstand does is not going to help you ride it. You need to grab a book, and start coding.
Last edited on Feb 14, 2015 at 8:39am
Feb 14, 2015 at 8:42am
I have a good C++ book and This question is from my book. When I could not find answers in my book, what I should do ?
Feb 14, 2015 at 8:43am
Get a different C++ book.
Feb 14, 2015 at 8:44am
ok, thanks.
Feb 14, 2015 at 8:50am
const after a function declaration means that the function is not allowed to change any class members ?
Topic archived. No new replies allowed.