Remortgages | Houses for Sale | Loans | Mobile Phones | Buy Anything On eBay
overloading the << operator.... [Archive] - PCMech Forums

PDA

View Full Version : overloading the << operator....


GSXdan
11-25-2003, 05:30 PM
please see:

http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=3&MsgID=227340&Setting=A9999F0001

TIA

doctorgonzo
11-26-2003, 09:08 AM
Okay, here we go...

First, the brackets in Deck::dealcard are all funky again. I have know idea what you are trying to do, so I probably didn't put the brackets in the right place, but I do know that I needed to put one more } bracket in there to get it to compile.

Second, you have to change the -> in the operator>> overload to a period.

You also have to change the declaration of the getMessage method by adding const at the end. Otherwise, the operator>> overload won't work, beause you say that the Card &X is const, and therefore all the member functions used in that method must be const.

You shouldn't need that "friend ostream..." anywhere, so I took it out.

The following code will compile, but you have to figure out if it works:

card.h


#include &lt;iostream&gt;
#include &lt;string&gt;
#ifndef CARD_H
#define CARD_H
using namespace std;

class Card {
public:
Card(int, int, string);
int getType() {return Ctype;} // < 0 pay, > 0 collect, = 0 GOOJF
int getFlag() {return flag;}
void setFlag(int x) {flag = x;}
string getMessage() const {return message;}


private:

int Ctype;
int amount;
string message;
static int flag;
};

class Deck {

public:
Deck();
Card* dealcard();
void discard(Card*);

private:
Card* data[10];
int top, bottom;
};
#endif


card.cpp

#include &lt;stdlib.h&gt;
#include "card.h"
#include &lt;iostream&gt;

using namespace std;

Card::Card(int type, int money, string M) {
Ctype = type;
amount = money;
message = M;
}
// static int intializer

int Card::flag = 1;

Deck::Deck() {
top = 9;
bottom = 8;
data[0] = new Card(0, 0, "Get out of BG Police Free");
data[1] = new Card(-1, 100, "Pay Health Center $100");
data[2] = new Card(-1, 50, "Pay Parking Fees $50");
data[3] = new Card(1, 200, "Collect $200 for winning BG lottery");
data[4] = new Card(-1, 550, "Pay Tuition $550");
data[5] = new Card(1, 600, "Collect $600 for scholarship");
data[6] = new Card(-1, 250, "Pay $250 for text books");
data[7] = new Card(-1, 640, "Pay Residence Hall fee $640");
data[8] = new Card(1, 350, "Collect job money $350");
data[9] = new Card(-1, 250, "Pay $250 for car maintenance");
}

Card* Deck::dealcard() {
Card* tmp;
if(data[top]->getType() == 0) {
if(data[top]->getFlag() == 0) {
if(top == 9) {
top = 0;
bottom++;
}
else {
top++;
if(bottom == 9) {
bottom = 0;
}
else {
bottom++;
}
}
tmp = data[top];
data[top]->setFlag(0);
}
else {
tmp = data[top];
}
if(top == 9) {
top = 0;
bottom++;
}
else {
top++;
if(bottom == 9) {
bottom = 0;
}
else {
bottom++;
}
}
return tmp;
}
}


void Deck::discard(Card* card) {
if(card->getType() == 0) {
data[0]->setFlag(1);
}
}





ostream& operator<<(ostream& out, const Card& X) {
out << X.getMessage() << endl;
return out;
}

HNPFL
11-30-2003, 10:54 PM
Boy do I hate those details.