Using Bus Card Object in the User Object

Hey guys, I want to use CARD object for the USER object. This is a simple Public Transportation Card project. The card has "enum cardType" and "int Balance". Every user must have 2 cards defaultly.

I know my codes going wrong but I want to set 2 cards per user.

In the user constructer I can't define the these two cards. Can anyone tell me how can I set these two cards to per user.

Here are my files;

//CARD.H

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef card_h
#define card_h
#include <stdio.h>
#include <iostream>
#include <string.h>

using namespace std;

enum card_Types { cardStudent, cardNormal, cardRetired };

class card{
public:
    card(card_Types, int);
private:
    int balance;
    card_Types card_Type;
};



//CARD.CPP

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <iostream>
#include <string.h>
#include "card.h"

using namespace std;

card::card(card_Types ct, int b){
    card_Type = ct;
    balance = b;
}


//USER.H

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef user_h
#define user_h
#include "person.h"
#include <string>
#include <vector>
#include "card.h"

class user : public person {
public:
    card::card card1;
    card::card card2;
    user(string, string, int, personTypes, card_Types, card, card);
    static void loadBalance(vector<person> list, int tc, int balance, card c);
    void readCard(card_Types, int);
    void showCardState(card_Types, int);
private:
    card card1;
    card card2;
};
#endif /* user_h */ 


//USER.CPP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <iostream>
#include <vector>
#include "user.h"
#include "card.h"

using namespace std;

user::user(string n, string s, int tc, personTypes pt, card_Types ct, card c1, card c2)
    :person(n, s, tc, pt, ct, c1, c2){
        c1 = card::card(ct,0);
        c2 = card::card(ct, 0);
        card1 = c1;
        card2 = c2;
}
Last edited on
1) Please use code tags when posting code, to make it readable.

http://www.cplusplus.com/articles/z13hAqkS/

2) You haven't actually told us what the problem is. We're not mind-readers. The more information you give us, the easier it will be for us to find the problem.

Do you have compiler errors? Linker errors? Runtime problems? If so, then tell us what they are.

3) Your user constructor doesn't make much sense:

1
2
3
4
5
6
7
user::user(string n, string s, int tc, personTypes pt, card_Types ct, card c1, card c2)
:person(n, s, tc, pt, ct, c1, c2){
    c1 = card::card(ct,0);
    c2 = card::card(ct, 0);
    card1 = c1;
    card2 = c2;
} 


Why are you trying to assign values to c1 and c2? You're passing those arguments into user::user(), and then trying to immediately overwrite them with new objects in lines 3 - 4.

Then, having overwritten your values of c1 and c2 with new values, the only thing you do with them is to assign them to the data members card1 and card2, which makes the whole existence of c1 and c2 pointless.

I suspect what you really intended to do is to pass c1 and c2 into the user constructor, and set the data members equal to those objects. In that case, you want to get rid of lines 3 - 4 entirely, and simply assign the values of c1 and c2 to card1 and card2.

However...

4) Assigning values to data members in a constructor is inefficient. In your example, first, card1 and card2 are default-constructed, and then assignment occurs. It's much more efficient to use an initialiser list, so those objects are simply constructed with the desired values:

1
2
3
4
5
6
user::user(string n, string s, int tc, personTypes pt, card_Types ct, card c1, card c2)
: person(n, s, tc, pt, ct, c1, c2)
, card1(c1)
, card2(c2)
{
} 


5) I can see you're passing c1 and c2 into the constructor for person. That makes me suspicious. You haven't shown us the definition of person. Does person need those values for anything? You're storing those objects in the user subclass; why do that if the base class is already storing them?
Last edited on
First thank you for warning me about code tags. Fixed this. And I fixed the question also. I know those codes pieces are wrong. I just want to use cards for users. How can I set them as a property of users. Thanks by the way :)
You're welcome!

I think I've already answered your question, as far as I can see. I've shown you how to initialise the two card objects held by the user.

What, specifically, are you still having trouble with?

(I also asked some questions, to try and find out some specifics of what you don't understand, but clearly you weren't interested in answering them.)
Topic archived. No new replies allowed.