Classes output

Hello,

I am learning about classes and would like to understand line for line what is actually happening in this code here.

What is A() { a=2; } exactly? What does it do? What is with the {} after each entry under public?
What does ":" on the A(int b) mean?

I have further questions but I think these will be a good start. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include <iostream>
class A { 
 int a;
 public:
  A() { a=2; }
  A(int b) : a(b++) { a+=++b; }
  A& operator (A &b) { b.a =a; return b; } 
  void print() { std::cout << a << 
  std::endl; }
 };
 
int main ()
{
 A l(3), r; // l.a = 3, b = 4, b = 5, l.a = 8, r.a = 2
 (l r).print(); // r.a = 16
 // Output: "16"
 return 0;
}
Topic archived. No new replies allowed.