How to multiply with another variable in class?

Hello!
Please, I wish to cound second ID for the products objects of the class.
WHERE should I declare 2( int m)? What if I want to change the value of it in each start of program?
Many thanks!

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>
using namespace std;


class product {
   
  public:
    int m=2;
    int ID;
    string name;
    string price;
    product(int, string, string);
    int IDsecond(){return (ID*m);}
};

product::product(int a, string b, string c) {
  ID = a;
  name = b;
  price=c;
}


int main(){    
  
  /*product toy;
  product book;
  product dress;*/

  product toy (1, "Toy", "11.22€");
  product book (2, "Book", "22.33€");
  product dress (3, "Dress", "33.44€");

     cout<<toy.ID<<endl<<toy.name<<endl<<toy.price<<endl<<endl<<book.ID<<endl<<book.name<<endl<<book.price<<endl<<endl<<dress.ID<<endl<<dress.name<<endl<<dress.price<<endl;
cout<<endl<<endl<<toy.IDsecond()<<endl;

return 0;

}

  
Last edited on
do you mean you will not modify it ? that should work w/ -std=c++11 compiler flag
sorry, still unclear...
closed account (28poGNh0)
You can assing it in your constructor

1
2
3
4
5
6
7
product::product(int a, string b, string c) {
  ID = a;
  name = b;
  price=c;

  m = 2;
}
Topic archived. No new replies allowed.