what is ++ difference

what is the differnce between
c = ++a and c = a++?
the book said
c = ++a means u increment a then put c
c = a++ means u put c first then increment.

But i still don't undersatnd anyone can give me a better explanation?

i need to know this becuase i am studying overloading an operator and look
1
2
3
4
5
6
7
dimension dimension::operator++(void)
++inches;
return*this;

void main(void) {
dimension one(4,5); dimension two = ++one
//both object = 4 feet 6 inches 


1
2
3
4
5
6
7
dimension dimension::operator++(void)
inches++;
return*this;

void main(void) {
dimension one(4,5); dimension two = one++
//one.inches = 6 feet 6 inches and two.inches = 5 


why just simply changing the position of ++ will give 4 feet 6 inches for both if ++inches and 6 feet 6 inches for one.inches for inches++? i don't get it anyone can explain it to me? (please ignore my syntax errors i am just being lazy).
Last edited on
Look at what the book said again, and try this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int a = 0;
int b = 0;
std::cout<<"a = "<<a<<std::endl;
std::cout<<"b = "<<b<<std::endl;
a = b++;
std::cout<<"a = "<<a<<std::endl;
std::cout<<"b = "<<b<<std::endl;

a = 0;
b = 0;
std::cout<<"a = "<<a<<std::endl;
std::cout<<"b = "<<b<<std::endl;
a = ++b;
std::cout<<"a = "<<a<<std::endl;
std::cout<<"b = "<<b<<std::endl;


When you have ++b (pre-increment) you add to 1 b before assigning it to a. When you have b++ (post-increment) you assign to a first, then to add 1 to b. There isn't really a much simpler explanation than that.
the part i don't understand is this
why one.inches = 6 feet 6 inches, i thought it's 4 feet 6 inches. i just don't know how they get the 6 feet
jimmy... take a look at this code and output...
It won't get much simpler than this...

1
2
3
4
5
6
7
8
9
10
11
12
13
int a = 5;
int b = 7;
std::cout << "a: " << a << std::endl; // Prints 5
std::cout << "b: " << b << std::endl; // Prints 7
a = b++; // After this executes, a will be equal to 7, and b will be equal to 8.
/*
Explanation
a = 5
b = 7
a = b++            <------------  Assigns b to a, making a = 7, then increments b by 1, making b = 8.
*/
std::cout << "a: " << a << std::endl; // Prints 7
std::cout << "b: " << b << std::endl; // Prints 8 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
int a = 5;
int b = 7;
std::cout << "a: " << a << std::endl; // Prints 5
std::cout << "b: " << b << std::endl; // Prints 7
a = ++b; // After this executes, a and b will both equal 8.
/*
Explanation
a = 5
b = 7

a = ++b      <--------  Adds 1 to b, making b = 8, then assigns b to a, making a = 8.
*/
std::cout << "a: " << a << std::endl; // Prints 8
std::cout << "b: " << b << std::endl; // Prints 8 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int a = 0;
int b = 0;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
a = b++;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;

a = 0;
b = 0;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
a = ++b;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;


a = 0
b = 0

a = 0
b = 1



a = 0
b = 0

a = 1
b = 1
Thanks, Kyon, for copying my code and re-posting it only five hours later...
Just for your information, I copied Zhuge's code and made the output a bit clearer. If this offended you in anyway, I am dearly sorry.
Last edited on
Even though I posted a full explanation right after he posted his?
Topic archived. No new replies allowed.