Doh, sorry to ask, such simple solution :o
JoBe 36 Posting Pro in Training
JoBe 36 Posting Pro in Training
Hello ladies and gents,
Ive got a question concerning how to actually send a program to someone (only the exe.file) so that he/she sees what the program can do without having the possibility to alter anything towards the code itself?
I'm using Microsoft Visual C++ 6.0 incase you need to know?
JoBe 36 Posting Pro in Training
Hi guys,
Due to trying to understand what the operator< actually was ment to do, I was trying something like this:
class point
{
private:
int x, y, z;
public:
point (int xx = 0, int yy = 0): x (xx), y (yy){}
vector<int> points;
void numbers()
{
while (cin>> x >> y, !cin.fail())
{
points.push_back(x);
points.push_back(y);
}
}
bool operator< (const point &b)
{
return x < b.x || (x == b.x && y < b.y);
}
void print()
{
int n = points.size();
for (z = 0; z < n; z++)
cout<< points[z] <<endl;
}
};
int main()
{
point p, q (10, 16);
int a;
cout<<"Type twee integere getallen in per keer: "<<endl;
p.numbers();
p.print();
if (p < q){a = 0;}
else{a = 1;}
cout<< a;
return 0;
}
Wich shows that the operator compares the object p with the object q and depending on the result will returns bool (0 or 1) to main.
Is that the reason why in the exercise there is written
Use the STL-algorithm 'sort' to sort the previous entered (pairs) x and y by using the defined order operator <.
But instead with two different objects, you use is as is written on the pairs by determining wether the first number is smaller then the second, if not then you use the 'sort' algorithm to put them in the correct order?
Sorry to ask this, but I'm still confused about the actual intention of this part of …
JoBe 36 Posting Pro in Training
Ok, thanks for the explanation Dave, I'll give it a test run to see how it works!
JoBe 36 Posting Pro in Training
Sorry DAve,
But let me get this straight, you're saying that
std::sort(points.begin(), points.end());
Is related to this part
class point
{
bool operator< (const point &b)
{
return x < b.x || (x == b.x && y < b.y);
}
friend bool operator< (const point &a, const point &b);
};
bool operator< (const point &a, const point &b)
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
Now I'm really confused :confused:
Is this like you are determinating one bool operator first and that solution is given to the friend operator as one of the parameters??
JoBe 36 Posting Pro in Training
Wait Dave,
What's that got to do with
std::sort(points.begin(), points.end());
As I said in the previous post, I'm not working with the operator functions just yet.
JoBe 36 Posting Pro in Training
Sorry Dave,
Tought you wouldn't need the total code and therefore posted only those parts. Don' worry about the operators '==' '<', haven't gotten them in working order yet ;)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class point
{
private:
int x, y, z;
public:
point (int xx = 0, int yy = 0): x (xx), y (yy){}
vector<int> s;
void numbers()
{
while (cin>> x >> y, !cin.fail())
{
s.push_back(x);
s.push_back(y);
}
}
/*void sort()
{
std::sort(s.begin(), s.end());
}*/
/*bool operator==(const point &a, const point &b)
{
return a.x == b.x && a.y == b.y;
}
bool operator< (const point &a, const point &b)
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
}*/
void print()
{
int n = s.size();
for (z = 0; z < n; z++)
cout<< s[z] <<endl;
}
};
int main()
{
point u;
vector<point>points;
cout<<"Type twee integere getallen in per keer: "<<endl;
u.numbers();
u.print();
//u.sort();
std::sort(points.begin(), points.end());
u.print();
cin.get();
return 0;
}
JoBe 36 Posting Pro in Training
Hi guys,
Why am I able to write this:
#include <iostream>
#include <vector>
#include <algorithm>
class point
{
...
public:
...
void sort()
{
std::sort(s.begin(), s.end());
}
...
};
int main()
{
point u;
...
u.sort();
...
}
But not this:
class point
{
...
};
int main()
{
...
vector<point>points;
std::sort(points.begin(), points.end());
...
}
JoBe 36 Posting Pro in Training
Why do people like you, Narue, Dave,... make it all look so simple, pfff, life isn't fair you know LOL :lol:
Thanks Dogtree, though, have one favor to ask you, if possible, could you give me some hints and clues in the future or even examples wich are related to the exersize instead of the solution because, that way, I'll learn much more then by simply getting the solution on a silver platter.
Please, don't missunderstand me, I really appreciate your help, but, I'd like to try the exersize first, if I really am not able to solve it, after getting tips, hints, examples, from you, Dave, Narue, Vegaseat, Asif, 1OOOBHP,... I'll ask you to help me out with the solution ;)
Anyway, thanks for the solution, this way, I'll get to the next chapter much faster :cheesy:
JoBe 36 Posting Pro in Training
Hey Dogtree,
Thanks for the explanation man ;)
But euh, ... if you don't mind, I'm going to skip it in this exersize since it's not being asked to implement it, I'm having a hard time allready getting it to do what it should without incorporating some more code :cheesy:
But, greatly appreciated man :!:
JoBe 36 Posting Pro in Training
>Do you want a vector of points instead?
Euh, no, don't think I need that, but thanks for the example, why would you use this?
The translation of the exersize is:
Define a class "point" with two datamembers, x and y. Use the STL-container vector to stack a row of point-objects. Enter them by using the keyboard by two(pairs), x and y. It's not known how many (pairs) there will be entered. Use a non numeric character to stop the input of the (pairs) x and y.
(<-- so far so good, no problem)
But this part of the exersize, I don't really understand what I'm supposed to do:
Define the operators == and < for two point-objects in wich p<q has the following meaning: p.x < q.x || (p.x == q.x && p.y < q.y)
Use the STL-algorithm 'sort' to sort the previous entered (pairs) x and y by using the defined order operator <.
I understand that those operators "== and <" are linked towards this: "p.x < q.x || (p.x == q.x && p.y < q.y)" But, don't know how this should get incorporated into a piece of code?
>[voice=Sgt. Shultz]I know nothing.[/voice]
LOL :lol:
[voice=Homer].....DOH,.. ssstupid policeman[/voice]
JoBe 36 Posting Pro in Training
Hi Dave,
Ive allready changed the cin part :)
Input for example:
10 20
30 40
50 60
stop
Output should be and is now:
10
20
30
40
50
60
This is just a small part of the exersize tough, have to include the operators == and <.
But, the exersize also talkes about two point-objects?? I'm I correct in thinking that this is when a class object has two datamembers?
Something like this,
class point
{
point (int xx = 0, int yy = 0): x (xx), y (yy){} --> two datamembers x and y.
...
};
int main()
{
point u;--> one class object
...
}
If you don't understand what I'm trying to say, then I'll translate the entire exersize.
By the way, have you heard anything from Narue, strange for her to stay away for so long :?:
JoBe 36 Posting Pro in Training
Found it, it's this wich was causing the problem
>>" ">>
Still, if someone might want to have a look at the part of code that I wrote allready and give some hints or suggestions to improve it, I'd very much appreciate it ;)
JoBe 36 Posting Pro in Training
Hello ladies and gents,
Wanted to make this part of an exercise in wich I enter each time two integers into an array for wich I have to use STL container vector.
Ive got this sofar, but getting an error message when I entered two numbers and the exe shuts down? I'm almost certain it's got to do with the
while (cin>> x >>" ">> y, !cin.fail())
But, not sure about it and even if, don't know how to solve it?
What Ive got sofar in total is this:
#include <iostream>
#include <vector>
using namespace std;
class point
{
private:
int x, y, z;
public:
point (int xx = 0, int yy = 0, int zz = 0): x (xx), y (yy), z (zz) {}
vector<int> s, t;
void numbers()
{
while (cin>> x >>" ">> y, !cin.fail())
{
s.push_back(x);
s.push_back(y);
}
}
void print()
{
int n = s.size();
for (z = 0; z < n; z++)
cout<< s[z] <<endl;
}
};
int main()
{
point u;
cout<<"Type twee integere getallen in per keer: "<<endl;
u.numbers();
u.print();
cin.get();
return 0;
}
Could someone please point me in the right direction, thank you ;)
JoBe 36 Posting Pro in Training
Was wondering wether you could help me out with a few questions Dogtree?
class LinkedList
{
Node<T> *_head;
public:
...
1) By writing Node above public, does that mean it's automatically private?
2) Why do you use and underscore before a member? Habit?
3) Isn't it possible to write the definition of iterator on the top of main in combination with list?
Like you can write
int i = 0, j, k = 4;
Why can't I write
LinkedList<int> :: iterator it, list;
:?:
4) When I rewrite the code and come towards 'std::', I get a list with several possibility's.
a) What exactly are those several options.
b) Why isn't cout included in that list but still possible to use?
Thanks for the help.
JoBe 36 Posting Pro in Training
Thanks Dogtree,
It's because of Narue's example has so manny whistles and bells that I couldn't make anything out of it, also, I couldn't debug it and give it a spin, usually I can comprehend it much better when Ive got an example.
Didn't say that it was a bad example ;)
I do wonder wether it's not possible to use the container <vector> in this exercise or is this used only when you have to manually input variables?
JoBe 36 Posting Pro in Training
Your wish, is our command ;)
Was there, noone around tough :D
JoBe 36 Posting Pro in Training
Hi Narue,
I know it's been a short while, but, I tried to run your program and it's given me errors when I debugged it.
Could you help me out please and simplify your program so that I could give it ago and see what you're doing and how :?:
JoBe 36 Posting Pro in Training
>> I put less than 30 minutes work into it, sue me.
Nah, I'll let you of the hook, ... this time :cheesy:
Thanks Narue ;)
JoBe 36 Posting Pro in Training
Good evening everyone,
This next exercise, I have to do the following:
Write a ClassTemplate for a linked list. Demonstrate it by using two linked lists. The datafield of the first type is int, the second one is double.
What Ive got sofar is this:
#include <iostream>
#include <vector>
using namespace std;
template <class T>
class vec
{
public:
vec(T xx = 0, T yy = 0, T zz = 0): x(xx), y(yy), z(zz){} // Constructor.
void linklist(T &x, T &y, T &z);
void print() const;
private:
T x, y, z;
};
template <class T>
void vec<T>::print() const
{
cout<< x << " " << y << " " << z <<endl;
}
template <class T>
void vec<T>::linklist(T &x, T &y, T &z)
{
??????????
}
int main()
{
vec <int> a(1), b(4), c(9), ilist;
vec <double> d(4.5), e(9.7), f(0.3), dlist;
linklist(a, b, c);
linklist(d, e, f);
return 0;
}
I don't think it really is good, that's why I wanted to ask if someone could point me in the correct direction.
I think I have to use the template <vector>, but, how do I combine that with the template class?????
JoBe 36 Posting Pro in Training
>> You may not believe me, but my version is actually simpler than yours.
:lol: I knew you would say this.
Well, thanks for the example and additional info Narue ;)
JoBe 36 Posting Pro in Training
Thanks for the example Narue, I was thinking about something simpler :lol:
Could you explain the reasons why you would prefer the way you wrote it, over mine please.
I understand that by using 'swap' , you don't have to make sure that your calculations are correctly written.
Seeing as you can use swap, isn't there a possibility to use sort? Something like this for instance?
#include <algorithm>
template <typename T>
void order ( T& a, T& b, T& c )
{
sort ( a, b, c);
}
I tried it like this, doesn't work :confused:
JoBe 36 Posting Pro in Training
Hi Narue,
The idea is sort three numbers/strings and sort them beginning with the smallest number or sort them alphabetically!
JoBe 36 Posting Pro in Training
Hello ladies and gents,
I wanted to ask if this piece of code can be written differently then the way I did:
template <class V>
void sequence3(V &a, V &b, V &c)
{
V w;
while (a > b || b > c)
{
if (a > b){w = b; b = a; a = w;}
if (b > c){w = c; c = b; b = w;}
if (a > c){w = a; a = c; c = w;}
}
}
JoBe 36 Posting Pro in Training
Thanks for the links Dave, I'll read them first and when Ive got questions, I'll be back ;)
JoBe 36 Posting Pro in Training
Hello ladies and gents,
Ive got this example of a program that I tried out wich shows some special possibilities to use new.
#include <iostream>
using namespace std;
int main()
{
int a[100]= {0};
for (int i = 0; i < 100; ++i)
a[i] = 100 * i;
int *p = new(a) int [5];
for (int j = 0; j < 5; j++)
cout<< p[j] << " "; // 0 10 20 30 40
cout<<endl;
double *pd = new (a + 5) double;
*pd = 12.34;
cout<< *pd <<endl; // 12.34
float *pf = new (a + 50) float (5.6F);
cout<< *pf <<endl; // 5.6
return 0;
}
Now, as a good hobbyist I'm trying to be, I tried to delete the pointer with
delete p;
But, euh..., that didn't work and gave my computer almost a hart attack :)
So I figured, it's got to do with the fact that the array a is connected to the pointer.
If I'm correct, then problem is, how do I delete it, do I use a loop in wich I delete everything in the array. Because I tought you only had to delete the pointer pointing to the first place?
Ive tried to use this:
delete [] p;
but got the same result, get message:
Debug Assertion Failed!
file: dbgheap.c
Line:1011
ANy help would be greatly appreciated ;)
JoBe 36 Posting Pro in Training
Yep, that made it much clearer, thanks ;)
JoBe 36 Posting Pro in Training
>>A zero in pointer context or the macro NULL will both be compiled as the value of a null pointer constant. (You don't set a pointer variable's address.)
Sorry Dave, could you explain this abit more, or maybe in another way? Don't exactly understand what your trying to explain?
Thanks for the additional link, I'll read that first before moving on.
>>Because if you did the following, it would not be the same.
char* p, *q;
pointer p, q;
So, in other words, what was explained in my book with the example It gave, isn't correct?
>> ...obfuscatory.
Wow, had to look that up in a dictionary, understand what it means now ;) Thanks for the English lesson Dave. I'm not only learning C++, but even improving my English here :D
JoBe 36 Posting Pro in Training
Hello ladies and gents,
Ive been reading about pointers abit and at there are two pieces of text in the book about pointers wich aren't clear to me, was wondering if any of you could enlighten me abit about them! :D
Sometimes it's desired to clearly point out that a certain pointer doesn't point to anything. We can give that pointer a value of 0. To declare this value, we have to write it either with 0 or as most of the times NULL. THis is however not a determinated word, but a constant wich in many headers, like iostream is defined like that. Another value for a pointer then 0 is not possible.
double *p; p = 0; // valid p = NULL; // valid p = 123; // not valid
1) QUESTION IS: how can a pointer be NULL or 0 when it's an adress wich gives a value?
Because, as I understand it to be like this:
Expression----------------Type--------------Value
p-------------------------pointer-to-double--an adress
*p------------------------double-------------(12.34)
Or, is it saying that when declaring p = NULL;, the adress of that pointer is 0x00000000 and there is no value stacked in this pointer?
Also, when do you use this?
2) Can someone explain what the use is of typedef and how I can interpret this explanation?
When this is declared: typedef char *pointer
the pointer becomes an indication of the type char*. This means that the following two declarations are equivalent:
char *p;
JoBe 36 Posting Pro in Training
Polymorphism, hmmm...polymorphism oh... yep found it --> Chapter 7 Section 4 :cheesy:
Guess It'll have to wait abit before I get there, but, great to hear there is a way to avoid so many cout's :D
Well, I'm starting Chapter 5 now, Templates, pointers and datastructures :!:
Thanks for the help Narue, going to read now about 40 pages, after that, it's exercise time again ;)
Class solved.
JoBe 36 Posting Pro in Training
>It depends on your needs. What you have is a valid solution
Then I'll leave it as it is.
>Unless something like this is acceptable then no:
Then I'll leave it as it is.
>Can you rephrase that?
Is there a way that when I write the operator I'm using in main it automatically writes a piece of code that explains what I'm going to do?
Like for instance when I type: c = fract1 * fract2; when executed, I would get automatically: Multiplied: - 1/2.
This way, I don't have to write cout's everytime I use a version of the operators.
Could this be done by writing the couts in the memberfunctions themselves?
I was thinking of grouping the text into one member function and depending on wich operator is used in main, call that particular piece of text?
Does that make sence :-|
JoBe 36 Posting Pro in Training
Wich do you consider being a good (affordable) compiler?
void print()
{
(denominator < 0 ? cout<<"- " << numerator <<"/" << - denominator :
cout<< numerator <<"/" << denominator <<endl);
}
Ive been changing this piece of code so that it takes account of negative fractions, this way I get - 1/2 for example instead of 1/ -2.
Would you have done this in a separate function or like I did in void print()?
Is there a way that I could alter this code so that I could write
_ (1_2 <-- underneath eachother) in a relative simple way? Or do I have to make serious adjustments to achieve that?
cout<<"Fractions 1 and 2 are:"<<endl;
cout<<"Multiplied:"<<endl;
cout<<"Divided:"<<endl;
cout<<"Added:"<<endl;
cout<<"Subtracting:"<<endl;
Is there a way to decrease the amount 'cout' in main but still have the possibility to write a destinctive name?
JoBe 36 Posting Pro in Training
Hi Narue,
What I did:
File --> New --> Win32 Console Application --> Project Name --> An empty project --> Add files to folder --> choose --> C++ Source Files (.c;.cpp;.cxx;.tli) --> write --> filename (name project + .cpp) --> click on New Text File or double click on name project.cpp in the workspace to open to create new file --> copy code into new file --> Ctrl F7 --> result, the same ERROR code once again :sad:
Ive tried the same with another exercise in wich operators + and - weren't used and it worked like a charm :rolleyes:
I give up Narue, I'll change the + and - operator towards the Fraction operator+ (const Fraction& secval) type. This way, Ive got both ways shown in one exercise :)
JoBe 36 Posting Pro in Training
Well, I'm glad the code is working atlast :cheesy: Thanks for the help Narue.
Wouldn't mind trying out to make a project without those pre compiled headers :D
Only thing is, how do I do this :?:
Ive allready tried to but without any succes, could you tell me the steps I had to take to run my program succesfully?
Choose new --> Win32 Console Application --> Project Name --> An empty project. Sofar so good, but then.
I get on the left side the project name(workspace) and the right side is empty, how do I connect those two?
Click New Text File?
Copy my code into that?
When I do that, I can copy the header files aswell, but they do not show up in the workspace?
So, big question is, how do I link them together, meaning workspace and the program I copy?
JoBe 36 Posting Pro in Training
Nope, not working, tried closing compiler, copy code into new program, still getting error message:
AOef4_8.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\OefeningenLeenAmmeraal\LAOef4_8\LAOef4_8.cpp(30) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Error executing cl.exe.
LAOef4_8.obj - 1 error(s), 0 warning(s)
THe code I'm trying to compile is:
#include "stdafx.h"
#include <iostream>
using namespace std;
short gcd(short numerator, short denominator);
class Fraction
{
public:
Fraction ( short a = 0, short b = 0) : numerator (a), denominator (b){}
friend Fraction operator* (const Fraction& firval, const Fraction& secval);
friend Fraction operator/ (const Fraction& firval, const Fraction& secval);
friend Fraction operator+ (const Fraction& firval, const Fraction& secval);
friend Fraction operator- (const Fraction& firval, const Fraction& secval);
Fraction reduce();
void print()
{
cout<< numerator << "/" << denominator <<endl;
}
private:
short numerator, denominator;
};
Fraction operator* (const Fraction& firval, const Fraction& secval)
{
Fraction result;
result.numerator = firval.numerator * secval.numerator;
result.denominator = firval.denominator * secval.denominator;
result.reduce();
return result;
}
Fraction operator/ (const Fraction& firval, const Fraction& secval)
{
Fraction result;
result.numerator = firval.numerator * secval.denominator;
result.denominator = firval.denominator * secval.numerator;
result.reduce();
return result;
}
Fraction operator+ (const Fraction& firval, const Fraction& secval)
{
Fraction result;
short mult = gcd(firval.denominator, secval.denominator);
result.denominator = (firval.denominator * secval.denominator) / mult;
result.numerator = (firval.numerator * secval.denominator) + (secval.numerator * firval.denominator);
result.reduce();
return result;
} …
JoBe 36 Posting Pro in Training
DAMN :(
Fraction operator+ (const Fraction& firval, const Fraction& secval)
{
Fraction result;
short mult = gcd(firval.denominator, secval.denominator);
result.denominator = (firval.denominator * secval.denominator) / mult;
result.numerator = (firval.numerator * secval.denominator) + (secval.numerator * firval.denominator);
result.reduce();
return result;
}
I'm almost certain that gcd(firval.denominator, secval.denominator); is correct, because it's those two value's that I need to get the correct returnvalue.
Problem is, I'm still getting the same error, has it got to do with the definition of function gcd( short numerator, short denominator); ??????
I'm sorry Narue, I can't seem to find the correct solution :!:
JoBe 36 Posting Pro in Training
Narue, I'll show you two versions,
When using: friend fraction operator+ (...) it doesn't work!
#include "stdafx.h"
#include <iostream>
using namespace std;
short gcd(short numerator, short denominator);
class Fraction
{
public:
Fraction ( short a = 0, short b = 0) : numerator (a), denominator (b){}
friend Fraction operator* (const Fraction& firval, const Fraction& secval);
friend Fraction operator/ (const Fraction& firval, const Fraction& secval);
friend Fraction operator+ (const Fraction& firval, const Fraction& secval);
Fraction reduce();
void print()
{
cout<< numerator << "/" << denominator <<endl;
}
private:
short numerator, denominator;
};
Fraction operator* (const Fraction& firval, const Fraction& secval)
{
Fraction result;
result.numerator = firval.numerator * secval.numerator;
result.denominator = firval.denominator * secval.denominator;
result.reduce();
return result;
}
Fraction operator/ (const Fraction& firval, const Fraction& secval)
{
Fraction result;
result.numerator = firval.numerator * secval.denominator;
result.denominator = firval.denominator * secval.numerator;
result.reduce();
return result;
}
Fraction operator+ (const Fraction& firval, const Fraction& secval)
{
Fraction result;
short mult = gcd( numerator, denominator);
result.denominator = (denominator * secval.denominator) / mult;
result.numerator = (numerator * secval.denominator) + (secval.numerator * denominator);
result.reduce();
return result;
}
Fraction Fraction::reduce()
{
short div = gcd(numerator, denominator);
numerator /= div;
denominator /= div;
return *this;
}
int main()
{
Fraction fract1 (1, 3), fract2 (2, 9), c;
cout<<"Fraction 1 and 2 are:"<<endl;
fract1.print();
fract2.print();cout<<endl;
cout<<"Multiplied:"<<endl;
c = fract1 * fract2;
c.print();cout<<endl;
cout<<"Divided:"<<endl;
c = fract1 / fract2;
c.print();cout<<endl;
cout<<"Added:"<<endl;
c = fract1 + fract2;
c.print();cout<<endl;
cin.get();
return 0;
}
short gcd(short numerator, short denominator)
{
short temp;
do
{ …
JoBe 36 Posting Pro in Training
Ok, I was trying out the + operator but it's not finished yet.
#include "stdafx.h"
#include <iostream>
using namespace std;
short gcd(short numerator, short denominator);
class Fraction
{
public:
Fraction ( short a = 0, short b = 0) : numerator (a), denominator (b){}
friend Fraction operator* (const Fraction& firval, const Fraction& secval);
friend Fraction operator/ (const Fraction& firval, const Fraction& secval);
friend Fraction operator+ (const Fraction& firval, const Fraction& secval);
Fraction reduce();
void print()
{
cout<< numerator << "/" << denominator <<endl;
}
private:
short numerator, denominator;
};
Fraction operator* (const Fraction& firval, const Fraction& secval)
{
Fraction result;
result.numerator = firval.numerator * secval.numerator;
result.denominator = firval.denominator * secval.denominator;
result.reduce();
return result;
}
Fraction operator/ (const Fraction& firval, const Fraction& secval)
{
Fraction result;
result.numerator = firval.numerator * secval.denominator;
result.denominator = firval.denominator * secval.numerator;
result.reduce();
return result;
}
Fraction operator+ (const Fraction primval, const Fraction& secval)
{
Fraction result;
result.denominator = scd(numerator, denominator);
result.numerator = numerator + secval.numerator;
return result;
}
Fraction Fraction::reduce()
{
short div = gcd(numerator, denominator);
numerator /= div;
denominator /= div;
return *this;
}
int main()
{
Fraction fract1 (3, 8), fract2 (4, 5), c;
fract1.print();
fract2.print();
c = fract1 * fract2;
c.print();
c = fract1 / fract2;
c.print();
c = fract1 + fract2;
c.print();
return 0;
}
short gcd(short numerator, short denominator)
{
short temp = 0;
do
{
temp = (denominator% numerator);
denominator = numerator;
numerator = temp;
}
while (denominator% temp != 0);
return temp;
}
short scd(short numerator, short denominator) …
JoBe 36 Posting Pro in Training
Hi Narue,
Ive been writing the operator/ and didn't have any problems since it's just a small difference towards the operator* (switching numerator/operator from the second value).
I wrote it like you did in your example and it worked out fine.
Now, I'm writing the operator+ syntax the same way as the previous once, but I'm getting an error message saying this:
C:\Program Files\Microsoft Visual Studio\MyProjects\OefeningenLeenAmmeraal\LAOef4_8\LAOef4_8.cpp(30) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information :eek:
I read the information given on the Help menu, but euh, well euh, remember that CHINEES thingy :cheesy:
Question is, could you please explain WHY this is happening :?:
When I rewrite this without using 'friend' I offcourse get the message that I have to many parameters/arguments. Changing this to one argument and using Fraction Fraction::operator+ (....) gives no problem and works out as it should.
JoBe 36 Posting Pro in Training
damn, this is confusing, im staying out of other people's threads :D
Very much appreciated :!:
JoBe 36 Posting Pro in Training
>I forgot to mention making it a friend in my other post, sorry.
Oh Narue, don't worry about that, I'm glad that you take the effort of helping me out and explaining those things ;)
>Any operations that can be made non-member functions or non-member friends should be
1) How do you know when it has to be included into a class?
2) Does this mean that considering I have to write the operators /, + and - aswell, I could best write does outside of the class and use a friend?
>operator*=
Which is the use of this operator?
JoBe 36 Posting Pro in Training
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 :-|
Fraction operator* ( const Fraction& a, const Fraction& b )
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
Fraction d
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
gcd ( numerator, denominator );
function correct?
5) Also, when I change my code like you wrote
Fraction operator* (const Fraction& a, const Fraction& b)
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
c = fract1 * fract2;
It's mentioned in the exercise to write it this way in main!
JoBe 36 Posting Pro in Training
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:
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 …
JoBe 36 Posting Pro in Training
> 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 :!:
JoBe 36 Posting Pro in Training
Why go anywhere else if we have people like Narue, Dave, Vegaseat, Asif, ... to ask questions towards, besides, Daniweb has got alot of tutorials aswell :!:
JoBe 36 Posting Pro in Training
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.
You know, saying what you said above or saying it in Chinees to me, would have the same result :cheesy:
I'm asking myself, what the h*ll did she just say ;)
JoBe 36 Posting Pro in Training
Thanks for the answers Narue, Ive been debugging your example, but several parts are to complicated for me to understand.
I thinks this is because there are several subjects Ive either just scratched the surface from (pointers, structures) and others I never read anything about yet.
JoBe 36 Posting Pro in Training
Waw Narue, that's what you could call, changing the program :D
I'm a bit baffled :eek: and actually don't know wich question to ask first :?:
Well, here it goes:
1) > I wouldn't waste my time performing the calculations manually:
Is that why you are using the header file <ctime> ?
2) When I look at what I wrote and what you wrote, yours is much more complicated! So, what advantage has your version got over mine?
- Speed?
- Portability?
- ...
Questions about my program:
3) What is the difference in using *this and time(hours, minutes) in my program, because I switched these and both work perfectly in returning the values of hours and minutes :?:
- I know '*this' is a pointer, but pointer for/from what ?
- By using time(hours, minutes), is this related towards the constructor ?
4) Maybe a stupid question, but does it matter in wich order you write private and public in a class?
5) Could I write this piece shorter, not using your alternative?
hours < 10 ? cout<<"The time is now: " << setw(2) << hours <<" Hr "<< minutes << "." <<endl:
cout<<"The time is now: " << setw(0) << hours <<" Hr "<< minutes << "." <<endl;
There are other questions about your code, but I'm gonna give it several test drives to see wether I'm able to understand what's happening in …
JoBe 36 Posting Pro in Training
I GOT IT :D Wohooooo :cheesy:
#include <iostream>
#include <iomanip>
using namespace std;
class time
{
public:
time (int uren = 0, int minuten = 0) : hours (uren), minutes (minuten) {}
time operator+ (int min)
{
minutes += min;
while (minutes >= 60)
{
minutes -= 60;
hours += 1;
if (hours >= 24) hours = 0;
}
return *this;
}
void print()const
{
hours < 10 ? cout<<"The time is now: " << setw(2) << hours <<" Hr "<< minutes << "." <<endl:
cout<<"The time is now: " << setw(0) << hours <<" Hr "<< minutes << "." <<endl;
}
private:
int hours, minutes;
};
int main()
{
time t0(23, 47), t1;
t0.print();
t1 = t0 + 59;
t1.print();
return 0;
}
Hi guys,
Hehe, Ive got a few questions, but the first one that I'm gonna ask is, what would you do different or what would you simplify in this exercise?
One thing to keep in mind is, in main you have to use this code:
time t0(23, 59), t1; // t0 = 23 Hr 59.
t1 = t0 + 120; // t1 = 1 Hr 59.
Rest of the questions will come later, thanks ;)
JoBe 36 Posting Pro in Training
Oh, now I understand, almost got it working tough, even with the use of class time ;)
I'm using Visual C 6.0, wich one do you use?
Only have to figure out how to correctly add the minutes towards the hour, but I'm almost there, I'll show you when it's finished so you could comment on it and perhaps improve it :)
Ive got other questions aswell, but I'll ask them when I figured out the program.
Thanks for the help Dave :)