Variables

Hey I've been watching this forum for a while and finally decided to stop being lazy and start learning how to program. Thanks in advance for the help :)

My question is: Why does the variable have to use an = sign instead of () in the second set of code.

For example, instead of a = 5; it could be a (5);
But when I try to do that it messes up this code and won't let it work. The only way that I can get that way to work is by doing this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main ()
{
  int a (5);             
  int b (2);             
  int result;            

  a = a + 3;
  result = a - b;
  cout << result;

  cin.get();
}


And the code that it messes up when I try to use () is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main ()
{
	int a, b;
	int result;

	a = 5; //if I put a (5); instead of a = 5 it won't work
	b = 2;
	a = a + 3;
	
        result = a - b;
	cout << result;
	
	cin.get();
}


Sorry if this is a stupid question but I think the only way to learn is to understand it, instead of memorizing it.
Last edited on
() are used for the constructor. When initializing a variable () and = have the same meaning but after that they change their meanings and to assign a value to an object you should use =
Thanks Bazzy.
Topic archived. No new replies allowed.