Fragments
anupthapa (4)
Oct 14, 2012 at 11:07pm UTC
What is the output for the following code fragments?
cin >> rhubarb;
rhubarb = rhubarb( rhubarb );
cout << rhubarb;
Need explanation as well.
andywestken (1950)
Oct 14, 2012 at 11:39pm UTC
We need a more complete program.
Can you please post the rest of your code along with the fragment, which I presume compiles cleanly.
Andy
anupthapa (4)
Oct 15, 2012 at 12:55am UTC
Sorry, the tutor has given only these lines of codes and nothing more. We have to write the output with expanation as well.
andywestken (1950)
Oct 15, 2012 at 1:04am UTC
Well, without more context, all I can say is that it looks weird.
I would be interested to know what your tutors answer is!
Andy
PS Have you tried to get the code to compile??
anupthapa (4)
Oct 15, 2012 at 1:24am UTC
i tried but its not working.it says 'compiler error' and also 'call of non-function'
helios (10126)
Oct 15, 2012 at 2:59am UTC
Sorry, the tutor has given only these lines of codes and nothing more. We have to write the output with expanation as well.
It takes a very special kind of asshole to give an exercise like this. Honestly, if I'm in a class and they give an assignment like this, I'd just get up and leave.
andywestken (1950)
Oct 15, 2012 at 12:57pm UTC
Actually, I was wondering if your tutor was looking for answers like "it won't compile", or "it's a load or rubbish/garbage" or even "rhubarb".
But this might be unclear to a beginner. The thing is, it is perfectly possible to define a "suitable" abstract type for the variable rhubarb which would not only compile but do a number of different things.
But it would be pointlessly daft code, and something that should get any professional code put up against the wall and shot if they wrote it as anything other than (an attempt at) a joke.
And I'm very much with helios sentiment. It's a far from useful problem.
Andy
Last edited on Oct 15, 2012 at 12:59pm UTC
L B (3327)
Oct 15, 2012 at 1:39pm UTC
I can prove that helios is innocent.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <iostream>
using namespace std;
struct jerkface
{
const jerkface &operator ()(const jerkface &) const
{
return *this ;
}
friend istream &operator >>(istream &is, jerkface &)
{
return is;
}
friend ostream &operator <<(ostream &os, const jerkface &)
{
return os;
}
};
int main()
{
jerkface rhubarb;
cin >> rhubarb;
rhubarb = rhubarb( rhubarb );
cout << rhubarb;
}
http://ideone.com/6SlF9
The output, ladies and gentlemen, is nothing.
Last edited on Oct 15, 2012 at 1:44pm UTC
andywestken (1950)
Oct 15, 2012 at 3:31pm UTC
Well, I got rhubarb as my output!
hello
rhubarb rhubarb rhubarb rhubarb
pointless
rhubarb rhubarb rhubarb rhubarb rhubarb rhubarb rhubarb rhubarb
42
rhubarb rhubarb rhubarb rhubarb rhubarb
321
rhubarb rhubarb rhubarb rhubarb rhubarb rhubarb rhubarb rhubarb
Andy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Rhubarb {
private :
string m_value;
public :
Rhubarb& operator ()(const Rhubarb& rhu) {
srand(rhu.m_value.length());
m_value.clear();
size_t count = rand() % 10;
while (0 < count--) {
if (!m_value.empty())
m_value += " " ;
m_value += "rhubarb" ;
}
return *this ;
}
friend istream& operator >>(istream& is, Rhubarb& rhu) {
is >> rhu.m_value;
return is;
}
friend ostream& operator <<(ostream& os, const Rhubarb& rhu) {
os << rhu.m_value;
return os;
}
};
int main() {
Rhubarb rhubarb;
cin >> rhubarb;
rhubarb = rhubarb( rhubarb );
cout << rhubarb;
return 0;
}
Built using CodeLite with MinGW version of GCC 4.6.1 and run on Windows 7
Last edited on Oct 15, 2012 at 3:58pm UTC
Topic archived. No new replies allowed.