| | |
Classes
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
The syntax may be a little confusing as I was going for relatively concise code and didn't include error checks.
Dereferencing a pointer returned by a function that can return a null pointer to get an object for immediate assignment isn't exactly the smartest thing in the world.
Dereferencing a pointer returned by a function that can return a null pointer to get an object for immediate assignment isn't exactly the smartest thing in the world. New members chased away this month: 5
•
•
•
•
Originally Posted by Narue
Dereferencing a pointer returned by a function that can return a null pointer to get an object for immediate assignment isn't exactly the smartest thing in the world.
I'm asking myself, what the h*ll did she just say
>saying what you said above or saying it in Chinees to me, would have the same result
:cheesy: It basically means that this:
Is generally an unsafe practice. It should be:
:cheesy: It basically means that this:
C++ Syntax (Toggle Plain Text)
T obj = *function();
C++ Syntax (Toggle Plain Text)
T *p = function(); T obj; if ( p != 0 ) obj = *p;
New members chased away this month: 5
> T *p = function();
This means, that your declaring an object as a pointer with a function as variable?
> T obj;
Declaration of an object in a class.
>if ( p != 0 )
obj = *p;
If the value of p is not equal to zero, ... then Ive lost you, you put the pointer into the object???
I'm just trying to break it apart so it's abit clearer to me :!:
This means, that your declaring an object as a pointer with a function as variable?
> T obj;
Declaration of an object in a class.
>if ( p != 0 )
obj = *p;
If the value of p is not equal to zero, ... then Ive lost you, you put the pointer into the object???
I'm just trying to break it apart so it's abit clearer to me :!:
>This means, that your declaring an object as a pointer with a function as variable?
Defining a pointer to T and initializing it with the return value of a function.
>Declaration of an object in a class.
T stands for a generic type, so it could be a class object or an object of built in type.
>If the value of p is not equal to zero
Also read as "if p is not a null pointer".
>you put the pointer into the object???
Assign the data that the pointer points to, to the object.
Defining a pointer to T and initializing it with the return value of a function.
>Declaration of an object in a class.
T stands for a generic type, so it could be a class object or an object of built in type.
>If the value of p is not equal to zero
Also read as "if p is not a null pointer".
>you put the pointer into the object???
Assign the data that the pointer points to, to the object.
New members chased away this month: 5
Hi guys,
Next exercise:
Write a class Fractions to work with Fractions, like this for example:
Fractions a(3, 8) , b(4, 5), c = a * b;
Wich should result into de fractions a, b en c: 3/8, 4/5 and 3/10 (= 3/8 * 4/5).
Clues: Start with the operator* and / and add the more difficult operators + and - afterwards. Use the Greatest Common Divider function wich was used in exercise 4.4 to simplify fractions. When adding and subtracting with fractions you have to make sure that the denominators are made equal for wich you can use GCD function aswell. The Smallest Common Multiplicator for the denominator x and y can be calculated with: SCM(x, y) = xy/ GCD(x, y)
What Ive got so far is the multiplier operator* wich is this:
This works out the way it should, meaning that my results are 3/8, 4/5 and 3/10.
But I was wondering wether this part could be written shorter or better:
I was also wondering wether it wouldn't be better to write a separate class function for this piece of code, so that I maybe could use it for the other operators aswell if this works out to be necessary?
Next exercise:
Write a class Fractions to work with Fractions, like this for example:
Fractions a(3, 8) , b(4, 5), c = a * b;
Wich should result into de fractions a, b en c: 3/8, 4/5 and 3/10 (= 3/8 * 4/5).
Clues: Start with the operator* and / and add the more difficult operators + and - afterwards. Use the Greatest Common Divider function wich was used in exercise 4.4 to simplify fractions. When adding and subtracting with fractions you have to make sure that the denominators are made equal for wich you can use GCD function aswell. The Smallest Common Multiplicator for the denominator x and y can be calculated with: SCM(x, y) = xy/ GCD(x, y)
What Ive got so far is the multiplier operator* wich is this:
C++ Syntax (Toggle Plain Text)
class Fractions { public: Fractions ( short a = 0, short b = 0) : numerator (a), denominator (b){} Fractions operator* (Fractions d) { Fractions result; result.numerator = numerator * d.numerator; result.denominator = denominator * d.denominator; while (result.denominator % 2 == 0 && result.numerator % 2 == 0) { result.numerator /= 2; result.denominator /= 2; } return result; } void print() { cout<< numerator << "/" << denominator <<endl; } private: short numerator, denominator; }; int main() { Fractions fract1 (3, 8), fract2 (4, 5), c; fract1.print(); fract2.print(); c = fract1 * fract2; c.print(); return 0; }
This works out the way it should, meaning that my results are 3/8, 4/5 and 3/10.
But I was wondering wether this part could be written shorter or better:
C++ Syntax (Toggle Plain Text)
Fractions operator* (Fractions d) { Fractions result; result.numerator = numerator * d.numerator; result.denominator = denominator * d.denominator; while (result.denominator % 2 == 0 && result.numerator % 2 == 0) { result.numerator /= 2; result.denominator /= 2; } return result; }
I was also wondering wether it wouldn't be better to write a separate class function for this piece of code, so that I maybe could use it for the other operators aswell if this works out to be necessary?
For reducing the fraction, a separate function that finds the greatest common divisor would be a good idea. Then you should define an explicit reduce member function since it's an operation you'll use a lot. It would also be a good idea for operator* to be a non-member function since it doesn't have to be called on behalf of either operand. Then you could change your operator* to:
C++ Syntax (Toggle Plain Text)
void Fraction::reduce() { short div = gcd ( numerator, denominator ); numerator /= div; denominator /= div; } Fraction operator* ( const Fraction& a, const Fraction& b ) { Fraction result; result.numerator = a.numerator * b.numerator; result.denominator = a.denominator * b.denominator; result.reduce(); return result; }
New members chased away this month: 5
Hi Narue,
Thanks for the explanation, few questions tough before I go further with this exercise.
> It would also be a good idea for operator* to be a non-member function...
1) Do you mean that it would be best to write this function outside the class? Don't understand what you're trying to explain to me :-|
2) why do you make Fraction constant?
3) why do you make two arguments and make them references? I tought that the first (numerator (3), denominator(8)) goes to operator* and the second (numerator(4), denominator(5)) goes to the argument as in my program, so, I tought there wasn't a second argument necessary?
4) So, the only thing I have to write the code for is the function correct?
5) Also, when I change my code like you wrote I get an error saying that Ive got to many parameters, I'm sure it's got to do with this piece of code in main It's mentioned in the exercise to write it this way in main!
Thanks for the explanation, few questions tough before I go further with this exercise.
> It would also be a good idea for operator* to be a non-member function...
1) Do you mean that it would be best to write this function outside the class? Don't understand what you're trying to explain to me :-|
C++ Syntax (Toggle Plain Text)
Fraction operator* ( const Fraction& a, const Fraction& b )
3) why do you make two arguments and make them references? I tought that the first (numerator (3), denominator(8)) goes to operator* and the second (numerator(4), denominator(5)) goes to the argument
C++ Syntax (Toggle Plain Text)
Fraction d
4) So, the only thing I have to write the code for is the
C++ Syntax (Toggle Plain Text)
gcd ( numerator, denominator );
5) Also, when I change my code like you wrote
C++ Syntax (Toggle Plain Text)
Fraction operator* (const Fraction& a, const Fraction& b)
C++ Syntax (Toggle Plain Text)
c = fract1 * fract2;
>Do you mean that it would be best to write this function outside the class?
Yes. Instead of this:
You would do this:
I forgot to mention making it a friend in my other post, sorry.
>2) why do you make Fraction constant?
Because you don't change any of the data members. By making the parameters const, you'll be warned if any changes made to the function try to make modifications. More importantly, you can pass constant objects to the function:
>3) why do you make two arguments and make them references?
operator* can be either a member function with one argument, or a non-member function with two arguments. When it's a member function, the object that you call the function on acts as the left side operand and the argument acts as the right side operand. When it's a non-member function, the first argument is the left side operand and the second argument is the right side operand.
The reason I used a non-member function is because you should strive to keep your class interface as sparse as possible. Any operations that can be made non-member functions or non-member friends should be. If we were talking about operator*= then it would be a member function, but operator* doesn't need to be.
It's generally best to pass class objects by reference to avoid the overhead of calling the copy constructor. If the object isn't going to be changed, pass by const reference.
>So, the only thing I have to write the code for is the
Correct.
>I get an error saying that Ive got to many parameters
You're still defining it as a member function. See my second example using a friend and match your code to that.
>It's mentioned in the exercise to write it this way in main!
It's always written that way unless you want to do it without the abstraction. Your error is trying to mix a non-member operator overload with a member operator overload. You have to do one or the other.
Yes. Instead of this:
C++ Syntax (Toggle Plain Text)
class Fraction { public: Fraction operator* ( const Fraction& rhs ); ... }; Fraction Fraction::operator* ( const Fraction& rhs ) { ... }
C++ Syntax (Toggle Plain Text)
class Fraction { public: friend Fraction operator* ( const Fraction& lhs, const Fraction& rhs ); ... }; Fraction operator* ( const Fraction& lhs, const Fraction& rhs ) { ... }
>2) why do you make Fraction constant?
Because you don't change any of the data members. By making the parameters const, you'll be warned if any changes made to the function try to make modifications. More importantly, you can pass constant objects to the function:
C++ Syntax (Toggle Plain Text)
const Fraction a ( 2, 3 ); const Fraction b ( 5, 10 ); Fraction c = a * b;
operator* can be either a member function with one argument, or a non-member function with two arguments. When it's a member function, the object that you call the function on acts as the left side operand and the argument acts as the right side operand. When it's a non-member function, the first argument is the left side operand and the second argument is the right side operand.
The reason I used a non-member function is because you should strive to keep your class interface as sparse as possible. Any operations that can be made non-member functions or non-member friends should be. If we were talking about operator*= then it would be a member function, but operator* doesn't need to be.
It's generally best to pass class objects by reference to avoid the overhead of calling the copy constructor. If the object isn't going to be changed, pass by const reference.
>So, the only thing I have to write the code for is the
Correct.
>I get an error saying that Ive got to many parameters
You're still defining it as a member function. See my second example using a friend and match your code to that.
>It's mentioned in the exercise to write it this way in main!
It's always written that way unless you want to do it without the abstraction. Your error is trying to mix a non-member operator overload with a member operator overload. You have to do one or the other.
New members chased away this month: 5
![]() |
Similar Threads
- need idea for project using classes and inheritance (C++)
- How do i do chat program using MFC (Microsoft Foundation Classes) and Visual Basic? (Visual Basic 4 / 5 / 6)
- Loading classes from a .jar file (Java)
- Help with Classes (C++)
- Understanding classes (C++)
- Working with objects of different Classes (Java)
- Classes (C++)
- Summer Classes (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: why won't my virtual area function work?
- Next Thread: minor problem with vectors and OOP
Views: 5569 | Replies: 44
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






