another "why" question - classes

Here is the code for a tut on classes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// example: class constructor
#include <iostream>
using namespace std;

class CRectangle {
    int width, height;
  public:
    CRectangle (int,int);
    int area () {return (width*height);}
};

CRectangle::CRectangle (int a, int b) {
  width = a;
  height = b;
}

int main () {
  CRectangle rect (3,4);
  CRectangle rectb (5,6);
  cout << "rect area: " << rect.area() << endl;
  cout << "rectb area: " << rectb.area() << endl;
  return 0;
}


It seems to me prototypes are just extra code and confusing. Why is the tut example better than my version of it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// example: class constructor
#include <iostream>
using namespace std;

class CRectangle {
    int width, height;
  public:
    CRectangle (int a,int b){
        width = a;
        height = b;
    }
    int area () {return (width*height);}
};

int main () {
  CRectangle rect (3,4);
  CRectangle rectb (5,6);
  cout << "rect area: " << rect.area() << endl;
  cout << "rectb area: " << rectb.area() << endl;
  return 0;
}
That is the case for single file projects. In this case, it's just more code to maintain.
However, in multi file projects this is necessary to allow different files access to the functions declared in other files.

Oh. It is also easier to read a class that has its method definitions outside of it.
ahhh OK, thanks. That explains why there is so much of this :: in the Civ IV files.
Topic archived. No new replies allowed.