954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Cannot get PAIR::big to just display greater number

Hi,

Appreciate any help. My code compiles... builds.... executes just fine. Only problem I'm having is... I cant figure out how to get PAIR::big() to simply print out the larger #. When I execute... it displays... the Larger # like I want... but then for some reason it displays the smaller number next to it. I want to get rid of the smaller #.

Any suggestions?

Thanks Mike

#include <iostream>
using namespace std;
 
class PAIR
 
{
private:
 int a;
 int b;
 
public:
 
 void print();
 PAIR();
 PAIR(int);
 PAIR(int,int);
 ~PAIR();

 void swap();
 int diff();
 int big();
 int area();
};

int main()
{
PAIR c, d(2), e(12,13);

int ans;
c.print();
d.print();
e.print();

e.swap();
e.print();

ans = c.diff();
cout << ans << endl;

e.big();
e.print();

return 0;
}

PAIR::PAIR() 
{
a = 2;
b= 3;
}

void PAIR::print()
{
cout << a << " " << b << endl;
}

PAIR:: ~PAIR()
{
cout << "Display Destructor Message" << endl;
}

PAIR::PAIR(int p1)
{
a=p1;
b=p1;
}

PAIR::PAIR(int p1,int p2)
{
a=p1;
b=p2;
}

void PAIR::swap()
{
 int c;
 c=a;
 a=b;
 b=c;
}

int PAIR::diff()
{
return b - a;
}

int PAIR::big()
{
if (a > b)
{
return a;
}

else 
{
return b;
}
}
ToySoldier
Newbie Poster
20 posts since Oct 2005
Reputation Points: 18
Solved Threads: 0
 

Hi,

#include <iostream>
using namespace std;
  class PAIR
 
{
private:
 int a;
 int b;
 
public:
 
 void print();
 PAIR();
 PAIR(int);
 PAIR(int,int);
 ~PAIR();

 void swap();
 int diff();
 int big();
 int area();
};

int main()
{
PAIR c, d(2), e(12,13);

int ans;
c.print();
d.print();
e.print();

e.swap();
e.print();

ans = c.diff();
cout << ans << endl;
e.big();
e.print(); 

return 0;
}

PAIR::PAIR() 
{
a = 2;
b= 3;
}

void PAIR::print()
{
cout << a << " " << b << endl;
}

PAIR:: ~PAIR()
{
cout << "Display Destructor Message" << endl;
}

PAIR::PAIR(int p1)
{
a=p1;
b=p1;
}

PAIR::PAIR(int p1,int p2)
{
a=p1;
b=p2;
}

void PAIR::swap()
{
 int c;
 c=a;
 a=b;
 b=c;
}

int PAIR::diff()
{
return b - a;
}

 int PAIR::big()
{
if (a > b)
{
return a;
}

else 
{
return b;
} 
}

The reason is that there is no temporary variable to accept the bigger value returned by the "big()" function and hence you end up printing the original value of your object "e". So you can write something like this:

int big = e.big () ;
cout << "\nThe bigger number is " << big ;


Hope it helped, bye.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Thanks... I'll try it out.

ToySoldier
Newbie Poster
20 posts since Oct 2005
Reputation Points: 18
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You