Question

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 );
}
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
What will "factor = gcf(numerator, second.numerator);
" will do in this case ?

Thanks.
Some times why we use const keyword at last, Like as:

Fraction operator+( const Fraction &second ) const;
@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 ;;
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 , ?
Lol, are you trolling? Why is your name AqsaAnum? Not BillyBob?
Last edited on
Why do you use a . at the end of your sentence instead of a ~?
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 ;;
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
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 ?
Get a different C++ book.
ok, thanks.
const after a function declaration means that the function is not allowed to change any class members ?
Topic archived. No new replies allowed.