constructor

hi experts,
I have a question about class constructor.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class salesman{
private:
 string name;
 int ID;
public:
 salesman();
};

salesman::salesman(string a, int b){
  name = a;
  ID = b;
}

class transaction {
private:
 int itemID;
 salesman seller;
public:
 transaction(int, string, int);
};

transaction::transaction(int item, string name, int ID):seller(name, ID){
 itemID = item;
}


This is my class file just briefly write it hope all understand..
Now my problem is:
When I want to create a transaction, but i always need to delay a new transaction object. For example in my main function:

1
2
3
4
void main(){
 transaction x(5,5,"Ape");
 transaction y(6,6,"Ball");
}

if i have many transaction to input, how i gonna do it?

I have think of create a method in class transaction call "addTransaction" but i dunno how could i initialize the object "seller", for example:

1
2
3
4
5
6
7
8
9
10
void transaction::addTransaction(int item, string name, int ID){
 itemID = item;
 seller(name, ID);  // I know this part is wrong!!
}

void main(){
 transaction x;
 x.addTransaction(5,5,"Ape");
 x.addTransaction(6,6,"Ball");
}


So, can give me idea to solve the error part? for initialize the object "seller".
First, use int main()

Second, your transaction constructor takes an int, a string and an int, in that order, but you give it an int, an int, and a string in that order.

Anyway, I'm not sure what this means:
When I want to create a transaction, but i always need to delay a new transaction object.
ops.. maybe tats is my typing error...
i know tat will be x.addTransaction(5,"ape",5) like this..
but my main problem is how to initialize the object seller in addTransaction().
seller(name,ID) this is a wrong statement....

When I want to create a transaction, but i always need to delay a new transaction object.

this means:
during running my program, maybe i need to add many data of transaction. It cant be i keep on declare new object "transaction" to do it rite? for example: i want to key in 10 data
1
2
3
4
5
6
7
8
9
10
transaction a(5,"a",5);
transaction b(5,"b",5);
transaction c(5,"c",5);
transaction d(5,"d",5);
transaction e(5,"e",5);
transaction f(5,"f",5);
transaction g(5,"g",5);
transaction h(5,"h",5);
transaction i(5,"i",5);
transaction j(5,"j",5);


it not a good way to do it rite?
so i decide to use addTransaction()
1
2
3
4
5
6
7
8
9
10
11
transaction x;
x.addTransaction(5,a,5)
x.addTransaction(5,b,5)
x.addTransaction(5,c,5)
x.addTransaction(5,d,5)
x.addTransaction(5,e,5)
x.addTransaction(5,f,5)
x.addTransaction(5,g,5)
x.addTransaction(5,h,5)
x.addTransaction(5,i,5)
x.addTransaction(5,j,5)

izit better to do like this?

so how would i initialize the object "seller" ?
seller(name, ID)
this is my main problem...

thx for reply..=)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class salesman {
  public:
     salesman( std::string name, int ID ); 
};

class transaction {
   public:
      transaction( int item, salesman s ) : item( item ), seller( s ) {}
   private:
      int item;
      salesman seller;
};

int main() {
   // Read in input (item, name, ID)
   transaction t( item, salesman( name, ID ) );
}


oh thx, you're guiding me the way to solve my problem...
THX firedraco and jsmith~
Topic archived. No new replies allowed.